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) » Paragraph Formatting » All Submissions » Shishir Mittal [108175]
0
Your rating: None

CodeChef submission 108175 (C++ 4.0.0-8)

CodeChef submission 108175 (C++ 4.0.0-8) plaintext list. Status: RE, problem H5, contest OCT09. By porsh (Shishir Mittal), 2009-10-11 01:01:00.
  1. #include<iostream>
  2. using namespace std;
  3. int p2;
  4. struct LINE{
  5. int len;
  6. int stIndex;
  7. }line[205];
  8.  
  9. int binarySearch(struct LINE arr[],int low,int high, int x){
  10. int mid,i,k;
  11. while(low < high){
  12. mid = (low + high + 1)/2;
  13. if(x >= arr[mid].stIndex)
  14. low = mid;
  15. else
  16. high = mid -1;
  17. }
  18. return low;
  19. }
  20.  
  21. struct wordNode{
  22. int key,S;
  23. }word[132000];
  24.  
  25. void updateWordTree(int wordIndex, int delta){
  26. int index = p2 + wordIndex - 1;
  27. word[index].S += delta;
  28. do{
  29. if(index%2 == 0){
  30. word[index/2].S += delta;
  31. }
  32. index = index/2;
  33. }while(index > 1);
  34. }
  35. int getSum(int wordIndex){
  36. if(wordIndex== 0)
  37. return 0;
  38. int i=1,k,sum=0;
  39. while(i<p2){
  40. if(wordIndex <= word[i].key)
  41. i = 2*i;
  42. else{
  43. sum += word[i].S;
  44. i = 2*i+1;
  45. }
  46. }
  47. sum += word[i].S;
  48. //cout<<"getSum("<<wordIndex<<") = "<<sum<<endl;
  49. return sum;
  50. }
  51.  
  52. int wordSearch(int lowWordIndex, int highWordIndex, int charNo){
  53. //cout<<lowWordIndex<<","<<highWordIndex<<endl;
  54. int midSum,low = lowWordIndex,high = highWordIndex,mid, s= getSum(lowWordIndex-1);
  55. while(low < high){
  56. mid = (low + high)/2;
  57. midSum = getSum(mid);
  58. if(charNo > midSum - s + mid - lowWordIndex+1)
  59. low = mid +1;
  60. else
  61. high = mid;
  62. }
  63. if(getSum(high) - s + high - lowWordIndex <= charNo)
  64. return high;
  65. else
  66. return high-1;
  67. }
  68. int main(){
  69. int i,T,kase,k,M,N,h,arr[50000],lineCtr=1,length,wordIndex,newLen,lineNo,delta,charNo,w;
  70. long long sum[50000];
  71. scanf("%d",&T);
  72. for(kase=1;kase<=T;kase++){
  73. printf("Case #%d:\n",kase);
  74. //input M, N, ai's.
  75. scanf("%d %d",&M,&N);
  76. for(p2=1;p2<N;p2*=2);
  77. lineCtr=1;
  78. sum[0] = 0;
  79. //form line array with the indices of first word
  80. line[lineCtr].stIndex = 1;
  81. scanf("%d",&arr[1]);
  82. sum[1] = arr[1] + sum[0];
  83. length = arr[1];
  84. for(i=2;i<=N;i++){
  85. scanf("%d",&arr[i]);
  86. sum[i] = arr[i] + sum[i-1];
  87. if(length + 1 + arr[i] <=M)
  88. length += 1+arr[i];
  89. else{
  90. line[lineCtr].len = length;
  91. length = arr[i];
  92. lineCtr++;
  93. line[lineCtr].stIndex = i;
  94. }
  95. }
  96. line[lineCtr].len = length;
  97. line[lineCtr+1].stIndex = N+1;
  98. line[lineCtr+1].len = 0;
  99. //form word tree to find 'sum till ith word in theta(log N) time'.
  100. for(i=N+1;i<=p2;i++){
  101. arr[i] = 0;
  102. sum[i] = sum[i-1];
  103. }
  104. for(i=0;i<p2;i++){
  105. word[p2+i].key = i+1;
  106. word[p2+i].S = arr[i+1];
  107. }
  108. for(k=p2/2,h=1;k>=1;k = k/2,h++){
  109. for(i=k;i<2*k;i++){
  110. word[i].key = word[2*i +1].key - 1;
  111. word[i].S = sum[word[i].key] - sum[word[i].key-h];
  112. }
  113. }
  114. //Input Query
  115. char ch[2];
  116. while(scanf("%1s",ch), ch[0] != 'E'){
  117. //if Query is to output #line, binary search in line array.
  118. switch(ch[0]){
  119. case 'I': scanf("%d",&wordIndex);
  120. printf("%d\n",binarySearch(line,1 , lineCtr, wordIndex));
  121. break;
  122. //if Query is changing wordLength,
  123. case 'C': scanf("%d %d",&wordIndex,&newLen);
  124. //First find the #line,
  125. lineNo = binarySearch(line,1 , lineCtr, wordIndex);
  126. //cout<<"lineNo:"<<lineNo<<endl;
  127. delta = newLen - word[p2+wordIndex-1].S;
  128. //cout<<"delta="<<delta<<endl;
  129. updateWordTree(wordIndex, delta);
  130. if(delta==0)
  131. break;
  132. else if(delta < 0 && lineNo > 1){
  133. wordIndex = line[lineNo].stIndex;
  134. charNo = M - (getSum(line[lineNo].stIndex-1) - getSum(line[lineNo-1].stIndex-1) + line[lineNo].stIndex - line[lineNo-1].stIndex);
  135. w = wordSearch(line[lineNo].stIndex,line[lineNo +1].stIndex-1,charNo);
  136. line[lineNo].stIndex = w+1;
  137. charNo = M - (getSum(line[lineNo+1].stIndex-1) - getSum(line[lineNo].stIndex-1) + line[lineNo+1].stIndex - line[lineNo].stIndex);
  138. while(lineNo<lineCtr && (w = wordSearch(line[lineNo+1].stIndex,line[lineNo +2].stIndex-1,charNo)) != line[lineNo+1].stIndex-1){
  139. lineNo++;
  140. line[lineNo].stIndex = w+1;
  141. charNo = M - (getSum(line[lineNo+1].stIndex-1) - getSum(line[lineNo].stIndex-1) + line[lineNo+1].stIndex - line[lineNo].stIndex);
  142. }
  143. if(line[lineCtr].stIndex == N+1){
  144. //cout<<"afa"<<endl;
  145. lineCtr--;
  146. }
  147. }
  148. else{
  149. charNo = M - (getSum(wordIndex-1) - getSum(line[lineNo].stIndex-1) + wordIndex - line[lineNo].stIndex);
  150. //if(delta < 0) lineNo++;
  151. while((w = wordSearch(wordIndex,line[lineNo +1].stIndex-1,charNo)) != line[lineNo+1].stIndex-1){
  152. //if(getSum(w) - getSum(line[lineNo].stIndex) + w - line[lineNo].stIndex >= M-1){
  153. //cout<<"w="<<w<<endl;
  154. charNo = M - (getSum(line[lineNo+1].stIndex - 1) - getSum(w) + line[lineNo+1].stIndex -w -1);
  155. lineNo++;
  156. if(lineNo > lineCtr){
  157. lineCtr = lineNo;
  158. line[lineCtr+1].stIndex = N+1;
  159. //break;
  160. }
  161. wordIndex = line[lineNo].stIndex;
  162. line[lineNo].stIndex = w+1;
  163. if(wordIndex == N+1)
  164. break;
  165. }
  166. }
  167. break;
  168. default: cout<<"Error parsing input\n";
  169. }
  170. }
  171. printf("\n");
  172. }
  173. return 0;
  174. }


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