/*
标称并没有用到题解中提到的那种奇妙的性质,我们可以证明,正常从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. PHP设计模式(三)

    注册器模式 这种模式比较简单好理解,在PHP框架中会经常用到,在某些比较大的PHP框架中,会在初始化时将一些常用的类实例放在注册器中,实际是存在注册器类中的一个静态数组中,以后想去用它的话,直接根据名 ...

  2. Oracle实例、用户、权限和角色

    1.数据库的实例:数据库创建后会有一系列为该数据库提供服务的内存空间和后天进程,称为该数据库的实例.每一个数据库至少会有一个实例为其服务.实例中的内存结构称为系统全局区(SGA),系统会根据当前计算机 ...

  3. HDU 1425 sort(堆排序/快排/最大堆/最小堆)

    传送门 Description 给你n个整数,请按从大到小的顺序输出其中前m大的数. Input 每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行包含n个各不 ...

  4. Pythonn new-style class and old-style class

    In [1]: class old(): ...: a = 1 ...: In [2]: o = old() In [3]: o.__class__ Out[3]: <class __main_ ...

  5. BZOJ1031: [JSOI2007]字符加密Cipher

    传送门 后缀数组模板题 //BZOJ 1031 //by Cydiater //2016.9.21 #include <iostream> #include <cstring> ...

  6. Install latest R for ubuntu

    ### delete old version rm -rf /usr/local/lib/R /usr/lib/R ~/**/R sudo apt-get autoremove rstudio sud ...

  7. gnuplot使用1

    安装之后,迫切需要运行一个程序来看看,首先要找到软件默认的使用路径: 输入 show loadpath命令就会显示默认查找的几个路径, loadpath is loadpath from GNUPLO ...

  8. window.location.href的用法

    在写ASP.Net程序的时候,我们经常遇到跳转页面的问题,我们经常使用Response.Redirect 做ASP.NET框架页跳转,如果客户要在跳转的时候使用提示,这个就不灵光了,如: Respon ...

  9. Objective-C学习笔记之block

    //定义一个函数,传入block类型参数myBlock5 int fun(int (^myBlock5)(int a,int b)) { return myBlock5(10,20); } int ( ...

  10. easyUI创建dialog弹框

    1.在当前页面必须有一个DIV <!-- 保证金明细的详情列表显示 --> <div id="dialog-alarm-detail"></div&g ...