fork download
  1. /*
  2. written by- Piyush Golani
  3. language- c++
  4. country- India
  5. College- N.I.T Jamshedpur
  6. */
  7. #include <cmath>
  8. #include <ctime>
  9. #include <iostream>
  10. #include <string>
  11. #include <vector>
  12. #include<cstdio>
  13. #include<sstream>
  14. #include<algorithm>
  15. #include<cstdlib>
  16. #include<cstring>
  17. #include<map>
  18. #include<queue>
  19. #include<cctype>
  20. using namespace std;
  21. #define pb push_back
  22. #define all(s) s.begin(),s.end()
  23. #define f(i,a,b) for(int i=a;i<b;i++)
  24. #define F(i,a,b) for(int i=a;i>=b;i--)
  25. #define PI 3.1415926535897932384626433832795
  26. #define INF 2000000000
  27. #define BIG_INF 7000000000000000000LL
  28. #define mp make_pair
  29. #define eps 1e-9
  30. #define LL long long
  31. #define si(n) scanf("%d",&n)
  32. #define sll(n) scanf("%lld",&n)
  33. #define mod 1000000007
  34. #define mm 10000000
  35.  
  36. string inttostring(int n)
  37. {
  38. stringstream a;
  39. a<<n;
  40. return a.str();
  41. }
  42.  
  43. int stringtoint(string A)
  44. {
  45. istringstream a(A);
  46. int p;
  47. a>>p;
  48. return p;
  49. }
  50.  
  51. class CakeParty {
  52. public:
  53. int digit(int a)
  54. {
  55. return inttostring(a).size();
  56. }
  57. string get(int a,int b)
  58. {
  59. if(digit(a)==digit(b)) return inttostring(a);
  60. string cand1="1";
  61. f(i,0,digit(a)) cand1+="0";
  62. string cand2=inttostring(a);
  63. f(i,0,cand2.size())
  64. {
  65. if(cand1[i]==cand2[i]) continue;
  66.  
  67. if(cand1[i]<cand2[i]) return cand1;
  68. if(cand1[i]>cand2[i]) break;
  69. }
  70. return cand2;
  71. }
  72. string makeMove( vector <int> pieces ) {
  73. int max1=-INF;
  74. int n=pieces.size();
  75. if(n==1)
  76. {
  77. return "CAKE 0 PIECES "+inttostring(pieces[0]);
  78. }
  79. f(i,0,n) max1=max(max1,pieces[i]);
  80. int max2=-INF;
  81. f(i,0,n)
  82. {
  83. if(pieces[i]==max1) continue;
  84. max2=max(max2,pieces[i]);
  85. }
  86. int c1=0,c2=0;
  87. f(i,0,n)
  88. {
  89. if(pieces[i]==max1) c1++;
  90. if(pieces[i]==max2) c2++;
  91. }
  92. string cake="A";
  93. f(i,0,n) if(pieces[i]==max1) {cake=min(cake,inttostring(i));}
  94. string p;
  95. if(c1%2==0) p='1';
  96. else
  97. {
  98. if(c1>1) p='1';
  99. else
  100. {
  101. if(c2%2!=0)
  102. {
  103. p=inttostring(max1-max2);
  104. }
  105. else
  106. {
  107. p=get(max1-max2+1,max1);
  108. }
  109. }
  110. }
  111. //cout<<c1<<" "<<c2<<"\n";
  112. //cout<<p<<"\n";
  113. string res="CAKE "+ cake +" PIECES "+ p;
  114. return res;
  115. }
  116. };
  117.  
  118. // BEGIN CUT HERE
  119. namespace moj_harness {
  120. int run_test_case(int);
  121. void run_test(int casenum = -1, bool quiet = false) {
  122. if (casenum != -1) {
  123. if (run_test_case(casenum) == -1 && !quiet) {
  124. cerr << "Illegal input! Test case " << casenum << " does not exist." << endl;
  125. }
  126. return;
  127. }
  128.  
  129. int correct = 0, total = 0;
  130. for (int i=0;; ++i) {
  131. int x = run_test_case(i);
  132. if (x == -1) {
  133. if (i >= 100) break;
  134. continue;
  135. }
  136. correct += x;
  137. ++total;
  138. }
  139.  
  140. if (total == 0) {
  141. cerr << "No test cases run." << endl;
  142. } else if (correct < total) {
  143. cerr << "Some cases FAILED (passed " << correct << " of " << total << ")." << endl;
  144. } else {
  145. cerr << "All " << total << " tests passed!" << endl;
  146. }
  147. }
  148.  
  149. int verify_case(int casenum, const string &expected, const string &received, clock_t elapsed) {
  150. cerr << "Example " << casenum << "... ";
  151.  
  152. string verdict;
  153. vector<string> info;
  154. char buf[100];
  155.  
  156. if (elapsed > CLOCKS_PER_SEC / 200) {
  157. sprintf(buf, "time %.2fs", elapsed * (1.0/CLOCKS_PER_SEC));
  158. info.push_back(buf);
  159. }
  160.  
  161. if (expected == received) {
  162. verdict = "PASSED";
  163. } else {
  164. verdict = "FAILED";
  165. }
  166.  
  167. cerr << verdict;
  168. if (!info.empty()) {
  169. cerr << " (";
  170. for (int i=0; i<(int)info.size(); ++i) {
  171. if (i > 0) cerr << ", ";
  172. cerr << info[i];
  173. }
  174. cerr << ")";
  175. }
  176. cerr << endl;
  177.  
  178. if (verdict == "FAILED") {
  179. cerr << " Expected: \"" << expected << "\"" << endl;
  180. cerr << " Received: \"" << received << "\"" << endl;
  181. }
  182.  
  183. return verdict == "PASSED";
  184. }
  185.  
  186. int run_test_case(int casenum__) {
  187. switch (casenum__) {
  188. case 0: {
  189. int pieces[] = {47};
  190. string expected__ = "CAKE 0 PIECES 47";
  191.  
  192. clock_t start__ = clock();
  193. string received__ = CakeParty().makeMove(vector <int>(pieces, pieces + (sizeof pieces / sizeof pieces[0])));
  194. return verify_case(casenum__, expected__, received__, clock()-start__);
  195. }
  196. case 1: {
  197. int pieces[] = {3,3};
  198. string expected__ = "CAKE 0 PIECES 1";
  199.  
  200. clock_t start__ = clock();
  201. string received__ = CakeParty().makeMove(vector <int>(pieces, pieces + (sizeof pieces / sizeof pieces[0])));
  202. return verify_case(casenum__, expected__, received__, clock()-start__);
  203. }
  204. case 2: {
  205. int pieces[] = {3,5};
  206. string expected__ = "CAKE 1 PIECES 2";
  207.  
  208. clock_t start__ = clock();
  209. string received__ = CakeParty().makeMove(vector <int>(pieces, pieces + (sizeof pieces / sizeof pieces[0])));
  210. return verify_case(casenum__, expected__, received__, clock()-start__);
  211. }
  212. case 3: {
  213. int pieces[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1};
  214. string expected__ = "CAKE 0 PIECES 1";
  215.  
  216. clock_t start__ = clock();
  217. string received__ = CakeParty().makeMove(vector <int>(pieces, pieces + (sizeof pieces / sizeof pieces[0])));
  218. return verify_case(casenum__, expected__, received__, clock()-start__);
  219. }
  220. case 4: {
  221. int pieces[] = {3,3,112};
  222. string expected__ = "CAKE 2 PIECES 110";
  223.  
  224. clock_t start__ = clock();
  225. string received__ = CakeParty().makeMove(vector <int>(pieces, pieces + (sizeof pieces / sizeof pieces[0])));
  226. return verify_case(casenum__, expected__, received__, clock()-start__);
  227. }
  228. case 5: {
  229. int pieces[] = {3,3,1};
  230. string expected__ = "CAKE 0 PIECES 1";
  231.  
  232. clock_t start__ = clock();
  233. string received__ = CakeParty().makeMove(vector <int>(pieces, pieces + (sizeof pieces / sizeof pieces[0])));
  234. return verify_case(casenum__, expected__, received__, clock()-start__);
  235. }
  236. case 6: {
  237. int pieces[] = {4,7,4,7,4,7,4,7,47,47,47,47};
  238. string expected__ = "CAKE 10 PIECES 1";
  239.  
  240. clock_t start__ = clock();
  241. string received__ = CakeParty().makeMove(vector <int>(pieces, pieces + (sizeof pieces / sizeof pieces[0])));
  242. return verify_case(casenum__, expected__, received__, clock()-start__);
  243. }
  244.  
  245. // custom cases
  246.  
  247. /* case 7: {
  248. int pieces[] = ;
  249. string expected__ = ;
  250.  
  251. clock_t start__ = clock();
  252. string received__ = CakeParty().makeMove(vector <int>(pieces, pieces + (sizeof pieces / sizeof pieces[0])));
  253. return verify_case(casenum__, expected__, received__, clock()-start__);
  254. }*/
  255. /* case 8: {
  256. int pieces[] = ;
  257. string expected__ = ;
  258.  
  259. clock_t start__ = clock();
  260. string received__ = CakeParty().makeMove(vector <int>(pieces, pieces + (sizeof pieces / sizeof pieces[0])));
  261. return verify_case(casenum__, expected__, received__, clock()-start__);
  262. }*/
  263. /* case 9: {
  264. int pieces[] = ;
  265. string expected__ = ;
  266.  
  267. clock_t start__ = clock();
  268. string received__ = CakeParty().makeMove(vector <int>(pieces, pieces + (sizeof pieces / sizeof pieces[0])));
  269. return verify_case(casenum__, expected__, received__, clock()-start__);
  270. }*/
  271. default:
  272. return -1;
  273. }
  274. }
  275. }
  276.  
  277.  
  278. int main(int argc, char *argv[]) {
  279. if (argc == 1) {
  280. moj_harness::run_test();
  281. } else {
  282. for (int i=1; i<argc; ++i)
  283. moj_harness::run_test(atoi(argv[i]));
  284. }
  285. }
  286. // END CUT HERE
  287.  
Success #stdin #stdout 0.02s 2868KB
stdin
Standard input is empty
stdout
Standard output is empty