1044. Shopping in Mars (25)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 (<=105), the total number of diamonds on the chain, and M (<=108), the amount that the customer has to pay. Then the next line contains N positive numbers D1 ... DN (Di<=103 for all i=1, ..., N) 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 > M with (Di + ... + Dj - M) 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

提交代码

 #include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
#include<cmath>
#include<string>
#include<map>
#include<set>
using namespace std;
vector<pair<int,int> > line;
#define inf 100000005
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int minsum;//历史上的最小值
int n,sum,i,j;
scanf("%d %d",&n,&sum);//规定的最小值
int *dia=new int[n+];
for(i=;i<=n;i++){
scanf("%d",&dia[i]);
}
dia[]=dia[];
j=;//虚拟0位置还有数
int cursum=dia[]+dia[];//当前的最小值
minsum=inf;
line.push_back(make_pair(,));
for(i=;i<=n;i++){//指针思想
cursum-=dia[i-];
while(j<n&&cursum<sum){
cursum+=dia[++j];
}
if(cursum>=sum&&cursum<minsum){//update
//这里的cursum>=sum是针对j已经到数组末尾设立的
//j之前如果已经到末尾,i向后移动有可能cursum有可能等于sum,但一定是减少的
line.clear();
line.push_back(make_pair(i,j));
minsum=cursum;
}
else{
if(cursum==minsum){//insert
line.push_back(make_pair(i,j));
}
}
}
for(i=;i<line.size();i++){
printf("%d-%d\n",line[i].first,line[i].second);
}
return ;
}

pat1044. Shopping in Mars (25)的更多相关文章

  1. PAT 甲级 1044 Shopping in Mars (25 分)(滑动窗口,尺取法,也可二分)

    1044 Shopping in Mars (25 分)   Shopping in Mars is quite a different experience. The Mars people pay ...

  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 Advanced 1044 Shopping in Mars (25) [⼆分查找]

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

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

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

  5. 1044. Shopping in Mars (25)

    分析: 考察二分,简单模拟会超时,优化后时间正好,但二分速度快些,注意以下几点: (1):如果一个序列D1 ... Dn,如果我们计算Di到Dj的和, 那么我们可以计算D1到Dj的和sum1,D1到D ...

  6. PAT (Advanced Level) 1044. Shopping in Mars (25)

    双指针. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  7. PAT甲题题解-1044. Shopping in Mars (25)-水题

    n,m然后给出n个数让你求所有存在的区间[l,r],使得a[l]~a[r]的和为m并且按l的大小顺序输出对应区间.如果不存在和为m的区间段,则输出a[l]~a[r]-m最小的区间段方案. 如果两层fo ...

  8. A1044 Shopping in Mars (25 分)

    一.技术总结 可以开始把每个数都直接相加当前这个位置的存放所有数之前相加的结果,这样就是递增的了,把i,j位置数相减就是他们之间数的和. 需要写一个函数用于查找之间的值,如果有就放返回大于等于这个数的 ...

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

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

随机推荐

  1. HTML5与CSS3基础教程(第8版) PDF扫描版​

    <HTML5与CSS3基础教程(第8版)>自第1版至今,一直是讲解HTML和CSS入门知识的经典畅销书,全面系统地阐述HTML5和CSS3基础知识以及实际运用技术,通过大量实例深入浅出地分 ...

  2. 关于C# WinForm_Tree View的一些基本用法(摘抄)

    下面是treeview的用法TreeView组件是由多个类来定义的,TreeView组件是由命名空间"System.Windows .Forms"中的"TreeView& ...

  3. JAVA学习第三篇Boolean

    Boolean是布尔值. 布尔值只有两个值:真和假.写作:true false. 那么如何使用呢? 最简单的用法: public class demo{ public static void main ...

  4. Wet Shark and Bishops(思维)

    Today, Wet Shark is given n bishops on a 1000 by 1000 grid. Both rows and columns of the grid are nu ...

  5. 爬取腾讯课堂IT-互联网分类的的课程信息存入csv文件

    标签(空格分隔): python from urllib.request import urlopen from bs4 import BeautifulSoup #获取IT-互联网分类每页的课程的链 ...

  6. 编译 ambari 2.7.3

    官方给的教程比较简单,需要事先安装的工具也是这里列一点,那里列一点.在此记录一下编译要点(在 centos 7 下). 1. 事先需要安装的工具 yum install -y git svn node ...

  7. springboot添加第三方的jar或本地jar

    原文链接:https://www.cnblogs.com/fengli9998/p/8044923.html 由对接支付引入第三方的jar时冒出的问题,如何在springboot项目中来引入第三方的j ...

  8. CF1100E Andrew and Taxi 二分答案+拓扑排序

    \(\color{#0066ff}{ 题目描述 }\) 给定一个有向图,改变其中某些边的方向,它将成为一个有向无环图. 现在求一个改变边方向的方案,使得所选边边权的最大值最小. \(\color{#0 ...

  9. 平衡树学习笔记(6)-------RBT

    RBT 上一篇:平衡树学习笔记(5)-------SBT RBT是...是一棵恐怖的树 有多恐怖? 平衡树中最快的♂ 不到200ms的优势,连权值线段树都无法匹敌 但是,通过大量百度,发现RBT的代码 ...

  10. SPI 实现原理及运用

    SPI原理 SPI的全名为Service Provider Interface.大多数开发人员可能不熟悉,因为这个是针对厂商或者插件的.在java.util.ServiceLoader的文档里有比较详 ...