CodeForces 430A Points and Segments (easy)(构造)题解
题意:之前愣是没看懂题意...就是给你n个点的坐标xi,然后还规定了Li,Ri,要求给每个点染色,每一组L,R内的点红色和黑色的个数不能相差大于1个,问你能不能染成功,不能输出-1,能就按照输入的顺序输出颜色
思路:我会说我根本没想到怎么做吗?显然只要交替染色相差就不会大于1
代码:
#include<set> #include<map> #include<stack> #include<cmath> #include<queue> #include<vector> #include<string> #include<cstdio> #include<cstring> #include<sstream> #include<iostream> #include<algorithm> typedef long long ll; using namespace std; const int maxn = 100 + 10; const int MOD = 1e9 + 7; const int INF = 0x3f3f3f3f; struct node{ int id, col, i; bool operator < (const node x) const{ return id < x.id; } }a[maxn]; bool cmp(node x, node y){ return x.i < y.i; } int main(){ int n, m, c; scanf("%d%d", &n, &m); for(int i = 1; i <= n; i++) scanf("%d", &a[i].id), a[i].i = i; for(int i = 1; i <= m; i++) scanf("%d%d", &c, &c); sort(a + 1, a + n + 1); for(int i = 1; i <= n; i++) a[i].col = i & 1; sort(a + 1, a + n + 1, cmp); for(int i = 1; i <= n; i++){ if(i != 1) printf(" "); printf("%d", a[i].col); } printf("\n"); return 0; }