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 ...
随机推荐
- openstack 使用pbr配置,setup.cfg的格式与含义
pbr - Python Build Reasonableness A library for managing setuptools packaging needs in a consistent ...
- virsh 连接虚拟机 (vnc 或 控制台)
第一种方式 1.如果虚拟机登录方式为VNC,在ubuntu机器上安装vncviewer 在虚拟机的配置xml中 <graphics type="vnc" autoport=& ...
- 在swift工程调用第三方库,Bridging导入头文件提示not found解决办法
swift语言简洁,上手之后感觉还不错,今天在使用swift调用cocoapods的AFNetworking时候总是提示“'AFNetworking/AFNetworking.h' file not ...
- HTML DOM--基础概述
DOM: Document Object Model(文档对象模型)的简写,那么,这是一种什么样的模型,简单点来说,就是将文档当成了一棵树.它独立于平台与语言,允许程序与脚本动态地访问.更新文档的结构 ...
- 【算法笔记】B1050 螺旋矩阵
1050 螺旋矩阵 (25 分) 本题要求将给定的 N 个正整数按非递增的顺序,填入“螺旋矩阵”.所谓“螺旋矩阵”,是指从左上角第 1 个格子开始,按顺时针螺旋方向填充.要求矩阵的规模为 m 行 ...
- ZOJ - 3946-Highway Project(最短路变形+优先队列优化)
Edward, the emperor of the Marjar Empire, wants to build some bidirectional highways so that he can ...
- Web前端测试要点
Web前端测试维度: 功能性:功能准确性.兼容性 易用性:易学性.易理解性.易操作性.吸引性 可靠性:成熟性.容错性.稳定性.可恢复性.安全性 可移植性:适应性.易安装性.兼容性 可维护性:易分析性. ...
- 学习python 3 入门知识
1.安装 http://www.runoob.com/python3/python3-install.html https://www.python.org/ 2.使用 工具一:IDLE IDLE 是 ...
- es第十篇:Elasticsearch for Apache Hadoop
es for apache hadoop(elasticsearch-hadoop.jar)允许hadoop作业(mapreduce.hive.pig.cascading.spark)与es交互. A ...
- (转)Linux-HA实战(1)— Heartbeat安装
原文:http://blog.csdn.net/liaomin416100569/article/details/76087448-------centos7源代码编译安装heartbeat 原文:h ...