PAT 1044. Shopping in Mars
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <climits>
#include <algorithm>
using namespace std; int main() {
int N, M;
scanf("%d%d", &N, &M);
vector<int> nums(N); vector<pair<int, int> > cuts; for (int i=; i<N; i++) {
scanf("%d", &nums[i]);
}
int p = ;
int q = ; int csum = nums[];
int lsum = INT_MAX; while (p<=q && q < N) {
if (csum < M) {
if (q+ >= N) {
break;
}
csum += nums[++q];
} else if (csum == M) {
if (lsum > csum) {
cuts.clear();
}
lsum = csum;
cuts.push_back(make_pair(p, q));
if (q+ >= N) {
break;
}
csum -= nums[p++];
csum += nums[++q];
} else {
if (csum < lsum) {
cuts.clear();
lsum = csum;
cuts.push_back(make_pair(p, q));
} else if (csum == lsum) {
cuts.push_back(make_pair(p, q));
}
if (p == q && (q + < N)) {
csum += nums[++q];
}
csum -= nums[p++];
}
}
sort(cuts.begin(), cuts.end());
int len = cuts.size(); for (int i=; i<len; i++) {
printf("%d-%d\n", cuts[i].first + , cuts[i].second + );
}
return ;
}
一根爬虫
PAT 1044. Shopping in Mars的更多相关文章
- PAT 1044 Shopping in Mars[二分][难]
1044 Shopping in Mars(25 分) Shopping in Mars is quite a different experience. The Mars people pay by ...
- 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 分)
1044 Shopping in Mars (25 分) Shopping in Mars is quite a different experience. The Mars people pay b ...
- PAT 甲级 1044 Shopping in Mars
https://pintia.cn/problem-sets/994805342720868352/problems/994805439202443264 Shopping in Mars is qu ...
- 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
Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diam ...
- PTA(Advanced Level)1044.Shopping in Mars
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 ...
- 1044 Shopping in Mars (25分)(二分查找)
Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diam ...
随机推荐
- excel的公式:查找匹配某个值的单元格--MATCH()
这个非常简单,没啥说的,D1位置是显示这个值所在的行数,如果是跨表,就在匹配区域前面加上 sheetName!
- nginx高性能WEB服务器系列之四配置文件详解
nginx系列友情链接:nginx高性能WEB服务器系列之一简介及安装https://www.cnblogs.com/maxtgood/p/9597596.htmlnginx高性能WEB服务器系列之二 ...
- linux入门 配置网络
我因为有自己的ssr,因此想让虚拟机共享我的ssr,并且不想搞的太麻烦,特此记录一下 1. 获取本机的ip 在命令行里输入ipconfig /all 获取本机IP,如下图所示,我本机ip为10.3.7 ...
- mocha测试框架
中文翻译文档:https://www.jianshu.com/p/9c78548caffa 阮一峰:http://www.ruanyifeng.com/blog/2015/12/a-mocha-tut ...
- 求两个数a、b的最大公约数
//求两个数a.b的最大公约数 function gcd(a,b){ return b===0?a:gcd(b,a%b) }
- apache的应用(发布目录,黑白名单,虚拟主机,PHP-cgi支持,正向代理,https加密,)
[root@apache1 ~]# yum install httpd -y [root@apache1 ~]# cd /var/www/html/ 进入默认发布目录 [root@apache1 ...
- vue-cli 启动过项目步骤
一. 安装 node.js 安装完成后,可以命令行工具中输入 node -v 和 npm -v,如果能显示出版本号,就说明安装成功. 二.安装webpack npm install webpack - ...
- J15W-J45W铜质截止阀厂家,J15W-J45W铜质截止阀价格 - 专题栏目 - 无极资讯网
无极资讯网 首页 最新资讯 最新图集 最新标签 搜索 J15W-J45W铜质截止阀 无极资讯网精心为您挑选了(J15W-J45W铜质截止阀)信息,其中包含了(J15W-J45W铜质截止阀)厂家,( ...
- URL中参数为数组
今天写代码时候碰到了一个需要在URL中传递数组类型的参数,记录一下. var urlstr = "http://test"; var test = new Array(); for ...
- ocr 资源
1. PIL 在图片上添加中文 https://blog.csdn.net/m0_37606112/article/details/78511381 2 .Chinese Text in the Wi ...