2015年4月10日 星期五

ZeptoLab Code Rush 2015 E. Transmitting Levels

pop_back那裏特判一堆 也不太知道怎寫好._.


#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;

typedef long long int  int64;

int64 an[2000000];
int rtpos[2000000];
int que[2000000];

int main(){
    int n, q; scanf("%d %d", &n, &q);
    for(int lx = 0;lx < n;lx++){
        scanf("%I64d", an + lx);
        an[lx + n] = an[lx];
    }

    while(q--){
        int64 b; scanf("%I64d", &b);
        int ptrs = 0;
        int64 cnt = 0;
        for(int lx = 0;lx < 2*n;lx++){
            cnt += an[lx];
            while(cnt > b){
                cnt -= an[ptrs];
                ptrs++;
            }
            rtpos[lx] = ptrs;
        }
        //for(int lx = 0;lx < 2*n;lx++)
        //    printf("%d%c", rtpos[lx], lx==2*n-1 ? ('\n'):(' '));
        int ed = 0;
        int ans = 2*n;
        int qs = 0, qe = 0;
        for(int lx = 0;lx < 2*n;lx++){
            while(qe-qs >= 1 and rtpos[lx] == rtpos[que[qe-1]])
                qe--;
            while(qe-qs >= 2 and rtpos[lx] <= que[qe-2])
                qe--;
            if(qe-qs >= 2 and que[qe-2] == rtpos[lx]-1)
                qe--;
            que[qe++] = lx;
            while(qs < qe and lx - rtpos[que[qs]] + 1 >= n){
                ans = min(qe-qs, ans);
                /*printf("get ans:");
                for(int lx = qs;lx < qe;lx++)
                    printf("[%d %d] ", rtpos[que[lx]], que[lx]);
                printf("\n");*/
                qs++;
            }
        }
        printf("%d\n", ans);
    }

    return 0;
}  

沒有留言:

張貼留言