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  » November 2009 (Contest X) » The Best Box » All Submissions » Balakrishnan Varadarajan [130297]
0
Your rating: None

CodeChef submission 130297 (C++ 4.0.0-8)

CodeChef submission 130297 (C++ 4.0.0-8) plaintext list. Status: WA, problem J7, contest NOV09. By balakrishnan_v (Balakrishnan Varadarajan), 2009-11-11 04:47:56.
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. int D,S;
  6. int B[502];
  7. bool M[502][502];
  8. int T[502];
  9. int l[501][502];
  10. int dl[501][502];
  11. int nextl[501][502];
  12. int nextdl[501][502];
  13. bool nextthere[501][502];
  14. int gains[501][502];
  15. bool there[501][502];
  16. int optswaps[501][502];
  17. int numexts[501];
  18. int totgains[502];
  19. int satis[502*501];
  20. int kval[502*501];
  21. int swapval[502*501];
  22. bool answer[502];
  23. int likes[502];
  24. int dislikes[502];
  25. int width;
  26. int kcurr;
  27. bool diff[90][90][502];
  28. int distance[90][90];
  29. int max(int a,int b){return a>b?a:b;}
  30. int min(int a,int b){return a<b?a:b;}
  31.  
  32. int main()
  33. {
  34. scanf("%d %d",&D,&S);
  35. for(int i=0;i<D;i++)
  36. scanf("%d",&B[i]);
  37. for(int i=0;i<D;i++)
  38. for(int j=0;j<S;j++)
  39. scanf("%d",&M[i][j]);
  40. for(int i=0;i<D;i++)
  41. {
  42. l[0][i]=0;
  43. dl[0][i]=0;
  44. }
  45. for(int i=0;i<S;i++)
  46. there[0][i]=0;
  47. satis[0]=0;
  48. swapval[0]=-1;
  49. width=500;
  50. kcurr=1;
  51. int maxsatis=-1;
  52. for(int iters=1;iters<=30;iters++)
  53. {
  54. if(iters==2)
  55. width=400;
  56. if(iters>2)
  57. width=50;
  58. for(int i=0;i<=D;i++)
  59. totgains[i]=0;
  60. if(iters>=4)
  61. {
  62. for(int i=0;i<kcurr;i++)
  63. for(int j=0;j<kcurr;j++)
  64. {
  65. distance[i][j]=0;
  66. for(int k=0;k<S;k++)
  67. {
  68. diff[i][j][k]=there[i][k]^there[j][k];
  69. distance[i][j]+=there[i][k]^there[j][k];
  70. }
  71. }
  72. }
  73.  
  74. for(int i=0;i<kcurr;i++)
  75. {
  76. //printf("%d\n",iters);
  77. // printf("%d %d %d %d %d\n",there[i][0],there[i][1],there[i][2],there[i][3],there[i][4]);
  78. //printf("%d %d %d %d\n",l[i][0],l[i][1],l[i][2],l[i][3]);
  79. //printf("%d %d %d %d\n",dl[i][0],dl[i][1],dl[i][2],dl[i][3]);
  80. // printf("------------------\n");
  81. for(int j=0;j<S;j++)
  82. {
  83. gains[i][j]=1000;
  84. if(j==swapval[i])
  85. continue;
  86. if(iters==2 && swapval[i]>j)
  87. continue;
  88. bool repeat=0;
  89. if(iters>=4)
  90. {
  91. for(int k=0;k<i;k++)
  92. {
  93. if(distance[i][k]==2 && diff[i][k][j])
  94. {
  95. repeat=1;
  96. //printf("sdfsdf\n");
  97. break;
  98. }
  99. }
  100. }
  101. if(repeat)
  102. continue;
  103.  
  104.  
  105. gains[i][j]=satis[i];
  106. for(int k=0;k<D;k++)
  107. {
  108. if(there[i][j])//going to remove
  109. {
  110. if(M[k][j] && l[i][k]==1 && dl[i][k]<B[k])
  111. gains[i][j]--;
  112. if(!M[k][j] && l[i][k]>0 && dl[i][k]==B[k])
  113. gains[i][j]++;
  114. }
  115. else//going to add
  116. {
  117. if(M[k][j] && l[i][k]==0 && dl[i][k]<B[k])
  118. gains[i][j]++;
  119. if(!M[k][j] && l[i][k]>0 && dl[i][k]==B[k]-1)
  120. gains[i][j]--;
  121. }
  122. }
  123. //printf("%d %d %d %d %d swapping %d initial=%d g=%d ,%d %d,iters=%d\n",there[i][0],there[i][1],there[i][2],there[i][3],there[i][4],j,satis[i],gains[i][j],dl[i][0],l[i][0],iters);
  124. totgains[D-gains[i][j]]++;
  125. }
  126. }
  127. //gains can be from 0 to +D
  128. for(int i=1;i<=D;i++)
  129. {
  130. //printf("%d %d\n",i,totgains[i]);
  131. totgains[i]+=totgains[i-1];//cumulative frequency
  132. }
  133. int size=totgains[D];
  134. //printf("size=%d\n",size);
  135. for(int i=0;i<kcurr;i++)
  136. {
  137. for(int j=0;j<S;j++)//bucket sort
  138. {
  139. if(gains[i][j]>D)
  140. continue;
  141. //else
  142. // {
  143. // printf("%d %d\n",i,j);
  144. // }
  145. satis[totgains[D-gains[i][j]]-1]=gains[i][j];
  146. kval[totgains[D-gains[i][j]]-1]=i;
  147. swapval[--totgains[D-gains[i][j]]]=j;
  148. }
  149. }
  150. int up=min(width,size)-1;
  151. //printf("%d %d\n",up,totgains[D-satis[up]+1]-1);
  152. //up=min(totgains[D-satis[up]+1]-1,40);
  153.  
  154. for(int i=0;i<=up;i++)
  155. {
  156. for(int j=0;j<S;j++)
  157. {
  158. nextthere[i][j]=there[kval[i]][j];
  159. //printf("%d,",there[kval[i]][j]);
  160. }
  161. int j=swapval[i];
  162. for(int k=0;k<D;k++)
  163. {
  164. nextl[i][k]=l[kval[i]][k];
  165. nextdl[i][k]=dl[kval[i]][k];
  166. }
  167. for(int k=0;k<D;k++)
  168. {
  169. if(nextthere[i][j] && M[k][j])
  170. nextl[i][k]=l[kval[i]][k]-1;
  171. if(nextthere[i][j] && !M[k][j])
  172. nextdl[i][k]=dl[kval[i]][k]-1;
  173. if(!nextthere[i][j] && M[k][j])
  174. nextl[i][k]=l[kval[i]][k]+1;
  175. if(!nextthere[i][j] && !M[k][j])
  176. nextdl[i][k]=dl[kval[i]][k]+1;
  177. }
  178. nextthere[i][j]=!nextthere[i][j];
  179. }
  180. for(int i=0;i<=up;i++)
  181. {
  182. for(int j=0;j<S;j++)
  183. there[i][j]=nextthere[i][j];
  184. for(int k=0;k<D;k++)
  185. {
  186. l[i][k]=nextl[i][k];
  187. dl[i][k]=nextdl[i][k];
  188. }
  189. if(satis[i]>maxsatis)
  190. {
  191. for(int j=0;j<S;j++)
  192. answer[j]=there[i][j];
  193. for(int j=0;j<D;j++)
  194. {
  195. likes[j]=l[i][j];
  196. dislikes[j]=dl[i][j];
  197. }
  198. maxsatis=satis[i];
  199. }
  200. }
  201. kcurr=up-1;
  202. }
  203. int tot=0;
  204. for(int i=0;i<S;i++)
  205. {
  206. tot+=answer[i];
  207. }
  208. for(int i=0;i<D;i++)
  209. {
  210. T[i]=1+max(0,tot-B[i]);
  211. }
  212. printf("%d\n",tot);
  213. int S1[600];
  214. int S2[600];
  215. int val=maxsatis;
  216. for(int iters=1;iters<=0;iters++)
  217. {
  218. int inival=val;
  219. int S1size=0;
  220. for(int i=0;i<D;i++)
  221. if(likes[i]==T[i]-1)
  222. S1[S1size++]=i;
  223. int maxcount=-1;
  224. int iopt=-1;
  225. for(int i=0;i<S;i++)
  226. {
  227. if(!answer[i])
  228. {
  229. int count=0;
  230. for(int j=0;j<S1size;j++)
  231. {
  232. if(M[S1[j]][i])
  233. {
  234. count++;
  235. }
  236. }
  237. if(count>maxcount)
  238. {
  239. iopt=i;
  240. maxcount=count;
  241. }
  242. }
  243. }
  244. if(iopt==-1)
  245. break;
  246. answer[iopt]=1;
  247. //printf("--%d %d\n",maxcount,iopt);
  248. val+=maxcount;
  249. for(int j=0;j<D;j++)
  250. {
  251. if(M[j][iopt])
  252. likes[j]++;
  253. }
  254. int S2size=0;
  255. for(int i=0;i<D;i++)
  256. if(likes[i]==T[i])
  257. S2[S2size++]=i;
  258. iopt=-1;
  259. maxcount=3000;
  260. for(int i=0;i<S;i++)
  261. {
  262. if(answer[i])
  263. {
  264. int count=0;
  265. for(int j=0;j<S2size;j++)
  266. {
  267. if(M[S2[j]][i])
  268. count++;
  269. }
  270. if(count<maxcount)
  271. {
  272. iopt=i;
  273. maxcount=count;
  274. }
  275. }
  276. }
  277. if(iopt==-1)
  278. break;
  279. answer[iopt]=0;
  280. for(int j=0;j<D;j++)
  281. {
  282. if(M[j][iopt])
  283. likes[j]--;
  284. }
  285. //printf("^^%d\n",maxcount);
  286. val-=maxcount;
  287. //printf("%d %d\n",val,inival);
  288. if(val==inival)
  289. break;
  290. }
  291. for(int i=0;i<S;i++)
  292. if(answer[i])
  293. printf("%d\n",i);
  294. //printf("satis=%d\n",maxsatis);
  295. /*int val=0;
  296. for(int i=0;i<D;i++)
  297. {
  298. int likes=0;
  299. int dislikes=0;
  300. //printf("error %d\n",l[i]);
  301. for(int j=0;j<S;j++)
  302. {
  303. if(answer[j])
  304. {
  305. if(M[i][j])
  306. likes++;
  307. else
  308. dislikes++;
  309. }
  310. }
  311. //if(likes!=l[i])
  312. //printf("error %d %d %d\n",i,likes,l[i]);
  313. if(likes>0 && dislikes<B[i])
  314. val++;
  315. }
  316. printf("%d\n",val);*/
  317. }


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