HDU 6232 Confliction

Problem Description
Recently, Alice and Bob are working on a resource-sharing computation model. In this model, there are two processing units, and a memory space which could be represented as unlimited linear grids  ,A3,A2,A1,A0,A1,A2,A3, . Each processing units has a pointer to mark exactly one grid in the memory. In each clock turn, the pointer would stay at the current grid, move the pointer one grid forward, or move the pointer one grid backward. In their work, Alice and Bob would submit their codes, and their programs would start at the same time. Initially, both pointers would be located at a random grid, and move according to a set of instructions. If both pointers are at the same grid at the same time, the confliction counter would plus one and record it(If their initial grids are the same, the counter would still record it). Now it is your job to find the maximum conflictions the counter could record.
 

Input
The first line is the number of test cases.
For each test case, the first is an integer  NAlice(NAlice100000) , 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(NBob100000) , 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,LBob1018 .
 

Output
For each test case, output a line containing the maximum conflictions.
 

Sample Input
  
  
2 1 1 2 2 1 1 -1 1 1 0 6 4 -1 2 1 1 -1 2 1 1
 

Sample Output
  
  
2 3
#include<cstdio>
#include<algorithm>
using namespace std;
const int N = 6e5 + 10;
int T, n, m, sz;
long long ca[N], ta[N], cb[N], tb[N];

struct point {
	long long r, add;
	long long odd, even;
	point() {}
	point(long long r, long long odd, long long even, long long add) :r(r), odd(odd), even(even), add(add) {}
	bool operator<(const point a) {
		return r == a.r ? add > a.add : r < a.r;
	}
}g[N];

int main() {
	for (scanf("%d", &T); T--;) {
		scanf("%d", &n);
		for (int i = 1; i <= n; i++) {
			scanf("%lld%lld", &ca[i], &ta[i]);
		}
		scanf("%d", &m);
		for (int i = 1; i <= m; i++) {
			scanf("%lld%lld", &cb[i], &tb[i]);
		}
		sz = 0;
		g[sz++] = point(0, 1, 1, 1);
		g[sz++] = point(1, 1, 1, -1);
		for (long long c = 0, i = 1, j = 1; i <= n && j <= m;) {
			long long tc = min(ta[i], tb[j]);

			long long l = c, r = c;
			if (ca[i] == cb[j]) {
				int flag = l % 2 ? 1 : 0;
				g[sz++] = point(l, flag, !flag, tc);
				g[sz++] = point(r + 1, flag, !flag, -tc);
			}
			else {
				if (ca[i] == 0) {
					if (cb[j] == 1) l -= tc, c -= tc, r--;
					else r += tc, c += tc, l++;

					int flag = l % 2 ? 1 : 0;
					g[sz++] = point(l, flag, !flag, 1);
					g[sz++] = point(l + 1, !flag, flag, 1);
					g[sz++] = point(r + 1, 1, 1, -1);
				}
				else if (cb[j] == 0) {
					if (ca[i] == 1) r += tc, c += tc, l++;
					else l -= tc, c -= tc, r--;

					int flag = l % 2 ? 1 : 0;
					g[sz++] = point(l, flag, !flag, 1);
					g[sz++] = point(l + 1, !flag, flag, 1);
					g[sz++] = point(r + 1, 1, 1, -1);
				}
				else {
					if (ca[i] == 1) r += tc + tc, c += tc + tc, l += 2;
					else l -= tc + tc, c -= tc + tc, r -= 2;
					int flag = l % 2 ? 1 : 0;
					g[sz++] = point(l, flag,  !flag, 1);
					g[sz++] = point(r + 1, flag, !flag, -1);//.
				}
			}

			if (!(ta[i] -= tc)) i++; 
			if (!(tb[j] -= tc)) j++;
		}
		sort(g, g + sz);
		long long odd = 0, even = 0, ans = 0;
		for (int i = 0, j = 0; i < sz; i = j) {
			for (j = i; j < sz && g[i].r == g[j].r; j++) {
				odd += g[j].odd * g[j].add;
				even += g[j].even * g[j].add;
			}
			ans = max(ans, max(odd, even));
		}
		printf("%lld\n", ans);
	}
	return 0;
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值