2014年11月7日 星期五

JAG pI - Color the Map Extreme

亂做一通
35個點就亂著色啦XD
(幾何part由西瓜提供XD)

  1. #include<cstdio>
  2. #include<cstdlib>
  3. #include<algorithm>
  4. #include<cstring>
  5. #include<vector>
  6. #include<utility>
  7. #include<map>
  8. #include<set>
  9. #include<queue>
  10. #include<cmath>
  11. #define X first
  12. #define Y second
  13. using namespace std;
  14. typedef long long int int64;
  15. typedef pair<int,int> pii;
  16. pii operator-(const pii &a, const pii &b){
  17. return pii(a.X - b.X, a.Y - b.Y);
  18. }
  19.  
  20. inline int dot(pii p1, pii p2){
  21. return p1.X * p2.X + p1.Y * p2.Y;
  22. }
  23. inline int cross(pii p1, pii p2){
  24. return p1.X * p2.Y - p2.X * p1.Y;
  25. }
  26.  
  27. bool check(pii p1, pii p2, pii p3, pii p4){
  28. if((p1 == p3) and (p2 == p4)) return true;
  29. if((p1 == p4) and (p2 == p3)) return true;
  30. if ((p2.Y - p1.Y) * (p4.X - p3.X) != (p4.Y - p3.Y) * (p2.X - p1.X)) return false;
  31. if (cross(p2 - p1, p3 - p1)) return false;
  32. if (dot(p1 - p3, p1 - p4) < 0 || dot(p2 - p3, p2 - p4) < 0 ||
  33. dot(p3 - p1, p3 - p2) < 0 || dot(p4 - p1, p4 - p2) < 0)
  34. return true;
  35. return false;
  36. }
  37. vector<int> to[100];
  38. int col[100];
  39. void color(int prc){
  40. //printf("draw at %d\n", prc);
  41. int ctab[5] = {0};
  42. for(int lx = 0;lx < to[prc].size();lx++){
  43. int pind = to[prc][lx];
  44. if(col[pind] != -1)
  45. ctab[col[pind]] = 1;
  46. }
  47. int get_color = 4;
  48. for(int lx = 1;lx <= 4;lx++){
  49. if(ctab[lx] == 0){
  50. get_color = lx;
  51. break;
  52. }
  53. }
  54. col[prc] = get_color;
  55. //printf("draw %d\n", get_color);
  56. int a[100];
  57. for(int lx = 0;lx < to[prc].size();lx++){ a[lx] = lx; }
  58. random_shuffle(a, a+to[prc].size());
  59. for(int ly = 0;ly < to[prc].size();ly++){
  60. int pind = to[prc][a[ly]];
  61. if(col[pind] != -1) continue;
  62. color(pind);
  63. }
  64. return;
  65. }
  66. int main()
  67. {
  68. int n;
  69. srand(514514);
  70. for(;;){
  71. scanf("%d", &n);
  72. if(n == 0) break;
  73. pii polys[100][100];
  74. int ptcnt[100] = {0};
  75. bool metr[100][100] = {0};
  76. for(int lx = 0;lx < n;lx++)
  77. to[lx].clear();
  78. for(int lx = 0;lx < n;lx++){
  79. scanf("%d", ptcnt+lx);
  80. for(int ly = 0;ly < ptcnt[lx];ly++)
  81. scanf("%d %d", &polys[lx][ly].X, &polys[lx][ly].Y);
  82. }
  83. for(int lx = 0;lx < n;lx++){
  84. for(int ly = lx+1;ly < n;ly++){
  85. //printf("cal %d -> %d\n", lx, ly);
  86. bool ok = false;
  87. for(int la = 0;la < ptcnt[lx] and not ok; la++){
  88. for(int lb = 0;lb < ptcnt[ly] and not ok;lb++){
  89. int na = (la+1)%ptcnt[lx];
  90. int nb = (lb+1)%ptcnt[ly];
  91. if(check(polys[lx][la], polys[lx][na], polys[ly][lb], polys[ly][nb])){
  92. //printf("%d %d %d %d\n", la, lb, na, nb);
  93. ok = true;
  94. }
  95. }
  96. }
  97. metr[lx][ly] = ok;
  98. metr[ly][lx] = ok;
  99. }
  100. }
  101. if(n == 1){ puts("1"); continue; }
  102.  
  103. for(int lx = 0;lx < n;lx++)
  104. for(int ly = 0;ly < n;ly++)
  105. if(metr[lx][ly]){
  106. //printf("%d -> %d\n", lx, ly);
  107. to[lx].push_back(ly);
  108. }
  109. int min_color = 4;
  110. for(int _ = 0;_ < 1000;_++){
  111. for(int lx = 0;lx < n;lx++) col[lx] = -1;
  112. for(int lx = 0;lx < n;lx++)
  113. if(col[lx] == -1)
  114. color(lx);
  115. int max_c = -1;
  116. for(int lx = 0;lx < n;lx++) max_c = max(max_c, col[lx]);
  117. min_color = min(min_color, max_c);
  118. }
  119. if(min_color == 4){
  120. // check 3-cyc
  121. int cyc3 = 0;
  122. for(int lx = 0;lx < n;lx++)
  123. for(int ly = lx+1;ly < n;ly++)
  124. for(int lz = ly+1;lz < n;lz++)
  125. cyc3 += metr[lx][ly] and metr[lz][ly] and metr[lx][lz];
  126. if(cyc3 == 0){puts("3"); continue;}
  127. }
  128. printf("%d\n", min_color);
  129. }
  130. return 0;
  131. }

JAG K - Idempotent Filter

http://jag2014autumn.contest.atcoder.jp/tasks/icpc2014autumn_k

To check : for all x in imgF, F(x) = x

  1. #include<cstdio>
  2. #include<cstdlib>
  3. #include<algorithm>
  4. #include<cstring>
  5. #include<vector>
  6. #include<utility>
  7. #include<map>
  8. #include<set>
  9. #include<queue>
  10. #include<cmath>
  11. using namespace std;
  12. typedef long long int int64;
  13. char str[1000];
  14. const int fil = (1<<0) + (1<<1) + (1<<2) + (1<<4) + (1<<5) + (1<<6);
  15. int make(int a0, int a1, int a2, int a3, int a4, int a5, int a6){
  16. return (a0<<0)+(a1<<1)+(a2<<2)+(a3<<3)+(a4<<4)+(a5<<5)+(a6<<6);
  17. }
  18. int g(int x){ return str[x] == '1'; }
  19. int f(int x){
  20. //printf("f(%d)\n", x);
  21. return (x&fil)|(g(x)<<3);
  22. }
  23.  
  24. // ***
  25. // ****
  26. //*****
  27. // ****
  28. // ***
  29. void prt(int x){
  30. for(int lx = 0;lx < 7;lx++)
  31. printf("%d", (x>>lx)&1);
  32. return;
  33. }
  34. int main()
  35. {
  36.  
  37. for(;;){
  38. scanf("%s", str);
  39. if(str[0] == '#') break;
  40. // Find Image
  41. bool check = true;
  42. for(int lx = 0;lx < (1<<19) and check;lx++){
  43. int p[19];
  44. for(int pp = 0;pp < 19;pp++){ p[pp] = (lx>>pp)&1; }
  45. int img = make(g(make(p[18], p[15], p[17], p[14], p[10], p[13], p[9])),
  46. g(make(p[15], p[11], p[14], p[10], p[6], p[9], p[5])),
  47. g(make(p[17], p[14], p[16], p[13], p[9], p[12], p[8])),
  48. g(make(p[14], p[10], p[13], p[9], p[5], p[8], p[4])),
  49. g(make(p[10], p[6], p[9], p[5], p[2], p[4], p[1])),
  50. g(make(p[13], p[9], p[12], p[8], p[4], p[7], p[3])),
  51. g(make(p[9], p[5], p[8], p[4], p[1], p[3], p[0])));
  52. //prt(img); printf(" -> "); prt(f(img)); puts("");
  53. if(img != f(img))
  54. check = false;
  55. }
  56. puts(check ? "yes":"no");
  57. }
  58. return 0;
  59. }