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 ...
随机推荐
- Chapter 1 First Sight——32
The class seemed to drag on longer than the others. 这堂课看起来比别的课要长. Was it because the day was finally ...
- php mysql数据库 分页与搜索
<?php/** * Created by coder meng. * User: coder meng * Date: 2016/8/29 10:27 */header("Conte ...
- ieee80211_rx
ieee80211rx.c(E:\code\linux\net\ieee80211) 所有接收到的帧都送到这个函数中去 int ieee80211_rx(struct ieee80211_device ...
- 认识ASP.NET MVC的5种AuthorizationFilter
在总体介绍了筛选器及其提供机制(<深入探讨ASP.NET MVC的筛选器>)之后,我们按照执行的先后顺序对四种不同的筛选器进行单独介绍,首先来介绍最先执行的AuthorizationFil ...
- MySQL半同步复制的数据一致性探讨微信后台团队实践【转】
MySQL是一个RDBMS(关系型数据库管理系统),由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品.由于其体积小.速度快.拥有成本低,尤其是开放源码这一特点,广受各大企业欢迎,包括 ...
- php常用命令大全
1.php -v 查看版本号 [root@rs-2 lib]# php -v PHP 5.5.11 (cli) (built: Apr 29 2014 12:35:52) Copyrigh ...
- 树形dp Codeforces Round #364 (Div. 1)B
http://codeforces.com/problemset/problem/700/B 题目大意:给你一棵树,给你k个树上的点对.找到k/2个点对,使它在树上的距离最远.问,最大距离是多少? 思 ...
- Java学习笔记之一
1.对大小写敏感 2.class,类是构建所有Java应用程序和applet的构建块,Java应用程序中的全部内容都必须放置在类中. 3.类名,没有长度限制,必须以字母开头,可以使字母.数字.下划线的 ...
- Android中实现滑动翻页—使用ViewFlipper(dp和px之间进行转换)
Android中实现滑动翻页—使用ViewFlipper(dp和px之间进行转换) Android中dp和px之间进行转换 在xml布局文件中,我们既可以设置px,也可以设置dp(或者dip).一般情 ...
- 转载 Deep learning:六(regularized logistic回归练习)
前言: 在上一讲Deep learning:五(regularized线性回归练习)中已经介绍了regularization项在线性回归问题中的应用,这节主要是练习regularization项在lo ...