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 (≤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
注意点:1.注意边界值n+1;
2.注意下标j和j+1;
#include<bits/stdc++.h>
using namespace std; const int maxn=;
const int inf=0x3f3f3f3f; int sum[maxn]; int n,S,nearS=inf; int upper_bound(int low,int high,int x){
int left=low,right=high,mid; if(sum[high]<=x)
return high+; while(left<right){
mid=(left+right)/; if(sum[mid]>x){
right = mid;
}else{
left = mid+;
}
} return left;
} int main(){
cin>>n>>S; for(int i=;i<=n;i++){
cin>>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(sum[j]-sum[i-]>S&&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)
cout<<i<<"-"<<j-<<endl;
} return ; }
1044 Shopping in Mars (25 分)的更多相关文章
- PAT 甲级 1044 Shopping in Mars (25 分)(滑动窗口,尺取法,也可二分)
1044 Shopping in Mars (25 分) Shopping in Mars is quite a different experience. The Mars people pay ...
- 1044 Shopping in Mars (25分)(二分查找)
Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diam ...
- PAT Advanced 1044 Shopping in Mars (25) [⼆分查找]
题目 Shopping in Mars is quite a diferent experience. The Mars people pay by chained diamonds. Each di ...
- 【PAT甲级】1044 Shopping in Mars (25 分)(前缀和,双指针)
题意: 输入一个正整数N和M(N<=1e5,M<=1e8),接下来输入N个正整数(<=1e3),按照升序输出"i-j",i~j的和等于M或者是最小的大于M的数段. ...
- 1044. Shopping in Mars (25)
分析: 考察二分,简单模拟会超时,优化后时间正好,但二分速度快些,注意以下几点: (1):如果一个序列D1 ... Dn,如果我们计算Di到Dj的和, 那么我们可以计算D1到Dj的和sum1,D1到D ...
- PAT (Advanced Level) 1044. Shopping in Mars (25)
双指针. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- PAT甲题题解-1044. Shopping in Mars (25)-水题
n,m然后给出n个数让你求所有存在的区间[l,r],使得a[l]~a[r]的和为m并且按l的大小顺序输出对应区间.如果不存在和为m的区间段,则输出a[l]~a[r]-m最小的区间段方案. 如果两层fo ...
- A1044 Shopping in Mars (25 分)
一.技术总结 可以开始把每个数都直接相加当前这个位置的存放所有数之前相加的结果,这样就是递增的了,把i,j位置数相减就是他们之间数的和. 需要写一个函数用于查找之间的值,如果有就放返回大于等于这个数的 ...
- pat1044. Shopping in Mars (25)
1044. Shopping in Mars (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Shop ...
随机推荐
- java中子类继承父类程序执行顺序
java中子类继承父类程序执行顺序 FatherTest.java public class FatherTest { private String name; public FatherTest() ...
- Linux打开关闭ping
#关闭 ” >/proc/sys/net/ipv4/icmp_echo_ignore_all #打开 ” >/proc/sys/net/ipv4/icmp_echo_ignore_all
- Eclipse转idea改设置
1 自动导包:画圈的打钩,实现自动导包,去除无用包.导入的类名相同时需要自己手动导包-> alt+enter. 2:修改快捷键 左移光标,右移同理. 上移光标:下移同理 光标移至行首,行末为e ...
- python学习笔记:json与字典的转换(dump 和dumps load和loads的区别)
1. json序列化(字典转成字符串)方法: dumps:无文件操作 dump:序列化+写入文件 2. json反序列化(字符串转成字典)方法: loads:无文件操作 ...
- Spring Data JPA查询关联数据
1. Query方式@Query("select s from Store s join fetch s.products where s.user.id = :user_id") ...
- Linux部分常用命令详解(二)
date 命令详解 date命令可以按照指定格式显示日期,只键入date则以默认格式显示当前时间 例如: 如果需要以指定的格式显示日期,可以使用“+”开头的字符串指定其格式,详细格式如下: %n : ...
- 微信小程序の小程序事件流
一.什么是事件? 事件是视图层到逻辑层的通讯方式:事件可以将用户的行为,反馈到逻辑层进行处理:事件可以绑定在组件上,触发事件后,就会执行逻辑层中对应的事件处理函数:事件对象可以携带额外信息. 二.事件 ...
- spark on yarn提交任务时报ClosedChannelException解决方案
spark2.1出来了,想玩玩就搭了个原生的apache集群,但在standalone模式下没有任何问题,基于apache hadoop 2.7.3使用spark on yarn一直报这个错.(Jav ...
- element-UI select 踩过的坑和解决办法
今天遇到了一个bug,就是在使用element-UI的select框时,当选中值,会触发@change事件,下拉框消失,但是这时候select框还是处于获取焦点状态.可以看到select框还是处于颜色 ...
- 洛谷P3979 遥远的国度 树链剖分+分类讨论
题意:给出一棵树,这棵树每个点有权值,然后有3种操作.操作一:修改树根为rt,操作二:修改u到v路径上点权值为w,操作三:询问以rt为根x子树的最小权值. 解法:如果没有修改树根操作那么这题就是树链剖 ...