PAT 甲级 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:
- 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).
- Cut before 5 or after 6, and take off the diamonds from the position 4 to 6 (with values 5+4+6=15).
- 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 D1⋯DN (Di≤103 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 分)(滑动窗口,尺取法,也可二分)的更多相关文章
- PAT Advanced 1044 Shopping in Mars (25) [⼆分查找]
题目 Shopping in Mars is quite a diferent experience. The Mars people pay by chained diamonds. Each di ...
- 1044 Shopping in Mars (25分)(二分查找)
Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diam ...
- PAT 甲级 1044 Shopping in Mars
https://pintia.cn/problem-sets/994805342720868352/problems/994805439202443264 Shopping in Mars is qu ...
- 1044 Shopping in Mars (25 分)
Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diam ...
- 【PAT甲级】1044 Shopping in Mars (25 分)(前缀和,双指针)
题意: 输入一个正整数N和M(N<=1e5,M<=1e8),接下来输入N个正整数(<=1e3),按照升序输出"i-j",i~j的和等于M或者是最小的大于M的数段. ...
- PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)
1040 Longest Symmetric String (25 分) Given a string, you are supposed to output the length of the ...
- PAT 甲级 1083 List Grades (25 分)
1083 List Grades (25 分) Given a list of N student records with name, ID and grade. You are supposed ...
- PAT甲级——1130 Infix Expression (25 分)
1130 Infix Expression (25 分)(找规律.中序遍历) 我是先在CSDN上面发表的这篇文章https://blog.csdn.net/weixin_44385565/articl ...
- 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 ...
随机推荐
- Robot Framework--标签Tag
Robot Framework的标签是一个简单而又强大的分类机制,功能如下: 标签在reports,logs以及测试数据中展示,显示关于测试用例的元数据信息 用例的执行统计(total,passed, ...
- py停止工作
# 参考https://blog.csdn.net/w952470866/article/details/79132955 电脑搬动换了网络后打开pycharm显示停止工作解决办法: 将python. ...
- Mac下mysql出现错误:ERROR 1055 (42000)
问题原因: ONLY_FULL_GROUP_BY的意思是:对于GROUP BY聚合操作,如果在SELECT中的列,没有在GROUP BY中出现,那么这个SQL是不合法的,因为列不在GROUP BY从句 ...
- 基于 CSS 的 Web 框架 CJSS
CJSS 是一个基于 CSS 的 Web 框架,所有效果都在 CSS 文件中生效,可以在 CSS 中使用它添加更多功能,或者构建一个完整的页面. 使用方法: HTML 想要使用某个组件,在 CSS 文 ...
- 2019牛客多校B Beauty Values——思维题
题目 求所有子区间中不同元素之和. 分析 枚举相邻的相同数字形成的区间,计算它是哪些区间的子区间,每次有-1的贡献,将其从总贡献中减去. #include<bits/stdc++.h> u ...
- 什么是 restful 规范?
一种软件架构风格.设计风格,而不是标准,只是提供了一组设计原则和约束条件. 一.协议 API与用户的通信协议,总是使用HTTPs协议. 什么是https协议 二.域名 应该尽量将API部署在专用域名之 ...
- Java Executor框架使用
Java Executor框架是Jdk1.5之后推出的,是为了更加方便的开发多线程应用而封装的框架: 相比传统的Thread类,Java Executor使用方便,性能更好,更易于管理,而且支持线程池 ...
- zookeeper 集群简单搭建,以及Error contacting service,It is probably not running问题解决
第一步:现在http://www-eu.apache.org/dist/zookeeper/zookeeper-3.4.9/ 下载一个gz包,然后解压.当然,zookeeper 需要在java 的环境 ...
- RabbitMQ的5种模式
队列截图,去rabbitMq.com去找学习文档 =========================================================================== ...
- [luogu] 斐波那契数列
https://www.luogu.org/problemnew/show/P1962 矩阵快速幂加速 #include <bits/stdc++.h> using namespace s ...