Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diamond has a value (in Mars dollars M$). When making the payment, the chain can be cut at any position for only once and some of the diamonds are taken off the chain one by one. Once a diamond is off the chain, it cannot be taken back. For example, if we have a chain of 8 diamonds with values M$3, 2, 1, 5, 4, 6, 8, 7, and we must pay M$15. We may have 3 options:

  1. Cut the chain between 4 and 6, and take off the diamonds from the position 1 to 5 (with values 3+2+1+5+4=15).
  2. Cut before 5 or after 6, and take off the diamonds from the position 4 to 6 (with values 5+4+6=15).
  3. Cut before 8, and take off the diamonds from the position 7 to 8 (with values 8+7=15).

Now given the chain of diamond values and the amount that a customer has to pay, you are supposed to list all the paying options for the customer.

If it is impossible to pay the exact amount, you must suggest solutions with minimum lost.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 numbers: N (≤10​5​​), the total number of diamonds on the chain, and M (≤10​8​​), the amount that the customer has to pay. Then the next line contains N positive numbers D​1​​⋯D​N​​ (D​i​​≤10​3​​ for all i=1,⋯,N) which are the values of the diamonds. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print i-j in a line for each pair of ij such that Di + ... + Dj = M. Note that if there are more than one solution, all the solutions must be printed in increasing order of i.

If there is no solution, output i-j for pairs of ij such that Di + ... + Dj >M with (Di + ... + Dj −M) minimized. Again all the solutions must be printed in increasing order of i.

It is guaranteed that the total value of diamonds is sufficient to pay the given amount.

Sample Input 1:

16 15
3 2 1 5 4 6 8 7 16 10 15 11 9 12 14 13

Sample Output 1:

1-5
4-6
7-8
11-11

Sample Input 2:

5 13
2 4 5 7 9

Sample Output 2:

2-4
4-5

分析请参考【PAT1044】Shopping in Mars 二分法

注意:该题对时间有限制,需要用改进的算法实现,之前实现了一个O(N^2)级的,三个测试点没过,采用二分法即可解决。

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std; static const long int MAX = ; long int n, pay;
int M[MAX];
int D[MAX];
vector<long int> v; int findBestsum(int i, int n, int ddl){
int left = i+;
int right = n;
while(left<right){
int mid = (left+right)/;
if(M[mid]-M[i]>=ddl){
right = mid;
}
else{
left = mid+;
}
}
D[i] = right;
return M[right] - M[i];
} int main(){
cin>>n>>pay;
M[] = ;
for(long int i=;i<=n;i++){
cin>>M[i];
M[i] = M[i] + M[i-];
}
int res;
int temp = ;
for(int i=;i<n;i++){
res = findBestsum(i, n, pay);
if(res>=pay){
if(res<temp){
temp = res;
v.clear();
v.push_back(i);
}
else if(res==temp){
v.push_back(i);
}
}
}
for(vector<long int>::iterator it=v.begin();it!=v.end();it++){
cout<<*it+<<"-"<<D[*it]<<endl;
}
return ;
}

1044 Shopping in Mars的更多相关文章

  1. 1044 Shopping in Mars (25 分)

    1044 Shopping in Mars (25 分) Shopping in Mars is quite a different experience. The Mars people pay b ...

  2. PAT 1044 Shopping in Mars[二分][难]

    1044 Shopping in Mars(25 分) Shopping in Mars is quite a different experience. The Mars people pay by ...

  3. PAT 甲级 1044 Shopping in Mars (25 分)(滑动窗口,尺取法,也可二分)

    1044 Shopping in Mars (25 分)   Shopping in Mars is quite a different experience. The Mars people pay ...

  4. PAT 甲级 1044 Shopping in Mars

    https://pintia.cn/problem-sets/994805342720868352/problems/994805439202443264 Shopping in Mars is qu ...

  5. PTA(Advanced Level)1044.Shopping in Mars

    Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diam ...

  6. 1044 Shopping in Mars (25 分)

    Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diam ...

  7. PAT Advanced 1044 Shopping in Mars (25) [⼆分查找]

    题目 Shopping in Mars is quite a diferent experience. The Mars people pay by chained diamonds. Each di ...

  8. 1044 Shopping in Mars (25分)(二分查找)

    Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diam ...

  9. 1044. Shopping in Mars (25)

    分析: 考察二分,简单模拟会超时,优化后时间正好,但二分速度快些,注意以下几点: (1):如果一个序列D1 ... Dn,如果我们计算Di到Dj的和, 那么我们可以计算D1到Dj的和sum1,D1到D ...

随机推荐

  1. SQL IF while 游标

    -- if语句使用示例 declare @a int set @a=1 begin print @a =@a+1 end else begin print 'noooo' end -- while语句 ...

  2. zabbix第一篇:zabbix安装及使用

    常用软件安装及使用目录 一:搭建zabbix命令集 cat /etc/redhat-release uname -r getenforce systemctl status firewalld.ser ...

  3. java工程文件路径的问题

    String classpath = this.getClass().getResource("/").getPath().replaceFirst("/WEB-INF/ ...

  4. Daily Scrum9 11.13

    昨天的任务已完成. 今日任务: 姓名 今日任务 时长 徐钧鸿 测试SQL包里的代码 2h 张艺 继续搭建还没搭建完的框架 修复bug 2h 黄可嵩 继续进行搜索响应编写 2h 徐方宇 搭建框架 修改b ...

  5. “私人助手”Beta版使用说明

    私人助手(Beta)版使用说明 私人助手这款软件是通过添加事件提醒功能,让用户能在正确的时间做正确的事情,使得工作变得更有效率,而这款软件的特色在于提醒模式的添加,用户可以通过震动.铃声提醒,我们的特 ...

  6. Task 4 求数组的连续子数组的最大和(团队合作)

    小组成员:李敏.刘子晗 1.设计思想:由于已经做过这个题目,只要对之前的程序加上相应的测试和约束即可.我们两个人一起商议后,决定了程序的主框架和并列出了最终可以实现的功能.先要定义数组长度和上下限的变 ...

  7. 初入React(一)

    React:是2013年Facebook在github上的一个开源js库,它将用户界面抽象为一个个组件,再由开发者将其组合成页面.它不是完整的MVC/MVVM框架,专注于提供清晰.简洁的view层解决 ...

  8. C++ socket网络爬虫(1)

    C++写的socket网络爬虫,代码会在最后一次讲解中提供给大家,同时我也会在写的同时不断的对代码进行完善与修改 我首先向大家讲解如何将网页中的内容,文本,图片等下载到电脑中. 我会教大家如何将百度首 ...

  9. Genymotion-ARM-Translation_v1.1安装报“an error occured while deploying the file”

    如上图,在将Genymotion-ARM-Translation_v1.1.zip拖动Genymotion虚拟机中报了图中错误,在百度后找到了解决办法,下面是我的操作步骤 1.下载re管理器之类的ap ...

  10. 一键轻松查看apk包名和Main Activity

    环境 Windows系统(我的是Win10 64位) Python3(我的是3.6.1) 已安装Git 安装 pip install git+https://github.com/codeskyblu ...