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 (<=105), the total number of diamonds on the chain, and M (<=108), the amount that the customer has to pay. Then the next line contains N positive numbers D1 ... DN (Di<=103 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 i <= j 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 i <= j 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
 #include<cstdio>
#include<iostream>
using namespace std;
long long diamond[], sum[];
int binSearch1(long long diamond[], long long sum[], int low, int high, long long x){
int mid, start = low;
long long pay;
while(low <= high){
mid = low + (high - low) / ;
pay = sum[mid] - sum[start] + diamond[start];
if(pay == x)
return mid;
else if(pay > x)
high = mid - ;
else low = mid + ;
}
return -;
}
int binSearch2(long long diamond[], long long sum[], int low, int high, long long x, long long &ans){
int mid, start = low;
long long pay = ;
while(low < high){
mid = low + (high - low) / ;
pay = sum[mid] - sum[start] + diamond[start];
if(pay >= x)
high = mid;
else low = mid + ;
}
pay = sum[low] - sum[start] + diamond[start];
ans = pay;
return low;
}
int main(){
long long N, M, temp = , ans, min = ;
int cut;
scanf("%lld%lld", &N, &M);
for(int i = ; i < N; i++){
scanf("%lld", &diamond[i]);
temp += diamond[i];
sum[i] = temp;
}
int find = ;
for(int i = ; i < N; i++){
cut = binSearch1(diamond, sum, i, N - , M);
if(cut != -){
printf("%d-%d\n", i + , cut + );
find = ;
}
}
if(find == ){
for(int i = ; i < N; i++){
cut = binSearch2(diamond, sum, i, N, M, ans);
if (ans < min && cut != N)
min = ans;
}
for(int i = ; i < N; i++){
cut = binSearch2(diamond, sum, i, N, M, ans);
if(sum[cut] - sum[i] + diamond[i] == min)
printf("%d-%d\n", i + , cut + , ans);
}
}
cin >> N;
return ;
}

总结:

1、题意:给出一串数字,找出它们的一个子序列使得这个子序列的和刚好等于M。如果找不到,则找一个序列使得它的和大于M但又比其它大于M的序列的和小,如果这个序列有多个,则全部输出。 可以发现暴力破解会超时,只能二分解决。由于二分要求查找的序列是有序的,可以用diamond数组记录这些数字,sum数组记录这个序列的和,其中sum[ i ]表示diamond[0] 到 diamond[ i ]的和。在计算 i 到 j 的和时,直接sum[ j ] - sum[ i ] + diamond[ i ] 即可。且sum序列为递增。

2、二分法查找第一个满足某条件的元素,最后返回的是 low,有效结果也是low而非mid!

3、可以使用p、q双指针法来求和。pq之间序列即为所求。当和过大时,ans - num[p], p++; 当和太小时, q++, ans + num[q];

A1044. Shopping in Mars的更多相关文章

  1. PAT甲级——A1044 Shopping in Mars

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

  2. A1044 Shopping in Mars (25 分)

    一.技术总结 可以开始把每个数都直接相加当前这个位置的存放所有数之前相加的结果,这样就是递增的了,把i,j位置数相减就是他们之间数的和. 需要写一个函数用于查找之间的值,如果有就放返回大于等于这个数的 ...

  3. 1044 Shopping in Mars (25 分)

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

  4. PAT 甲级 1044 Shopping in Mars

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

  5. 1044 Shopping in Mars

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

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

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

  7. pat1044. Shopping in Mars (25)

    1044. Shopping in Mars (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Shop ...

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

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

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

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

随机推荐

  1. linux RPM manager

    RPM manage:rpm2cpio package_name | cpio -id #将一个rpm包解压至当前目录rpm -qi package_name #查看一个已安装的rpm包信息rpm - ...

  2. centos6.5虚拟机安装后,没有iptables配置文件

    openstack环境里安装centos6.5系统的虚拟机,安装好后,发现没有/etc/syscofig/iptables防火墙配置文件. 解决办法如下: [root@kvm-server005 ~] ...

  3. Python_每日习题_0001_数字组合

    # Topic: There are four digits: 1, 2, 3 and 4. # How many different three digits can be formed witho ...

  4. sixsix团队“餐站”应用代码规范及开发文档

    网络爬虫文档 以下是我们软工小组关于网络爬虫部分代码的的说明文档.至于一些分功能的小函数或方法就不在此赘述,一看就能明白.下面就主要的函数进行说明. 从总体上来说主要有三部分:店家信息爬取部分,菜品信 ...

  5. #个人博客作业Week1——浏览教材后提出的六个问题及软件与软件工程的提出。

    1.通常,我们阅读软件比编写软件花费的时间更多.正因为编写软件比阅读软件要容易,因此代码的可读性显得尤为重要.那么我们在写程序时应该如何避免多余的,带有误导性的注释,写出一个利于帮助别人读懂程序的注释 ...

  6. 个人博客作业Week3--必应词典案例分析

    第一部分  调研,评测 (软件的bug,功能评测,黑箱测试,第8章 用户调研,12 章软件的用户体验) 下载并使用,按照描述的bug定义,找出几个功能性的比较严重的bug.至少两个.用专业的语言描述( ...

  7. [北航矩阵理论A]课程笔记

    [北航矩阵理论A]课程笔记 一.特征值 特征根相关: 设任一方阵 \(A = (a_{ij})_{n\times n} \in C^{n\times n}\) 特征多项式 \(T(\lambda)=| ...

  8. 《Linux内核分析》第四周学习总结

    <Linux内核分析>第四周学习总结                         ——扒开系统调用的三层皮 姓名:王玮怡  学号:20135116 理论总结部分: 第一节 用户态.内核 ...

  9. Practice4 阅读《构建之法》6-7章

    关于第五章后面的阅读已经在Practice3中有所感悟,下面是6-7章的读书笔记. 第6章 敏捷流程这一章讲了“敏捷流程”这一概念,关于这一名词我是很陌生的,在阅读之后有了一定的理解.敏捷流程是提供了 ...

  10. JAVA面对对象(三)——Super、static、final关键字

    Super 使用super关键字可以直接调用父类中的构造方法.属性.普通方法 super调用父类构造方法的操作,与this调用构造方法一样,语句必须放在子类构造方法的首行 public class P ...