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  » Codechef Campus Snackdown » Alternating Permutations » All Submissions » haymakers [136779]
0
Your rating: None
CodeChef submission 136779 (C)

CodeChef submission 136779 (C) plaintext list. Status: CE, problem SNCK03, contest SNACKDWN. By haymakers (haymakers), 2009-11-21 23:56:45.
  1. /*
  2. * perm.cpp
  3. *
  4. *
  5. * Created by Mayank Jaiswal on 21/11/09.
  6. * Copyright 2009 IIT Kharagpur. All rights reserved.
  7. *
  8. */
  9. using namespace std;
  10. //using namespace stdcomb;
  11. #include<iostream>
  12. #include<algorithm>
  13. #include<vector>
  14. #include<set>
  15.  
  16. #define DEBUG false
  17.  
  18.  
  19. void print(set<int> s)
  20. {
  21. for(set<int> :: iterator p= s.begin(); p!= s.end() ; p++)
  22. if(DEBUG) cout<< *p<< " ";
  23. if(DEBUG) cout<< endl;
  24. }
  25. void print(vector<int> s)
  26. {
  27. for(int i=0; i<s.size(); i++)
  28. if(DEBUG) cout<< s[i]<< " ";
  29. if(DEBUG) cout<< endl;
  30. }
  31.  
  32.  
  33. //=======================================================
  34. // combination.h
  35. // Description : Template class to find combinations
  36. //=======================================================
  37. // Copyright 2003 - 2006 Wong Shao Voon
  38. // No warranty, implied or expressed, is included.
  39. // Author is not liable for any type of loss through
  40. // the use of this source code. Use it at your own risk!
  41. //=======================================================
  42. #ifndef __COMBINATION_H__
  43. #define __COMBINATION_H__
  44. namespace stdcomb
  45. {
  46. // Non recursive template function
  47. template <class BidIt>
  48. inline bool next_combination(BidIt n_begin, BidIt n_end,
  49. BidIt r_begin, BidIt r_end)
  50. {
  51. bool boolmarked=false;
  52. BidIt r_marked;
  53. BidIt n_it1=n_end;
  54. --n_it1;
  55. BidIt tmp_r_end=r_end;
  56. --tmp_r_end;
  57. for(BidIt r_it1=tmp_r_end; r_it1!=r_begin || r_it1==r_begin; --r_it1,--n_it1)
  58. {
  59. if(*r_it1==*n_it1 )
  60. {
  61. if(r_it1!=r_begin) //to ensure not at the start of r sequence
  62. {
  63. boolmarked=true;
  64. r_marked=(--r_it1);
  65. ++r_it1;//add it back again
  66. continue;
  67. }
  68. else // it means it is at the start the sequence, so return false
  69. return false;
  70. }
  71. else //if(*r_it1!=*n_it1 )
  72. {
  73. //marked code
  74. if(boolmarked==true)
  75. {
  76. //for loop to find which marked is in the first sequence
  77. BidIt n_marked;//mark in first sequence
  78. for (BidIt n_it2=n_begin;n_it2!=n_end;++n_it2)
  79. if(*r_marked==*n_it2) {n_marked=n_it2;break;}
  80. BidIt n_it3=++n_marked;
  81. for (BidIt r_it2=r_marked;r_it2!=r_end;++r_it2,++n_it3)
  82. {
  83. *r_it2=*n_it3;
  84. }
  85. return true;
  86. }
  87. for(BidIt n_it4=n_begin; n_it4!=n_end; ++n_it4)
  88. if(*r_it1==*n_it4)
  89. {
  90. *r_it1=*(++n_it4);
  91. return true;
  92. }
  93. }
  94. }
  95. return true;//will never reach here
  96. }
  97. // Non recursive template function with Pred
  98. template <class BidIt, class Prediate>
  99. inline bool next_combination(
  100. BidIt n_begin,
  101. BidIt n_end,
  102. BidIt r_begin,
  103. BidIt r_end,
  104. Prediate Equal)
  105. {
  106. bool boolmarked=false;
  107. BidIt r_marked;
  108. BidIt n_it1=n_end;
  109. --n_it1;
  110. BidIt tmp_r_end=r_end;
  111. --tmp_r_end;
  112. for(BidIt r_it1=tmp_r_end; r_it1!=r_begin || r_it1==r_begin; --r_it1,--n_it1)
  113. {
  114. if( Equal( *r_it1, *n_it1) )
  115. {
  116. if(r_it1!=r_begin) //to ensure not at the start of r sequence
  117. {
  118. boolmarked=true;
  119. r_marked=(--r_it1);
  120. ++r_it1;//add it back again
  121. continue;
  122. }
  123. else // it means it is at the start the sequence, so return false
  124. return false;
  125. }
  126. else //if(*r_it1!=*n_it1 )
  127. {
  128. //marked code
  129. if(boolmarked==true)
  130. {
  131. //for loop to find which marked is in the first sequence
  132. BidIt n_marked;//mark in first sequence
  133. for (BidIt n_it2=n_begin;n_it2!=n_end;++n_it2)
  134. if( Equal( *r_marked, *n_it2) ) {n_marked=n_it2;break;}
  135. BidIt n_it3=++n_marked;
  136. for (BidIt r_it2=r_marked;r_it2!=r_end;++r_it2,++n_it3)
  137. {
  138. *r_it2=*n_it3;
  139. }
  140. return true;
  141. }
  142. for(BidIt n_it4=n_begin; n_it4!=n_end; ++n_it4)
  143. if( Equal(*r_it1, *n_it4) )
  144. {
  145. *r_it1=*(++n_it4);
  146. return true;
  147. }
  148. }
  149. }
  150. return true;//will never reach here
  151. }
  152. // Non recursive template function
  153. template <class BidIt>
  154. inline bool prev_combination(BidIt n_begin, BidIt n_end,
  155. BidIt r_begin, BidIt r_end)
  156. {
  157. bool boolsame=false;
  158. BidIt marked;//for r
  159. BidIt r_marked;
  160. BidIt n_marked;
  161. BidIt tmp_n_end=n_end;
  162. --tmp_n_end;
  163. BidIt r_it1=r_end;
  164. --r_it1;
  165. for(BidIt n_it1=tmp_n_end; n_it1!=n_begin || n_it1==n_begin ; --n_it1)
  166. {
  167. if(*r_it1==*n_it1)
  168. {
  169. r_marked=r_it1;
  170. n_marked=n_it1;
  171. break;
  172. }
  173. }
  174. BidIt n_it2=n_marked;
  175. BidIt tmp_r_end=r_end;
  176. --tmp_r_end;
  177. for(BidIt r_it2=r_marked; r_it2!=r_begin || r_it2==r_begin; --r_it2,--n_it2)
  178. {
  179. if(*r_it2==*n_it2 )
  180. {
  181. if(r_it2==r_begin&& !(*r_it2==*n_begin) )
  182. {
  183. for(BidIt n_it3=n_begin;n_it3!=n_end;++n_it3)
  184. {
  185. if(*r_it2==*n_it3)
  186. {
  187. marked=r_it2;
  188. *r_it2=*(--n_it3);
  189. BidIt n_it4=n_end;
  190. --n_it4;
  191. for(BidIt r_it3=tmp_r_end; (r_it3!=r_begin || r_it3==r_begin) &&r_it3!=marked; --r_it3,--n_it4)
  192. {
  193. *r_it3=*n_it4;
  194. }
  195. return true;
  196. }
  197. }
  198. }
  199. else if(r_it2==r_begin&&*r_it2==*n_begin)
  200. {
  201. return false;//no more previous combination;
  202. }
  203. }
  204. else //if(*r_it2!=*n_it2 )
  205. {
  206. ++r_it2;
  207. marked=r_it2;
  208. for(BidIt n_it5=n_begin;n_it5!=n_end;++n_it5)
  209. {
  210. if(*r_it2==*n_it5)
  211. {
  212. *r_it2=*(--n_it5);
  213. BidIt n_it6=n_end;
  214. --n_it6;
  215. for(BidIt r_it4=tmp_r_end; (r_it4!=r_begin || r_it4==r_begin) &&r_it4!=marked; --r_it4,--n_it6)
  216. {
  217. *r_it4=*n_it6;
  218. }
  219. return true;
  220. }
  221. }
  222. }
  223. }
  224. return false;//Will never reach here, unless error
  225. }
  226. // Non recursive template function with Pred
  227. template <class BidIt, class Prediate>
  228. inline bool prev_combination(
  229. BidIt n_begin,
  230. BidIt n_end,
  231. BidIt r_begin,
  232. BidIt r_end,
  233. Prediate Equal)
  234. {
  235. bool boolsame=false;
  236. BidIt marked;//for r
  237. BidIt r_marked;
  238. BidIt n_marked;
  239. BidIt tmp_n_end=n_end;
  240. --tmp_n_end;
  241. BidIt r_it1=r_end;
  242. --r_it1;
  243. for(BidIt n_it1=tmp_n_end; n_it1!=n_begin || n_it1==n_begin ; --n_it1)
  244. {
  245. if( Equal(*r_it1, *n_it1) )
  246. {
  247. r_marked=r_it1;
  248. n_marked=n_it1;
  249. break;
  250. }
  251. }
  252. BidIt n_it2=n_marked;
  253. BidIt tmp_r_end=r_end;
  254. --tmp_r_end;
  255. for(BidIt r_it2=r_marked; r_it2!=r_begin || r_it2==r_begin; --r_it2,--n_it2)
  256. {
  257. if( Equal(*r_it2, *n_it2) )
  258. {
  259. if(r_it2==r_begin&& !Equal(*r_it2, *n_begin) )
  260. {
  261. for(BidIt n_it3=n_begin;n_it3!=n_end;++n_it3)
  262. {
  263. if(Equal(*r_it2, *n_it3))
  264. {
  265. marked=r_it2;
  266. *r_it2=*(--n_it3);
  267. BidIt n_it4=n_end;
  268. --n_it4;
  269. for(BidIt r_it3=tmp_r_end; (r_it3!=r_begin || r_it3==r_begin) &&r_it3!=marked; --r_it3,--n_it4)
  270. {
  271. *r_it3=*n_it4;
  272. }
  273. return true;
  274. }
  275. }
  276. }
  277. else if(r_it2==r_begin&&Equal(*r_it2, *n_begin))
  278. {
  279. return false;//no more previous combination;
  280. }
  281. }
  282. else //if(*r_it2!=*n_it2 )
  283. {
  284. ++r_it2;
  285. marked=r_it2;
  286. for(BidIt n_it5=n_begin;n_it5!=n_end;++n_it5)
  287. {
  288. if(Equal(*r_it2, *n_it5))
  289. {
  290. *r_it2=*(--n_it5);
  291. BidIt n_it6=n_end;
  292. --n_it6;
  293. for(BidIt r_it4=tmp_r_end; (r_it4!=r_begin || r_it4==r_begin) &&r_it4!=marked; --r_it4,--n_it6)
  294. {
  295. *r_it4=*n_it6;
  296. }
  297. return true;
  298. }
  299. }
  300. }
  301. }
  302. return false;//Will never reach here, unless error
  303. }
  304. // Recursive template function
  305. template <class RanIt, class Func>
  306. void recursive_combination(RanIt nbegin, RanIt nend, int n_column,
  307. RanIt rbegin, RanIt rend, int r_column,int loop, Func func)
  308. {
  309. int r_size=rend-rbegin;
  310. int localloop=loop;
  311. int local_n_column=n_column;
  312. //A different combination is out
  313. if(r_column>(r_size-1))
  314. {
  315. func(rbegin,rend);
  316. return;
  317. }
  318. /////////////////////////////////
  319. for(int i=0;i<=loop;++i)
  320. {
  321. RanIt it1=rbegin;
  322. for(int cnt=0;cnt<r_column;++cnt)
  323. {
  324. ++it1;
  325. }
  326. RanIt it2=nbegin;
  327. for(int cnt2=0;cnt2<n_column+i;++cnt2)
  328. {
  329. ++it2;
  330. }
  331. *it1=*it2;
  332. ++local_n_column;
  333. recursive_combination(nbegin,nend,local_n_column,
  334. rbegin,rend,r_column+1,localloop,func);
  335. --localloop;
  336. }
  337. }
  338. }
  339. #endif
  340.  
  341.  
  342. // for use with next_combination examples!
  343. template<class BidIt>
  344. void display(BidIt begin,BidIt end)
  345. {
  346. for (BidIt it=begin;it!=end;++it)
  347. if(DEBUG) cout<<*it<<" ";
  348. if(DEBUG) cout<<endl;
  349. }
  350.  
  351.  
  352.  
  353.  
  354. int perm(int cse, vector<int> list , int prev, vector<int> B )
  355. {
  356. int count=0;
  357. if(DEBUG) printf("\nIn Permutation: ");
  358. if(DEBUG) cout<< " list: ";
  359. for (int i=0; i<list.size(); i++)
  360. if(DEBUG) cout << list[i]<<" ";
  361. if(DEBUG) cout<< " B: ";
  362. for (int i=0; i<B.size(); i++)
  363. if(DEBUG) cout << B[i]<<" ";
  364. if(DEBUG) printf("cse =%d, prev= %d",cse, prev) ;
  365. if(DEBUG) printf(" B[size]-1=%d\n", B.size()-1);
  366. if(cse==B.size())
  367. {
  368. if(DEBUG) printf("cse==B.size()\n");
  369. return 1;
  370. }
  371. int dec= cse%2;
  372. if(dec==0) // increasing
  373. {
  374. if(DEBUG) printf("in increasing \n");
  375. /* //inserting in set
  376. set<int> listset;
  377. for (int i=0; i<list.size(); i++)
  378. listset.insert(list[i]);
  379. if(DEBUG) cout<< "set: ";
  380. for (set<int> :: iterator p = listset.begin(); p!= listset.end(); p++)
  381. if(DEBUG) cout << *p<<" ";
  382. if(DEBUG) cout << endl;
  383. */
  384. vector <int> smallListOptions;
  385. for (int i=0; i<list.size(); i++)
  386. if(list[i]>prev)
  387. smallListOptions.push_back(list[i]);
  388. if(smallListOptions.size() < B[cse]-1)
  389. return 0;
  390. vector<int> smallList;
  391. for(int i=0; i<B[cse]-1;i++)
  392. smallList.push_back(smallListOptions[i]);
  393. if(DEBUG) printf("small list :");
  394. for(int i=0; i<smallList.size();i++)
  395. if(DEBUG) cout<< smallList[i]<< " ";
  396. if(DEBUG) cout<<endl;
  397. do
  398. {
  399. if(DEBUG) printf("Combinations:");
  400. display(smallList.begin(),smallList.end());
  401.  
  402.  
  403. set<int> listset;
  404. for (int i=0; i<list.size(); i++)
  405. listset.insert(list[i]);
  406. for (int i=0; i<smallList.size(); i++)
  407. listset.erase(smallList[i]);
  408. vector<int> restlist;
  409. for (set<int> :: iterator p = listset.begin(); p!= listset.end(); p++)
  410. restlist.push_back(*p);
  411. if(DEBUG) printf("restlist: ");
  412. for (int i=0; i<restlist.size(); i++)
  413. if(DEBUG) cout<< restlist[i]<< " ";
  414. if(DEBUG) cout << endl;
  415.  
  416. count += perm(cse+1, restlist, smallList[smallList.size()-1],B);
  417. // int perm(int cse, vector<int> list , int prev, vector<int> B )
  418.  
  419. //******************************
  420.  
  421. }
  422. while(stdcomb::next_combination(smallListOptions.begin (),smallListOptions.end (),smallList.begin (),smallList.end()) );
  423. return count;
  424. }
  425. else if(dec==1) //decreasing
  426. {
  427. if(DEBUG) printf("in decreasing \n");
  428. vector <int> smallListOptions;
  429. for (int i=0; i<list.size(); i++)
  430. if(list[i]<prev)
  431. smallListOptions.push_back(list[i]);
  432.  
  433. if(DEBUG) printf("small list :");
  434. print(smallListOptions);
  435. if(smallListOptions.size() < B[cse]-1)
  436. return 0;
  437. vector<int> smallList;
  438. for(int i=0; i<B[cse]-1;i++)
  439. smallList.push_back(smallListOptions[i]);
  440. if(DEBUG) printf("small list :");
  441. for(int i=0; i<smallList.size();i++)
  442. if(DEBUG) cout<< smallList[i]<< " ";
  443. if(DEBUG) cout<<endl;
  444. do
  445. {
  446. if(DEBUG) printf("Combinations:");
  447. display(smallList.begin(),smallList.end());
  448. set<int> listset;
  449. if(list.size()!=0)
  450. for (int i=0; i<list.size(); i++)
  451. listset.insert(list[i]);
  452. if(DEBUG) printf("listset : ");
  453. print(listset);
  454. if(smallList.size()!=0)
  455. for (int i=0; i<smallList.size(); i++)
  456. listset.erase(smallList[i]);
  457. if(DEBUG) printf("smallList : ");
  458. print(smallList);
  459. vector<int> restlist;
  460. for (set<int> :: iterator p = listset.begin(); p!= listset.end(); p++)
  461. restlist.push_back(*p);
  462. if(DEBUG) printf("restlist : ");
  463. print(restlist);
  464.  
  465. count += perm(cse+1, restlist, smallList[0],B);
  466. // int perm(int cse, vector<int> list , int prev, vector<int> B )
  467. //******************************
  468. }
  469. while(stdcomb::next_combination(smallListOptions.begin (),smallListOptions.end (),smallList.begin (),smallList.end()) );
  470. return count;
  471. }
  472.  
  473. }
  474.  
  475.  
  476.  
  477. int main()
  478. {
  479. if(DEBUG) printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
  480.  
  481. int T;
  482. cin>> T;
  483. for(int test=1; test<=T ; test++ )
  484. {
  485. int N;
  486. int K;
  487. cin >> N;
  488. cin >> K;
  489. vector<int> list ;
  490. //input list
  491. for (int i=0; i<K; i++)
  492. {
  493. int temp;
  494. cin>>temp;
  495. list.push_back(temp);
  496. }
  497. for (int i=0; i<list.size(); i++)
  498. if(DEBUG) printf("list[%d]=%d\n",i,list[i]);
  499.  
  500. //creating B
  501. vector<int> B;
  502. for(int i=0; i<K-1; i++)
  503. B.push_back(list[i+1]-list[i]+1);
  504. for (int i=0; i<B.size(); i++)
  505. if(DEBUG) printf("B[%d]=%d\n",i,B[i]);
  506.  
  507. // for (int i=1; i<=N; i++)
  508. // list.push_back(i);
  509. int count=0;
  510. for (int i=1; i<=N; i++)
  511. {
  512. list.erase (list.begin(),list.end());
  513. for (int j=1; j<=N; j++)
  514. {
  515. if(j!=i)
  516. list.push_back(j);
  517. }
  518. count += perm(0,list,i ,B);
  519. if(DEBUG) printf("COUNT=%d\n",count);
  520. }
  521. // printf("Case #%2d :%d\n\n\n",test, count);
  522. printf("%d\n", count);
  523. }
  524. return 0;
  525. }
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.  
  533.  
  534. //
  535. // perm(2,list,4);


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