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. 轻院校赛-zzuli 2266: number【用每位的二进制的幂的和来进行hash(映射)处理】

    zzuli 2266: number 大致题意:   给定n,问有多少数对<x, y>满足: x, y∈[1, n], x < y            x, y中出现的[0, 9] ...

  2. JQgrid处理json数据

    jqGrid 实例中文版网址:http://blog.mn886.net/jqGrid/国外官网:http://www.trirand.com/blog/jqgrid/jqgrid.html http ...

  3. jQuery实现点击复制效果

    <!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" ...

  4. 7月新的开始 - Axure学习05 - 元件库的创建

    元件库的创建 元件库的创建.载入.编辑和删除等操作 元件库的后缀名名:.rplib 学习结果: 实现iPhone6的原型图 分辨率是 1080*1920,现在我们按比例缩小去实现(360*640)

  5. django-haystack 安装No local packages or working download links found for setuptools_scm

    在虚拟环境中安装drf-haystack时,pip报错 Liang-2:~ langying$ pip install django-haystack Collecting django-haysta ...

  6. Python中日志logging模块

    # coding:utf-8 import logging import os import time class Logger(object): def __init__(self): # 创建一个 ...

  7. h5css样式

    兼容性前缀: 谷歌:webkit 火狐:moz ie:ms 欧鹏:o选择器: 属性选择器: * = 包含 {href * = 'www'} ^ = 以什么开头 $ = 以什么结尾 伪类选择器: 第一个 ...

  8. spring-定时任务<task:scheduled-tasks>

    Spring内部有一个task是Spring自带的一个设定时间自动任务调度,提供了两种方式进行配置,一种是注解的方式,而另外一种就是XML配置方式了.注解方式比较简洁,XML配置方式相对而言有些繁琐, ...

  9. Android工程的合并

    http://www.xuanyusong.com/archives/3395 1.游戏包名( 类似 com.xx.xxx ) Android应用程序只能有一个包名,如果两个游戏包名一样,那么后者安装 ...

  10. polyfit 多项式曲线拟合matlab

    polyfit 多项式曲线拟合 全页折叠 语法 p = polyfit(x,y,n) [p,S] = polyfit(x,y,n) [p,S,mu] = polyfit(x,y,n)   说明 示例 ...