/*
标称并没有用到题解中提到的那种奇妙的性质,我们可以证明,正常从1开始走的话,需要T次,如何使这个次数减小?题解中提到一个办法,有一步小于n/t,我们考虑这一步,如果把它匀到左右两步中,则可以减小,就根据这个性质来优化
next函数的部分,我当时用了一个倍增法,题解用了一个并查集,倍增比较直观,然而并查集更为巧妙
*/
//my code
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#define ll long long
using namespace std;
const int maxn = ;
int n,q,cnt,ct[maxn];
ll a[maxn],sum[maxn],ans;
ll read(){
char ch=getchar();
ll x=,f=;
while(!(ch>=''&&ch<='')){if(ch=='-')f=-;ch=getchar();};
while(ch>=''&&ch<=''){x=x*+(ch-'');ch=getchar();};
return x*f;
}
ll greedy(int u,ll t){
ll tot = ,tmp = ;
ll j,lg,tst,nt;
for(int i = ;i <= n;i++){
tmp++;
if(a[u] + a[u+] > t || i == n){ }else{
lg = ;
j = ;
tot = ;
while(lg>=){
if(u+j<=n){
tst = sum[u+j]-sum[u-];
nt = u + j;
}
else{
tst = sum[n] - sum[u-] + sum[u+j-n];
nt = u + j - n;
}
if(tot + tst <= t){
i += j;
tot += tst - a[nt];
u = nt;
lg++;
j <<= ;
}else{
lg--;
j >>= ;
}
}
}
u++;
if(u > n) u = ;
if(tmp >= ans) break;
}
return tmp;
}
void work(ll t){
cnt = ;
ans = maxn;
ct[++cnt] = ;
ll sum = a[];
for(int i = n;i > ;i--){
sum += a[i];
if(sum > t) break;
ct[++cnt] = i;
}
for(int i = ;i <= cnt;i++){
ans = min(ans,greedy(ct[i],t));
}
cout<<ans<<endl;
}
int main(){
freopen("stall.in","r",stdin);
freopen("stall.out","w",stdout);
n = read();
q = read();
for(int i = ;i <= n;i++){
a[i] = read();
sum[i] = sum[i-] + a[i];
}
ll b;
for(int i = ;i <= q;i++){
b = read();
work(b);
}
return ;
}
//std
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cstdlib>
#include<string>
#include<bitset>
#define INF 1000000000
#define N 2000005
#define fi first
#define se second
#define debug(x) cout<<#x<<"="<<x<<endl
#define MP(x,y) make_pair(x,y)
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
LL a[N],s[N];
int to[N],fa[N],d[N];
int findr(int x)
{
if(fa[x]==x)
return x;
else
{
int t=fa[x];
fa[x]=findr(fa[x]);
d[x]=d[x]+d[t];
return fa[x];
}
} void Union(int x,int y)
{
fa[x]=y;
d[x]=;
} int main()
{
int size = << ; // 256MB
char *p = (char*)malloc(size) + size;
__asm__("movl %0, %%esp\n" :: "r"(p));
freopen("stall.in","r",stdin);
freopen("stall.out","w",stdout);
cin>>n>>q;
for(i=;i<=n;i++)
scanf("%I64d",&a[i]),a[i+n]=a[i];
for(i=n*;i;i--)
s[i]=s[i+]+a[i];
while(q--)
{
ans=INF;
cin>>m;
memset(d,,sizeof(d));
for(i=;i<=n;i++)
fa[i]=i,fa[i+n]=i+n;
for(j=n*,i=n*;i;i--)
{
while(s[i]-s[j+]>m) j--;
to[i]=j+;
//debug(to[i]);
}
//if(q==1) return 0;
for(i=;i<=n;i++)
{
j=findr(i);
while(to[j]<i+n)
{
k=to[j];
Union(j,k);
j=findr(k);
}
findr(i);
ans=min(ans,d[i]);
}
cout<<ans+<<endl;
}
return ;
}
// davidlee1999WTK 2015/
// srO myk Orz
//ios::sync_with_stdio(false);

繁华模拟赛day8 牛栏的更多相关文章

  1. 繁华模拟赛day8 字典序

    /* 这个题要我们求一个字典序,字符串给出的顺序,会对字母的字典序前后相对顺序进行限定,如何用来表示这种限定,我们注意到这种一个之后接着一个,只有先输出他前面的才能输出他,很明显就是拓扑排序,最小方案 ...

  2. 繁华模拟赛day8 科技树

    /* 贪心,很明显是越容易升级的越先升级 */ #include<iostream> #include<cstdio> #include<string> #incl ...

  3. [繁华模拟赛]Evensgn 剪树枝

    Evensgn 剪树枝 题目 繁华中学有一棵苹果树.苹果树有 n 个节点(也就是苹果),n − 1 条边(也就 是树枝).调皮的 Evensgn 爬到苹果树上.他发现这棵苹果树上的苹果有两种:一 种是 ...

  4. 繁华模拟赛 Vicent坐电梯

    /*n<=5000­这样就不能用O(n)的转移了,而是要用O(1)的转移.­注意我们每次的转移都来自一个连续的区间,而且我们是求和­区间求和?­前缀和!­令sum[step][i]表示f[ste ...

  5. 繁华模拟赛 Vincent的城堡

    #include<iostream> #include<cstdio> #include<string> #include<cstring> #incl ...

  6. 繁华模拟赛 vicent的字符串

    #include<iostream> #include<cstdio> #include<string> #include<cstring> #incl ...

  7. 繁华模拟赛 Evensgn剪树枝

    #include<iostream> #include<cstdio> #include<string> #include<cstring> #incl ...

  8. 繁华模拟赛 Evensgn玩序列

    #include<iostream> #include<cstdio> #include<string> #include<cstring> #incl ...

  9. 繁华模拟赛 Evensgn的债务

    #include<iostream> #include<cstdio> #include<string> #include<cstring> #incl ...

随机推荐

  1. UVa1636 Headshot

    数据结构学得心累,做点小题换心情 原题是PDF格式查看的,贴过来好麻烦,果断放弃 已知前一次尝试结果是0,则可以得知: 下一次若仍是0,则遇到了一个00串 下一次若是1,则遇到了一个01串 SHOOT ...

  2. Mac系统搭建Go语言Sublime Text 2环境配置

    Go语言是谷歌自家的编译型语言,旨在不损失性能的前提下降低代码复杂率.其优势是让软件充分发挥多核心处理器同步多工的优点,并可解决面向对象程序设计的麻烦. 一.安装Golang的SDK 在官网http: ...

  3. BZOJ2599 [IOI2011]Race

    传送门 点分治,黄学长的选根方法会T掉,换了这个人的选根方法就可以了. 当然,你也可以选择黄学长的奇淫优化 //BZOJ 2599 //by Cydiater //2016.9.23 #include ...

  4. tomcat密码的坑

    <role rolename="tomcat"/> <role rolename="role1"/> <user username ...

  5. JavaScript DES 加密tripledes.js:

    <html> <head> <meta http-equiv="content-type" content="text/html; char ...

  6. 如何用iframe标签以及Javascript制作时钟?

    如何利用iframe标签以及Javascript制作时钟? 如何制作一个时钟呢?效果如下图所示: 这里的时钟会不停的走,但是下面的页面是不会变得,这样就实现了我们想要的效果了.下面我将分为以下几个方面 ...

  7. php发送http请求方法实例及详解

    http请求有get,post. php发送http请求有三种方式[我所知道的有三种,有其他的告诉我]. file_get_contents();详情见:http://www.whosmall.com ...

  8. CSS3定位和浮动详解

    本文为大家分享CSS3定位和浮动的基础概念,与使用方法,供大家参考,具体内容如下 一.定位 1. css定位: 改变元素在页面上的位置 2. css定位机制: 普通流: 浮动: 绝对布局: 3. cs ...

  9. eclipse中SSH三大框架环境搭建<一>

    这里先简单介绍一下我用的三大框架版本以及下载地址 相关链接:eclipse中SSH三大框架环境搭建<二> eclipse中SSH三大框架环境搭建<三> struts-2.3.3 ...

  10. 转:netflix推荐系统竞赛

    原文链接:Netflix recommendations: beyond the 5 stars (Part 1), (Part 2) 原文作者:Xavier Amatriain and Justin ...