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. javascript常用工具类util.js

    //如果大家想要补充,请留言 /** * 判断指定名称的复选框是否被选中 * * @param {} * chname复选框名称 */ function chkCheckCha(chname) { v ...

  2. C# 通过 参数返回 C++ 指针

    参数返回 C++ 指针 C++ 代码 Extern_C BASECORELIBRARY_API char * GetFileByteArray(wchar_t * BinfilePath, wchar ...

  3. ipv4枯竭和ipv6的启用

    IPv4是Internet Protocol version 4的缩写,中文翻译为互联网通信协议(TCP/IP协议)第四版,通常简称为网际协议版本4. IPv4使用32位(4字节)地址,因此地址空间中 ...

  4. jq 字符串转数组

    一般我们在添加关键词时  会添加几组关键词     上传时怎么取值呢 取值时用以下格式 就能取到值 var FTag = "" //AAA,BBB if (FTag1 != &qu ...

  5. LOJ P10118 打鼹鼠 题解

    每日一题 day17 打卡 Analysis 二维树状数组的单点修改和区间查询,和一维的差不多 #include<iostream> #include<cstdio> #inc ...

  6. POJ P2279 Mr. Young's Picture Permutations 题解

    每日一题 day14 打卡 Analysis 五维dpf[a1,a2,a3,a4,a5]表示各排从左端起分别占了a1,a2,a3,a4,a5个人时合影方案数量然后我们枚举a1,a2,a3,a4,a5从 ...

  7. mongodb WiredTiger 内存分配

    转载自勤奋的小青蛙 mongodb占用内存非常高,这是因为官方为了提升存储的效率,设计就这么设计的. 但是大部分的个人开发者所购买的服务器内存并没有那么大,所以,我们需要配置下MongoDB的内存缓存 ...

  8. 【HTTP】协议详解

    什么是HTTP协议 协议是指计算机通信网络中两台计算机之间进行通信所必须共同遵守的规定或规则,超文本传输协议(HTTP)是一种通信协议,它允许将超文本标记语言(HTML)文档从Web服务器传送到客户端 ...

  9. 题解 CF375D 【Tree and Queries】

    首先,子树上的查询问题可以通过$DFS$序转为序列问题 再一看,没有修改,可以离线,这不就是莫队吗? 我们用$sum_i$表示出现次数$\geq i$的个数 用$val_i$表示第$i$种颜色的出现次 ...

  10. ubuntu中防火墙iptables配置

    特别说明:此文章完全转载于https://www.cnblogs.com/EasonJim/p/6851007.html 1.查看系统是否安装防火墙 root@localhost:/usr# whic ...