NC14302. Confliction
描述
输入描述
The first line is the number of test cases.
For each test case, the first is an integer NAlice(NAlice ≤100000), donating the length of the instructions of Alice.
The next NAlice lines describe Alice`s instructions. Each line consists of two integer c, t. c could be -1, 0, 1, donating moving forward, staying, and moving backward respectively. t is a non-negative integer donating that the instruction c would be executed t times, in the next t clock turns.
The next line is an integer NBob (NBob ≤ 100000), donating the length of the instructions of Bob.
The next NBob lines describe Bob`s instructions. Each line consists of two integer c, t. c could be -1, 0, 1, donating moving forward, staying, and moving backward respectively. t is a non-negative integer donating that the instruction c would be executed t times, in the next t clock turns.
Suppose LAlice equals the sum of all t in Alice's program, and LBob equals the sum of all t in Bob's program.
It is guaranteed LAlice = LBob and LAlice, LBob ≤ 1018.
输出描述
For each test case, output a line containing the maximum conflictions.
示例1
输入:
2 1 1 2 2 1 1 -1 1 1 0 6 4 -1 2 1 1 -1 2 1 1
输出:
2 3
C++11(clang++ 3.9) 解法, 执行用时: 947ms, 内存消耗: 12032K, 提交时间: 2017-11-11 18:04:33
#include<cstdio> #include<algorithm> using namespace std; #define LL long long struct Node{ LL l,t; int dir,flag; bool operator < (const Node& c)const{ return l<c.l||(l==c.l&&flag==1&&c.flag==-1); } }c[400005]; LL a[100001],b[100001]; int da[100001],db[100001]; int main() { int test,n,m; scanf("%d",&test); while(test--){ scanf("%d",&n); for(int i=0;i<n;i++) scanf("%d%lld",&da[i],&a[i]); da[n]=0;a[n]=1; scanf("%d",&m); for(int i=0;i<m;i++) scanf("%d%lld",&db[i],&b[i]); db[m]=0;b[m]=1; LL pos=0; int cnt=0; for(int i=0,j=0;i<=n&&j<=m;){ LL tmp=min(a[i],b[j]); int dir=da[i]-db[j]; LL d=pos+dir*tmp; LL l=min(pos,d-dir); c[cnt++]={l,tmp,abs(dir),1}; c[cnt++]={l+(tmp-1)*abs(dir),tmp,abs(dir),-1}; a[i]-=tmp;b[j]-=tmp; if(!a[i])i++; if(!b[j])j++; pos=d; } sort(c,c+cnt); LL ans=0,num[2]={0,0}; for(int i=0;i<cnt;i++){ if(c[i].dir==0)num[c[i].l&1]+=c[i].t*c[i].flag; else if(c[i].dir==1){num[0]+=c[i].flag;num[1]+=c[i].flag;} else if(c[i].dir==2)num[c[i].l&1]+=c[i].flag; ans=max(ans,num[c[i].l&1]); if(i+1<cnt&&c[i].l+1<c[i+1].l)ans=max(ans,num[~c[i].l&1]); } printf("%lld\n",ans); } }