1044 Shopping in Mars (25 分)
 

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 (≤), the total number of diamonds on the chain, and M (≤), 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 ,) 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 > with (Di + ... + Dj −) 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

题意:

找到和不小于一个给定值、但尽可能小的所有子串

题解:

两个指针,分别指向子串头尾,具体看代码(有优化,比如说找到一个满足要求的子串后,下一次扫描左指针右移一位,右指针的起始位置就保持在上一次右指针的位置即可)。

1.使用尺取法,如果取得范围总和大于需要pay的,删掉头部

2.如果取得范围综合小于pay的,增加尾部

3.注意边界情况和没有相等的情况,细节可查看代码

4.利用一个minAns的变量进行记录答案数值

AC代码:

#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<string>
#include<cstring>
using namespace std;
int a[];
int ans[];//数组开小了就测试点3一直不过
int k=;
int n,m;
int cha=0x7fffffff;
int main(){
cin>>n>>m;
for(int i=;i<=n;i++){
cin>>a[i];
}
int r=,l=;
int s=;
while(r<=n){
while(s<m&&l<=n){
s+=a[l];
l++;
}
if(s<m){
break;
}
else if(s<cha){
k=;
ans[++k]=r;
ans[++k]=l-;
cha=s;
}else if(s==cha){
ans[++k]=r;
ans[++k]=l-;
}
s-=a[r];
r++;
}
for(int i=;i<=k;i+=){
cout<<ans[i]<<"-"<<ans[i+]<<endl;
}
return ;
}

也可以用二分:

因为所有钻石价值为正,因此从开始到某位置的链条价值和恒正,在读入价值时进行累加;

从第一个位置出发用二分法找到恰好大于或等于目标值的位置,并计算差值进行比较;

如有差值更小的数据组,则更新记录;如找到差值恰好的数据组,加入记录;

按照题目要求输出结果,并返回零值。

#include<cstdio>
#include<fstream>
const int N=;
int sum[N];
int n, S, nears=; int upper_bound(int L, int R, int x){
int left=L, right=R, mid;
while(left<right){
mid=(left+right)/;
if(sum[mid]>x){
right=mid;
} else{
left=mid+;
}
}
return left;
} int main(){
// freopen("d://in.txt","r",stdin);
scanf("%d%d", &n, &S);
sum[]=;
for(int i=; i<=n; i++){
scanf("%d", &sum[i]);
sum[i]+=sum[i-];
} for(int i=; i<=n; i++){
int j=upper_bound(i, n+, sum[i-]+S);
if(sum[j-]-sum[i-]==S){
nears=S;
break;
} else if(j<=n && sum[j]-sum[i-]<nears){
nears=sum[j]-sum[i-];
}
} for(int i=; i<=n; i++){
int j=upper_bound(i, n+, sum[i-]+nears);
if(sum[j-]-sum[i-]==nears){
printf("%d-%d\n", i, j-);
}
}
return ;
}

PAT 甲级 1044 Shopping in Mars (25 分)(滑动窗口,尺取法,也可二分)的更多相关文章

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

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

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

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

  3. PAT 甲级 1044 Shopping in Mars

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

  4. 1044 Shopping in Mars (25 分)

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

  5. 【PAT甲级】1044 Shopping in Mars (25 分)(前缀和,双指针)

    题意: 输入一个正整数N和M(N<=1e5,M<=1e8),接下来输入N个正整数(<=1e3),按照升序输出"i-j",i~j的和等于M或者是最小的大于M的数段. ...

  6. PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)

    1040 Longest Symmetric String (25 分)   Given a string, you are supposed to output the length of the ...

  7. PAT 甲级 1083 List Grades (25 分)

    1083 List Grades (25 分) Given a list of N student records with name, ID and grade. You are supposed ...

  8. PAT甲级——1130 Infix Expression (25 分)

    1130 Infix Expression (25 分)(找规律.中序遍历) 我是先在CSDN上面发表的这篇文章https://blog.csdn.net/weixin_44385565/articl ...

  9. PAT 甲级 1074 Reversing Linked List (25 分)(链表部分逆置,结合使用双端队列和栈,其实使用vector更简单呐)

    1074 Reversing Linked List (25 分)   Given a constant K and a singly linked list L, you are supposed ...

随机推荐

  1. H5s播放rtsp和rtmp视频

    最近接触的几个项目都有对接视频的功能,目前国内视频厂商以海康和大华为主,其对应的视频流格式也不一致,导致对接起来很麻烦.有幸在客户那接触到一种新的视频对接解决方案,支持Html5标准.废话不多少,看一 ...

  2. 并发编程大师系列之:wait/notify/notifyAll/condition

    1. wait().notify()和notifyAll()方法是本地方法,并且为final方法,无法被重写. 2. 调用某个对象的wait()方法能让当前线程阻塞,并且当前线程必须拥有此对象的mon ...

  3. 2020还有9天!Winforms开发有哪些期待?DevExpress 2020计划出炉

    下载DevExpress v19.2完整版 DevExpress Winforms Controls 内置140多个UI控件和库,完美构建流畅.美观且易于使用的应用程序.DevExpress Winf ...

  4. electron中引入jquery

    <!-- Insert this line above script imports --> <script>if (typeof module === 'object') { ...

  5. pid 及参数调试方法

    所谓PID指的是Proportion-Integral-Differential.翻译成中文是比例-积分-微分. 记住两句话: 1.PID是经典控制(使用年代久远) 2.PID是误差控制() 对直流电 ...

  6. BZOJ2956: 模积和——整除分块

    题意 求 $\sum_{i=1}^n \sum_{j=1}^m (n \ mod \ i)*(m \ mod \ j)$($i \neq j$),$n,m \leq 10^9$答案对 $1994041 ...

  7. BZOJ 2699: 更新 (DP)

    题目 对于一个数列A[1-N],一种寻找最大值的方法是:依次枚举A[2]到A[N],如果A[i]比当前的A[1]值要大,那么就令A[1]=A[i],最后A[1]为所求最大值.假设所有数都在范围[1, ...

  8. Python 装饰器实现单列模式

    # 使用装饰器实现单列模式 def singleton(cls): # 用来存在实例的字典 singleton_instance = {} def wrapper(*args, **kwargs): ...

  9. 56、servlet3.0-与SpringMVC整合分析

    56.servlet3.0-与SpringMVC整合分析 web容器在启动的时候,会扫描每个jar包下的META-INF/services/javax.servlet.ServletContainer ...

  10. MongoDB 副本集主从切换方法

    一.方法一rs.setpDown() 将Primary节点降级为Secondary节点 myapp:PRIMARY> rs.stepDown() 这个命令会让primary降级为Secondar ...