Codeforces 526E Transmitting Levels 题解
来源:互联网 发布:mac系统怎么下载word 编辑:程序博客网 时间:2024/06/05 18:06
题意
给一个环状数列,每一次询问最少需要将这个数列分成连续的几部分使得每一个子序列的里的数字和都不超过一个给定的数
思路
首先对序列拆环,就是再复制一份接到尾部,然后从头扫到尾,分段,将段的长度不断放进一个队列,如果长度不小于数列长度就出队至小于,不断记录每一次不小于数列长度时队列的元素数,取最小值,再从尾到头同样做一次,就可以得到答案
代码
#include <cstdio>#include <algorithm>#include <queue>using namespace std;queue<long long> que;long long a[2000001];int main(){ long long n,q,b,s,t,temp,add,len,ans,fi,sum; scanf("%I64d%I64d",&n,&q); for(long long i=0;i<n;i++) scanf("%I64d",&a[i]); for(long long i=n;i<2*n;i++) a[i]=a[i-n]; sum=0; for(long long i=0;i<n;i++) sum+=a[i]; while(q--) { scanf("%I64d",&b); if(sum<=b) { printf("1\n"); continue; } ans=n; temp=0; add=0; len=0; for(long long j=0;j<2*n;j++) { if(temp+a[j]>b) { temp=a[j]; que.push(len); add+=len; fi=-1; while(add>=n) { add-=que.front(); fi=que.front(); que.pop(); } if(fi!=-1&&add+fi>=n&&que.size()+1<ans) ans=que.size()+1; len=1; } else { temp+=a[j]; len++; } } while(!que.empty()) que.pop(); reverse(a,a+2*n); temp=0; add=0; len=0; for(long long j=0;j<2*n;j++) { if(temp+a[j]>b) { temp=a[j]; que.push(len); add+=len; fi=-1; while(add>=n) { add-=que.front(); fi=que.front(); que.pop(); } if(fi!=-1&&add+fi>=n&&que.size()+1<ans) ans=que.size()+1; len=1; } else { temp+=a[j]; len++; } } while(!que.empty()) que.pop(); printf("%I64d\n",ans); } return 0;}
阅读全文
0 0
- Codeforces 526E Transmitting Levels 题解
- codeforces 526 e Transmitting Levels
- codeforces 526E E. Transmitting Levels(dp)
- Codeforces 526E. Transmitting Levels (贪心)
- [斜率优化DP] codeforces 673E. Levels and Regions
- [Codeforces 673E] Levels and Regions (期望+斜率DP)
- Codeforces 165E Compatible Numbers 题解&代码
- Codeforces 7E Defining Macros 题解
- Codeforces 808E Selling Souvenirs 题解
- 【codeforces 733E】Sleep in Class 题解
- Codeforces 567E President and Roads 题解
- Codeforces 5E Bindian Signalizing 题解
- Codeforces #803E: Roma and Pokers 题解
- CodeForces 815E Karen and Neighborhood题解
- CodeForces 817E Choosing The Commander题解
- Codeforces #831E: Cards Sorting 题解
- DP训练 Codeforces 673E Levels and Regions [斜率优化dp][期望]
- codeforces Codeforces Round #407 Div2题解 B,C,D,E
- Hibernate 缓存机制
- vm
- CentOS7.2设置本地yum源
- 深度学习实战中遇到的问题
- Python入门介绍
- Codeforces 526E Transmitting Levels 题解
- Python的socket模块详解
- Mysql几种索引类型的区别及适用情况
- 淘淘商城系列——中文分析器IK-Analyzer的使用
- 移动APP性能测评与优化读书笔记-1.1 内存分析
- 容器常用操作
- 6-JavaScript设计模式——工厂模式
- iOS开发 添加事件到系统日历
- 在Centos安装zookeeeper