BZOJ 3827: [Poi2014]Around the world

Sol

并查集.

一个点所能到达的最远是单调不降的.然后将链延长到两倍,预处理出每个点到达的最远点,然后倒着计算深度.

再然后一直跳,跳到>=x+n的点,因为跳到的点都能到最终的点,并且不影响后面的答案.

Code

/**************************************************************
    Problem: 3827
    User: BeiYu
    Language: C++
    Result: Accepted
    Time:70156 ms
    Memory:20820 kb
****************************************************************/

#include<cstdio>
#include<cstring>
#include<iostream>

using namespace std;

const int N = 1000005;
const int INF = 0x3fffffff;

int n,m,s,lim,ans;
int a[N],nxt[N<<1],d[N<<1];

inline int in(int x=0,char ch=getchar()){ while(ch>‘9‘||ch<‘0‘) ch=getchar();
    while(ch>=‘0‘&&ch<=‘9‘) x=(x<<3)+(x<<1)+ch-‘0‘,ch=getchar();return x; }

int DFS(int x){
    int ff=x;
    for(;ff<x+n;ff=nxt[ff]);
    for(int u=x,v=nxt[x];v!=ff;u=v,v=nxt[v]) nxt[u]=ff;
    return d[x]-d[ff];
}
int main(){
//  freopen("in.in","r",stdin);
    n=in(),s=in(),m=n<<1|1;
    for(int i=1;i<=n;i++) a[i]=in(),lim=max(lim,a[i]);
    for(int i=1,x;i<=s;i++){
        x=in();ans=INF;
        if(x<lim){ puts("NIE");continue; }
        for(int u=1,v=1,t=0;u<=m;u++){
            while(v<m&&t+a[v>n?v-n:v]<=x)
            t+=a[v>n?v-n:v],v++;
            nxt[u]=v,t-=a[u>n?u-n:u];
        }
        for(int i=m;i;i--) d[i]=d[nxt[i]]+1;
//      for(int i=1;i<=m;i++) cout<<nxt[i]<<" ";cout<<endl;
//      for(int i=1;i<=m;i++) cout<<d[i]<<" ";cout<<endl;

        for(int i=1;i<=n;i++) ans=min(ans,DFS(i));
        cout<<ans<<endl;
    }return 0;
}

  

时间: 2024-10-03 03:20:51

BZOJ 3827: [Poi2014]Around the world的相关文章

BZOJ 3524: [Poi2014]Couriers

3524: [Poi2014]Couriers Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 1905  Solved: 691[Submit][Status][Discuss] Description 给一个长度为n的序列a.1≤a[i]≤n.m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2.如果存在,输出这个数,否则输出0. Input 第一行两个数n,m.第二行n个数,a[i].接下来m行,

bzoj 3831: [Poi2014]Little Bird

3831: [Poi2014]Little Bird Description In the Byteotian Line Forest there are   trees in a row. On top of the first one, there is a little bird who would like to fly over to the top of the last tree. Being in fact very little, the bird might lack the

BZOJ 3524: [Poi2014]Couriers( 主席树 )

卡我空间.... 这道题应该是主席树入门题...无修改 , 离散化都不用...出题人业界良心啊 一开始的空白树我 build 出来结果就多了整整 2n 个 Node , 然后就 MLE 了... ( 双倍经验 , 另一道见上图 ) ---------------------------------------------------------------------------------------------- #include<cstdio> #include<cstring&g

bzoj 3832: [Poi2014]Rally(线段树+拓扑排序)

3832: [Poi2014]Rally Time Limit: 20 Sec  Memory Limit: 128 MBSec  Special Judge Submit: 113  Solved: 56 [Submit][Status][Discuss] Description An annual bicycle rally will soon begin in Byteburg. The bikers of Byteburg are natural long distance cyclis

【刷题】BZOJ 3832 [Poi2014]Rally

Description An annual bicycle rally will soon begin in Byteburg. The bikers of Byteburg are natural long distance cyclists. Local representatives of motorcyclists, long feuding the cyclists, have decided to sabotage the event. There are intersections

bzoj 3522: [Poi2014]Hotel

呵呵,一开始天真的我以为求个 西格玛 C(??,3)就好了.. (题解:比枚举2个数的再多一个,,一样搞) 1 #include <bits/stdc++.h> 2 #define LL long long 3 #define lowbit(x) x&(-x) 4 #define inf 1e15 5 using namespace std; 6 inline int ra() 7 { 8 int x=0,f=1; char ch=getchar(); 9 while (ch<'

bzoj 3526 : [Poi2014]Card

把每张卡牌看成两个点,裸的线段树维护连通性. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #define ls x*2,l,mid 6 #define rs x*2+1,mid+1,r 7 #define N 200005 8 using namespace std; 9 int read() 10 { 11 char c=getcha

BZOJ 3834 [Poi2014]Solar Panels 数学

题意: n组询问,每次问smin<=x<=smax, wmin<=y<=wmax时gcd(x, y)的最大值. 解析: md以前有个结论忘了结果这题坑我30分钟+ 我们显然可以找到一个结论. 令gcd(x,y)=z; 那么显然有(smin-1)/z #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace

BZOJ 3834 Poi2014 Solar Panels 数论

题目大意:给定a,b,c,d,多次询问a<=x<=b,c<=x<=d时Gcd(x,y)的最大值ぽい 考虑枚举n=Gcd(x,y),那么[a,b]和[c,d]两个区间内存在n的倍数当且仅当floor(b/n)>floor((a-1)/n)且floor(d/n)>floor((c-1)/n)ぽい 由于后面的式子最多有O(√max(b,d))个取值,因此枚举商就可以了ぽい 1L和2L写了啥ぽい- - #include <cstdio> #include <c