PAT (Advanced Level) 1044. Shopping in Mars (25)
双指针。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std; const int maxn=+;
long long a[maxn];
int n; long long val,Min;
struct Ans
{
int L,R;
Ans(int a,int b)
{
L=a;
R=b;
}
};
vector<Ans>v; int main()
{
Min=;
scanf("%d%lld",&n,&val);
for(int i=;i<=n;i++) scanf("%lld",&a[i]); long long sum=a[];
int p1=,p2=;
while()
{
int fail=;
if(sum>=val)
{
if(sum-val<Min)
{
Min=sum-val;
v.clear();
v.push_back(Ans(p1,p2));
}
else if(sum-val==Min)
v.push_back(Ans(p1,p2));
if(p1<=n)
{
sum=sum-a[p1];
p1++;
fail=;
}
}
else
{
if(p2+<=n){
p2++;
sum=sum+a[p2];
fail=;
}
}
if(fail==) break;
} for(int i=;i<v.size();i++)
{
printf("%d-%d\n",v[i].L,v[i].R);
}
return ;
}
PAT (Advanced Level) 1044. Shopping in Mars (25)的更多相关文章
- PTA(Advanced Level)1044.Shopping in Mars
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 甲级 1044 Shopping in Mars (25 分)(滑动窗口,尺取法,也可二分)
1044 Shopping in Mars (25 分) Shopping in Mars is quite a different experience. The Mars people pay ...
- 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 ...
- 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个数让你求所有存在的区间[l,r],使得a[l]~a[r]的和为m并且按l的大小顺序输出对应区间.如果不存在和为m的区间段,则输出a[l]~a[r]-m最小的区间段方案. 如果两层fo ...
- 1044. Shopping in Mars (25)
分析: 考察二分,简单模拟会超时,优化后时间正好,但二分速度快些,注意以下几点: (1):如果一个序列D1 ... Dn,如果我们计算Di到Dj的和, 那么我们可以计算D1到Dj的和sum1,D1到D ...
- PAT (Advanced Level) 1113. Integer Set Partition (25)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
随机推荐
- cPanel 安装方法
. 如何安装cPanel/WHM? 答:安装过程较长,建议使用screen或nohupscreen: yum -y install screen perl screen -S cpanel cd /h ...
- Chapter 2 Open Book——14
I backpedaled. "They seemed nice enough to me. I just noticed they keptto themselves. 我改口说道,他们看 ...
- js 交换表单中值
<html> <head> <meta charset="utf-8" /> <script type="text/javasc ...
- CSS3秘笈:第十三章
1.float属性能把网页元素移到网页(或者其他外围快)的某一侧.出现在浮动元素之后的所有HTML都能在网页中上移,环绕在浮动元素的周围. float属性接受以下3种不同的值:left(左).righ ...
- Java 环境搭建的一些问题
1.http://www.eclipse.org/webtools/ eclipse 官网,SE.EE方向是两个不同eclipse 2.tomcat 对eclipse来说是一个插件,需要额外下载 T ...
- HAProxy与varnish
Even if HAProxy can do TCP proxying, it is often used in front of web application, exactly where we ...
- shell中exec解析
参考:<linux命令.编辑器与shell编程> <unix环境高级编程> exec和source都属于bash内部命令(builtins commands),在bash下输入 ...
- 如何从Apache官网下载apache
apache服务器官网地址:http://httpd.apache.org/ linux版本下载比较容易,以windows版本,apache 2.4为例. 点击download 此处随便选一个提供商. ...
- 关于在MyEclipse中页面中文乱码的问题
1.首先在Window>preferences>General>Workspace中改为UTF-8. 2.将项目的Properties>Resource改为UTF-8. 3.将 ...
- Unable to chmod /system/build.prop.: Read-only file system
Unable to chmod /system/build.prop.: Read-only file system 只读文件系统 所以需要更改 使用下面的命令 mount -o remount,rw ...