【PAT甲级】1044 Shopping in Mars (25 分)(前缀和,双指针)
题意:
输入一个正整数N和M(N<=1e5,M<=1e8),接下来输入N个正整数(<=1e3),按照升序输出"i-j",i~j的和等于M或者是最小的大于M的数段。
AAAAAccepted code:
#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
int a[];
int sum[];
vector<pair<int,int> >ans;
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,m;
cin>>n>>m;
for(int i=;i<=n;++i){
cin>>a[i];
sum[i]=sum[i-]+a[i];
}
int l=,t=,mn=1e9;
for(int i=;i<=n;++i){
t-=a[i];
while(t<m&&l<=n)
t+=a[l++];
if(t>=m&&t<mn){
mn=t;
ans.clear();
ans.push_back({i+,l-});
}
else if(t==mn)
ans.push_back({i+,l-});
}
cout<<ans[].first<<"-"<<ans[].second;
for(int i=;i<ans.size();++i)
cout<<"\n"<<ans[i].first<<"-"<<ans[i].second;
return ;
}
【PAT甲级】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 ...
- 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 ...
- PAT 甲级 1044 Shopping in Mars
https://pintia.cn/problem-sets/994805342720868352/problems/994805439202443264 Shopping in Mars is qu ...
- 1044 Shopping in Mars (25 分)
Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diam ...
- PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)
1040 Longest Symmetric String (25 分) Given a string, you are supposed to output the length of the ...
- PAT 甲级 1083 List Grades (25 分)
1083 List Grades (25 分) Given a list of N student records with name, ID and grade. You are supposed ...
- PAT甲级——1130 Infix Expression (25 分)
1130 Infix Expression (25 分)(找规律.中序遍历) 我是先在CSDN上面发表的这篇文章https://blog.csdn.net/weixin_44385565/articl ...
- PAT 甲级 1074 Reversing Linked List (25 分)(链表部分逆置,结合使用双端队列和栈,其实使用vector更简单呐)
1074 Reversing Linked List (25 分) Given a constant K and a singly linked list L, you are supposed ...
随机推荐
- Whctf - OLDDRIVER - Writeup
Whctf - OLDDRIVER - Writeup 转载请标明出处http://www.cnblogs.com/WangAoBo/p/7541536.html 题目: 分析: 给了10组RSA的加 ...
- 出现 HTTP Status 500 - Servlet.init() for servlet springmvc threw exception 异常
出现这种异常在网上搜了搜 ,大多数都是说jdk和tomcat版本的问题:而我前几天都是运行得好好的,今天就编写了代码一运行项目发现报了这个错误.后台仔细看了看错误信息.结果是在你的项目中有相同的req ...
- c#修改项目名称
1.修改解决方案名称 右键,重命名 2.修改项目名称 右键,重命名 3.修改程序集名称和默认命名空间 项目,属性 4.替换解决方案中的名称 编辑,替换,替换范围默认整个解决方案 5.用记事本打开.sl ...
- opencv:边缘提取
#include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace st ...
- Docker - dockerhub 相关命令
1. 概述 水一发 docker 命令行里 dockerhub 相关的命令 2. 准备 有一个 dockerhub 的账号 ref dockerhub 3. 命令 登录 docker login 概述 ...
- Day0 认识Java与变量类型
字节码与虚拟机 Java介于编译型语言和解释型语言之间.编译型语言如C.C++,代码是直接编译成机器码执行,但是不同的平台(x86.ARM等)CPU的指令集不同,因此,需要编译出每一种平台的对应机器码 ...
- 离线安装paramiko以及pysftp linux,windows
离线安装 paramiko python37 pip download -d ./dependency -r requirements.txt pip install --no-index --ign ...
- 利用ZotFile对Zotero中的文献进行整理
1.安装ZotFile插件 *** 以后补充 *** 2.配置ZotFile 配置 3.整理操作 (1)将文件拖进Zotero软件相应的目录(自己创建) (2)查看文件位置 (未整理之前) (3)整理 ...
- [Jenkins] TestComplete 使用Jenkins进行持续集成测试
1.安装正确的TestComplete插件 在Jenkins里面搜索TestComplete,找到正确的插件然后安装,可以重启jenkins或者选择不重启 在Install Tab 下面可以查看到正确 ...
- HTML连载59-子绝父相
一.子绝父相 1.只使用相对定位,对图片的位置进行精准定位. <!DOCTYPE html> <html lang="en"> <head> & ...