CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • February CookOff
    • February Long Contest
    • January CookOff
  • DISCUSS
    • Wiki
    • Forums
    • Blog
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
    • Event Calendar
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Ranks
    • Tutorials
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home  » October 2009 (Contest IX) » Forces in the crystal » All Submissions » Keshav Dhandhania [108887]
0
Your rating: None

CodeChef submission 108887 (C++ 4.0.0-8)

CodeChef submission 108887 (C++ 4.0.0-8) plaintext list. Status: AC, problem HX, contest OCT09. By keshav_57 (Keshav Dhandhania), 2009-10-11 14:53:33.
  1.  
  2. // TO DO
  3. // Think of something which is 'global'
  4. // Like start from someplace with greater concentration of larger numbers
  5. // Check if repeating flip_two() will help, or make a queue out of this as well
  6.  
  7. #include<iostream>
  8. #include<fstream>
  9. #include<queue>
  10. #include<cstring>
  11. #include<algorithm>
  12. using namespace std;
  13.  
  14. #define sz 2010
  15. #define INF 100000000.0
  16. #define block 2
  17.  
  18. int n;
  19. float q[sz][sz];
  20. bool b[sz][sz];
  21. float sum[sz][sz];
  22. float SCORE[sz][sz];
  23. float SCOREc[sz][sz];
  24. int nx[7]; int ny[2][7];
  25.  
  26. float qdp[sz][sz];
  27. float cost[sz][(1<<block)];
  28. int prev[sz][(1<<block)];
  29. float sum3[sz][sz];
  30.  
  31. void modify(int &i, int &j)
  32. {
  33. for (int k = 0; k < 6; k++)
  34. if (b[i][j]==b[i+nx[k]][j+ny[(i&1)][k]]) SCORE[i+nx[k]][j+ny[(i&1)][k]] += q[i][j];
  35. else SCORE[i+nx[k]][j+ny[(i&1)][k]] -= q[i][j];
  36. }
  37.  
  38. void modify3(int &i, int &j)
  39. {
  40. for (int k = 0; k < 3; k++)
  41. if (b[i][j]==b[i+nx[k]][j+ny[(i&1)][k]]) SCORE[i+nx[k]][j+ny[(i&1)][k]] += q[i][j];
  42. else SCORE[i+nx[k]][j+ny[(i&1)][k]] -= q[i][j];
  43. }
  44.  
  45. void input()
  46. {
  47. scanf("%d\n", &n);
  48. char str[100000];
  49. int l = 0;
  50. float d;
  51. for (int i = 1; i <= n; i++)
  52. {
  53. gets(str);
  54. l = 0;
  55. for (int j = 1; j <= n; j++)
  56. {
  57. d = 1.0; q[i][j] += d*(str[l]-'0');
  58. while (1)
  59. {
  60. l++;
  61. if (str[l] == ' ' || str[l] == '\0') break;
  62. else if (str[l] >= '0' && str[l] <= '9')
  63. { d /= 10.0; q[i][j] += (str[l]-'0')*d; }
  64. }
  65. l++;
  66. }
  67. }
  68. }
  69.  
  70. void output()
  71. {
  72. char str[10000];
  73. memset(str, ' ', sizeof(str));
  74. str[2*n] = '\0';
  75. for (int i = 1; i <= n; i++)
  76. {
  77. for (int j = 1; j <= n; j++)
  78. str[2*(j-1)] = '0' + (b[i][j]?1:0);
  79. puts(str);
  80. }
  81. }
  82.  
  83. void init()
  84. {
  85. nx[1] = nx[2] = -1; nx[4] = nx[5] = 1;
  86. ny[1][1] = ny[1][5] = -1;
  87. ny[0][2] = ny[0][4] = 1;
  88. ny[1][0] = ny[0][0] = -1;
  89. ny[1][3] = ny[0][3] = 1;
  90. for (int i = 1; i <= n; i++)
  91. for (int j = 1; j <= n; j++)
  92. for (int k = 0; k < 6; k++)
  93. {
  94. if (k==3) sum3[i][j] = sum[i][j];
  95. sum[i][j] += q[i+nx[k]][j+ny[(i&1)][k]];
  96. }
  97. }
  98.  
  99. void score (int i, int j)
  100. {
  101. SCORE[i][j] = 0.0;
  102. for (int k = 0; k < 6; k++)
  103. if (b[i+nx[k]][j+ny[(i&1)][k]]==b[i][j])
  104. SCORE[i][j] += q[i+nx[k]][j+ny[(i&1)][k]];
  105. }
  106.  
  107. void score3 (int &i, int j, float &r1, float &r2)
  108. {
  109. r1 = 0.0;
  110. for (int k = 0; k < 3; k++)
  111. if (b[i+nx[k]][j+ny[(i&1)][k]]==b[i][j])
  112. r1 += q[i+nx[k]][j+ny[(i&1)][k]];
  113. r2 = sum3[i][j] - r1;
  114. }
  115.  
  116. void scoreref3 (int &i, int &j, float &r1, float &r2)
  117. {
  118. r1 = 0.0;
  119. for (int k = 0; k < 3; k++)
  120. if (b[i+nx[k]][j+ny[(i&1)][k]]==b[i][j])
  121. r1 += q[i+nx[k]][j+ny[(i&1)][k]];
  122. r2 = sum3[i][j] - r1;
  123. }
  124.  
  125. void scoreref (int &i, int &j)
  126. {
  127. SCORE[i][j] = 0.0;
  128. for (int k = 0; k < 6; k++)
  129. if (b[i+nx[k]][j+ny[(i&1)][k]]==b[i][j])
  130. SCORE[i][j] += q[i+nx[k]][j+ny[(i&1)][k]];
  131. }
  132.  
  133. void flip_one_queue()
  134. {
  135. float r1, r2;
  136. queue <int> x;
  137. queue <int> y;
  138. int px, py;
  139. for (int i = 1; i <= n; i++)
  140. for (int j = 1; j <= n; j++)
  141. {
  142. scoreref(i, j);
  143. if (sum[i][j]-SCORE[i][j] < SCORE[i][j])
  144. {
  145. b[i][j] = !b[i][j];
  146. SCORE[i][j] = sum[i][j]-SCORE[i][j];
  147. modify3(i,j);
  148. for (int k = 0; k < 3; k++)
  149. {
  150. x.push(i+nx[k]);
  151. y.push(j+ny[(i&1)][k]);
  152. }
  153. // the rest three are later, so they are automatically checked; dont push them;
  154. }
  155. }
  156. while (!x.empty())
  157. {
  158. px = x.front();
  159. py = y.front();
  160. x.pop(); y.pop();
  161. if (px < 1 || px > n || py < 1 || py > n) continue;
  162. if (sum[px][py]-SCORE[px][py] < SCORE[px][py])
  163. {
  164. SCORE[px][py] = sum[px][py]-SCORE[px][py];
  165. b[px][py] = !b[px][py];
  166. modify(px,py);
  167. for (int k = 0; k < 6; k++)
  168. {
  169. x.push(px+nx[k]);
  170. y.push(py+ny[(px&1)][k]);
  171. }
  172. }
  173. }
  174. }
  175.  
  176. void flip_one_queue2()
  177. {
  178. float r1, r2;
  179. queue <int> x;
  180. queue <int> y;
  181. int px, py;
  182. for (int i = 1; i <= n; i++)
  183. for (int j = 1; j <= n; j++)
  184. {
  185. if (sum[i][j]-SCORE[i][j] < SCORE[i][j])
  186. {
  187. b[i][j] = !b[i][j];
  188. SCORE[i][j] = sum[i][j]-SCORE[i][j];
  189. modify(i,j);
  190. for (int k = 0; k < 3; k++)
  191. {
  192. x.push(i+nx[k]);
  193. y.push(j+ny[(i&1)][k]);
  194. }
  195. // the rest three are later, so they are automatically checked; dont push them;
  196. }
  197. }
  198. while (!x.empty())
  199. {
  200. px = x.front();
  201. py = y.front();
  202. x.pop(); y.pop();
  203. if (px < 1 || px > n || py < 1 || py > n) continue;
  204. if (sum[px][py]-SCORE[px][py] < SCORE[px][py])
  205. {
  206. SCORE[px][py] = sum[px][py]-SCORE[px][py];
  207. b[px][py] = !b[px][py];
  208. modify(px,py);
  209. for (int k = 0; k < 6; k++)
  210. {
  211. x.push(px+nx[k]);
  212. y.push(py+ny[(px&1)][k]);
  213. }
  214. }
  215. }
  216. }
  217.  
  218. void dp()
  219. {
  220. int rem = n%block;
  221. int width = n + (rem?block-rem:0);
  222. int bitmask[(1<<block)]; bitmask[0] = 0;
  223. for (int i = 1; i < (1<<block); i++) bitmask[i] = (bitmask[i-1]^(i&(-i)));
  224. float cur;
  225. float r1, r2;
  226. int start, end;
  227. float addi;
  228. int exp = (1<<block);
  229. int i, j, k;
  230.  
  231. for (int c = 0; c < (width/block); c++)
  232. {
  233. memset(cost, 125, sizeof(cost));
  234. cost[0][0] = 0.0;
  235. start = c*block + 1; end = start + 1;
  236. for (i = 1; i <= n; i++)
  237. {
  238. qdp[i][start] = q[i][start];
  239. qdp[i][end] = q[i][end];
  240. }
  241. for (i = 1; i <= n+1; i++)
  242. for (j = 0; j < exp; j++) // state of prev row
  243. {
  244. addi = 0.0;
  245. cur = 0.0;
  246. b[i][start] = b[i][end] = false;
  247. b[i-1][start] = ((1&j)?true:false);
  248. b[i-1][end] = ((2&j)?true:false);
  249. scoreref3(i,start,r1,r2);
  250. cur += r1*qdp[i][start];
  251. scoreref3(i,end,r1,r2);
  252. cur += r1*qdp[i][end];
  253. if (i&1) if (!b[i+1][start-1]) addi = q[i][j]*q[i+1][start-1];
  254. if (cost[i][0] > cost[i-1][j]+cur+addi)
  255. {
  256. cost[i][0] = cost[i-1][j]+cur+addi;
  257. prev[i][0] = j;
  258. }
  259. for (k = 1; k < exp; k++)
  260. {
  261. if (k&1)
  262. {
  263. scoreref3(i,start,r1,r2);
  264. b[i][start] ^= 1;
  265. cur += qdp[i][start]*(r2-r1);
  266. }
  267. else
  268. {
  269. scoreref3(i,end,r1,r2);
  270. b[i][end] ^= 1;
  271. cur += qdp[i][end]*(r2-r1);
  272. }
  273. addi = (((i&1)&&(b[i+1][start-1]^(bitmask[k]&1)))?q[i][j]*q[i+1][start-1]:0.0);
  274. if (cost[i][bitmask[k]] > cost[i-1][j]+cur+addi)
  275. {
  276. cost[i][bitmask[k]] = cost[i-1][j]+cur+addi;
  277. prev[i][bitmask[k]] = j;
  278. }
  279. }
  280. }
  281. int now = 0;
  282. for (i = n; i >= 1; i--)
  283. {
  284. now = prev[i+1][now];
  285. for (int j = 0; j < block; j++)
  286. if ((1<<j)&now) b[i][start+j] = true;
  287. else b[i][start+j] = false;
  288. }
  289. }
  290. }
  291.  
  292. void flip_two()
  293. {
  294. float af, bf, cf, df;
  295. int tempi, tempj;
  296. bool v;
  297. if (n < 1851)
  298. {
  299. for (int i = 2; i <= n; i++)
  300. for (int j = 1; j <= n; j++)
  301. for (int k = 0; k < 3; k++)
  302. {
  303. v = (b[i][j]==b[i+nx[k]][j+ny[(i&1)][k]]);
  304. if
  305. ((sum[i][j] - SCORE[i][j] - (!v?q[i+nx[k]][j+ny[(i&1)][k]]:0.0))*q[i][j] +
  306. (sum[i+nx[k]][j+ny[(i&1)][k]] - SCORE[i+nx[k]][j+ny[(i&1)][k]] - (!v?q[i][j]:0.0))*q[i+nx[k]][j+ny[(i&1)][k]] <
  307. (SCORE[i][j] - (v?q[i+nx[k]][j+ny[(i&1)][k]]:0.0))*q[i][j] +
  308. (SCORE[i+nx[k]][j+ny[(i&1)][k]] - (v?q[i][j]:0.0))*q[i+nx[k]][j+ny[(i&1)][k]])
  309. {
  310. b[i][j] = !b[i][j]; b[i+nx[k]][j+ny[(i&1)][k]] = !b[i+nx[k]][j+ny[(i&1)][k]];
  311. modify(i,j); tempi = i+nx[k]; tempj = j+ny[(i&1)][k];
  312. modify(tempi,tempj);
  313. scoreref(i,j);
  314. scoreref(tempi,tempj);
  315. }
  316. }
  317. }
  318. else
  319. {
  320. for (int i = 1; i <= n; i++)
  321. for (int j = 2; j <= n; j++)
  322. {
  323. if ((sum[i][j] - SCORE[i][j] - (!v?q[i][j-1]:0.0))*q[i][j] +
  324. (sum[i][j-1] - SCORE[i][j-1] - (!v?q[i][j]:0.0))*q[i][j-1] <
  325. (SCORE[i][j] - (v?q[i][j-1]:0.0))*q[i][j] +
  326. (SCORE[i][j-1] - (v?q[i][j]:0.0))*q[i][j-1])
  327. {
  328. b[i][j] = !b[i][j]; b[i][j-1] = !b[i][j-1];
  329. modify(i,j);
  330. tempj = j-1;
  331. modify(i,tempj);
  332. scoreref(i,j);
  333. score(i,tempj);
  334. }
  335. }
  336. }
  337. }
  338.  
  339. int main()
  340. {
  341. input();
  342. init(); dp();
  343. flip_one_queue();
  344. if (n < 1751)
  345. {
  346. flip_two();
  347. flip_one_queue2();
  348. }
  349. output();
  350. return 0;
  351. }
  352. /*
  353. int main ()
  354. {
  355. char test_str[15] = "FC .txt\0";
  356. char test_str2[15] = "9FC .txt\0";
  357. double sc = 0.0, sc2 = 0.0;
  358. ofstream result;
  359. result.open("R9FlipTwo2.txt");
  360. for (int test = 0; test < 10; test++)
  361. {
  362. sc = 0.0;
  363. n = 0;
  364. memset(q, 0, sizeof(q));
  365. memset(b, 0, sizeof(b));
  366. memset(sum, 0, sizeof(sum));
  367. memset(qdp, 0, sizeof(qdp));
  368. memset(prev, 0, sizeof(prev));
  369. memset(cost, 0, sizeof(cost));
  370. memset(sum3, 0, sizeof(sum3));
  371. test_str[2] = test_str2[3] = test+'0';
  372. freopen(test_str, "r", stdin);
  373. freopen(test_str2, "w", stdout);
  374. printf("\n");
  375. MAIN();
  376. for (int i = 1; i <= n; i++)
  377. for (int j = 1; j <= n; j++)
  378. sc += q[i][j-1]*q[i][j]*((b[i][j-1]==b[i][j])?1.0:0.0) + q[i-1][j]*q[i][j]*((b[i-1][j]==b[i][j])?1.0:0.0) + q[i-1][(i%2?j-1:j+1)]*q[i][j]*((b[i-1][(i%2?j-1:j+1)]==b[i][j])?1.0:0.0);
  379. sc2 += sc;
  380. result << sc << endl;
  381. fclose(stdin);
  382. fclose(stdout);
  383. }
  384. sc2 /= 10.0;
  385. result << endl << sc2 << endl;
  386. result.close();
  387. return 0;
  388. }
  389. */
  390. // 1421158.892623


Comments

  • Login or Register to post a comment.

CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of computer programming. At CodeChef we work hard to revive the geek in you by hosting programming contests on a monthly basis. We also aim to have training sessions and events related to online programming for programmers around the world. Apart from providing a platform for programming competitions, CodeChef also has various tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our judge accepts solutions in over 35+ programming languages. Online programming was never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming competitions that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly programming contests and the shorter format Cook-off programming contests. Put yourself up for recognition and win great prizes. Prizes worth up to Rs.20,000 and $700 are up for grabs every month along with lots more CodeChef goodies.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be part of CodeChefs Forums and interact with all our programmers love helping out other programmers and share their ideas.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. Be a part of the CodeChef community through CodeChef meetups and techtalks. You can also host a programming contest for your institute on CodeChef and be a guest author on our blog.

Domain Name Registration, Web hosting, and Website Design provided by BigRock.com