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 ...
随机推荐
- 【Java】Eclipse+环境安装及新建Project
安装Eclipse 1.进入ecilpse官方下载地址:https://www.eclipse.org/downloads/?FEATURED_STORY 2.点击红色箭头指向的Download Pa ...
- Go 语言解释器 Yaegi
Yaegi 是一个优雅的 Go 语言解释器,可以执行 Go 脚本和插件. 特性 完整支持 Go 语言规范 用 Go 编写,只使用标准库 简单的解释器 API: New(), Eval(), Use() ...
- Python3连接MySQL数据库实战
Python3连接MySQL数据库实战 第三方库 :pymysql 数据库连接 def connect(): try: #建立数据库连接,从左至右参数依次为 # ip地址 我用的是云端数据库 如果为本 ...
- sql的九个常用语句是什么
一.基础1.说明:创建数据库CREATE DATABASE database-name2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备份数 ...
- 007——转载-MATLAB读取文件夹下的文件名
(一)参考文献:https://blog.csdn.net/liutaojia/article/details/84899923 (二)第一步:获取文件夹下某类型数据的所有文件名 主要包括三个步骤: ...
- spark读文件写mysql(java版)
package org.langtong.sparkdemo; import com.fasterxml.jackson.databind.ObjectMapper; import org.apach ...
- 十二.虚拟Web主机
*********************** 修改apache默认的网页文件存放位置 ]# mkdir /var/www/myweb ]# echo "I am MyWeb" & ...
- Gym - 102346G Getting Confidence 最小费用最大流
Gym - 102346GGetting Confidence 题意:n*n的格子,每个格子上有一个数,要求每行每列都只能拿一个数,使得乘积最大,然后输出每列选择的是第几行的数. 如果是加法的话,那么 ...
- P2679 子串 DP
P2679 子串 DP 从字符串A中取出\(k\)段子串,按原顺序拼接,问存在多少个方案使拼接的字符串与字符串B相同 淦,又是这种字符串dp 设状态\(ans[i][j][k]\)表示A串位置\(i\ ...
- codeforces#1248D2. The World Is Just a Programming Task(括号匹配转化为折线处理)
题目链接: http://codeforces.com/contest/1248/problem/D2 题意: 可以执行一次字符交换的操作 使得操作后的字符串,循环移位并且成功匹配的方案最多 输出最多 ...