• 「题解」Codeforces 741C Arpa’s overnight party and Mehrdad’s silent entering


    直接无脑随机调整!

    大力钦点 (121212cdots) 分配。

    有限制的之间记录一下,强制改成相反的,此后其中一个改变另一个也要改变。

    这个时候可能不满足相邻三个不能都相同了,把冲突的拉到一个队列里面,每次取出队头随机钦点一个修改,再把修改后新产生的冲突拉到队列里面去。

    复杂度玄学,实测跑的飞快。

    #include<iostream>
    #include<cstdio>
    #include<queue>
    #include<ctime>
    #include<algorithm>
    template <typename T> T Max(T x, T y) { return x > y ? x : y; }
    template <typename T> T Min(T x, T y) { return x < y ? x : y; }
    template <typename T>
    T& read(T& r) {
    	r = 0; bool w = 0; char ch = getchar();
    	while(ch < '0' || ch > '9') w = ch == '-' ? 1 : 0, ch = getchar();
    	while(ch >= '0' && ch <= '9') r = r * 10 + (ch ^ 48), ch = getchar();
    	return r = w ? -r : r;
    }
    const int N = 1000010;
    int n;
    int to[N], pai[N][2];
    int lastpos(int x) { return (x-2+n)%n+1; }
    int nextpos(int x) { return x%n+1; }
    int a[N];
    bool vis[N];
    std::queue<int>q;
    bool chk(int i) {return (a[i] == a[nextpos(i)] && a[nextpos(i)] == a[nextpos(nextpos(i))]); }
    void chk2(int pos) {
    	if(chk(pos)) {
    		if(!vis[pos]) q.push(pos);
    		vis[pos] = 1;
    	}
    	else vis[pos] = 0;
    	if(chk(lastpos(pos))) {
    		if(!vis[lastpos(pos)]) q.push(lastpos(pos));
    		vis[lastpos(pos)] = 1;
    	}
    	else vis[lastpos(pos)] = 0;
    	if(chk(lastpos(lastpos(pos)))) {
    		if(!vis[lastpos(lastpos(pos))]) q.push(lastpos(lastpos(pos)));
    		vis[lastpos(lastpos(pos))] = 1;
    	}
    	else vis[lastpos(lastpos(pos))] = 0;
    	if(chk(nextpos(pos))) {
    		if(!vis[nextpos(pos)]) q.push(nextpos(pos));
    		vis[nextpos(pos)] = 1;
    	}
    	else vis[nextpos(pos)] = 0;
    	if(chk(nextpos(nextpos(pos)))) {
    		if(!vis[nextpos(nextpos(pos))]) q.push(nextpos(nextpos(pos)));
    		vis[nextpos(nextpos(pos))] = 1;
    	}
    	else vis[nextpos(nextpos(pos))] = 0;
    }
    signed main() { //freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);
    	srand(time(NULL)^(unsigned)(* new char));
    	read(n); bool fl = 0; n <<= 1;
    	for(int i = 1; i <= n/2; ++i) {
    		int x, y; read(x); read(y);
    		pai[i][0] = x; pai[i][1] = y;
    		to[x] = y; to[y] = x;
    		if(!(y == (x-2+n)%n+1 || y == x%n+1)) fl = 1;
    	}
    	for(int i = 1; i <= n; ++i) a[i] = i&1;
    	for(int i = 1; i <= n; ++i)
    		if(a[i] == a[to[i]])
    			a[to[i]] = !a[to[i]];
    	for(int i = 1; i <= n; ++i) {
    		vis[i] = chk(i);
    		if(vis[i])
    			q.push(i);
    	}
    	while(!q.empty()) {
    		int x = q.front(); q.pop();
    		if(!chk(x)) continue ;
    		int pos = rand()%2+1;
    		if(pos == 1) pos = x;
    		else if(pos == 2) pos = nextpos(x);
    		else if(pos == 3) pos = nextpos(nextpos(x));
    		a[pos] = !a[pos]; a[to[pos]] = !a[to[pos]];
    		chk2(pos); chk2(to[pos]);
    	}
    	for(int i = 1; i <= n/2; ++i)
    		printf("%d %d
    ", 2-a[pai[i][0]], 2-a[pai[i][1]]);
    	return 0;
    }
    
  • 相关阅读:
    opencv 遍历Mat对象中数据方法-转
    JVM OutOfMemoryError 分析
    hibernate validation HV000030: No validator could be found for constraint
    通过aop实现rpc统一参数校验&异常捕捉
    java8 lambda groupingby 分组保持原来顺序
    递归判断素组是否有序
    dubbo 直连
    Linux 删除openjdk
    telnet命令调用远程dubbo 接口
    git submodule ssh key
  • 原文地址:https://www.cnblogs.com/do-while-true/p/15146276.html
Copyright © 2020-2023  润新知