Skip to content

Commit 6e97ad9

Browse files
committed
srm 641 div 2 900
1 parent 24b07de commit 6e97ad9

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

ShufflingCardsDiv2.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <iostream>
2+
#include <sstream>
3+
#include <vector>
4+
#include <algorithm>
5+
#include <set>
6+
#include <map>
7+
#include <queue>
8+
#include <cstring>
9+
#include <climits>
10+
#include <cstdio>
11+
using namespace std;
12+
typedef pair<int,int> pi;
13+
typedef vector<int> vi;
14+
typedef vector<vi> vvi;
15+
typedef vector<string> vs;
16+
typedef vector<vs> vvs;
17+
18+
class ShufflingCardsDiv2{
19+
public:
20+
string shuffle(vector <int> p) {
21+
int n = p.size();
22+
n /= 2;
23+
vi c, d;
24+
for(int i = 0; i < 2*n && i+1 < 2*n; i = i+2){
25+
c.push_back(p[i]);
26+
d.push_back(p[i+1]);
27+
}
28+
int n1 = n/2 + n%2, n2 = n/2;
29+
int a = 0, b = 0;
30+
for(int k : c){
31+
if(k <= n) a++;
32+
else b++;
33+
}
34+
cout<<a<<" "<<b<<endl;
35+
if(a == n1 && b == n2) return "Possible";
36+
return "Impossible";
37+
}
38+
39+
// BEGIN CUT HERE
40+
public:
41+
void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); }
42+
private:
43+
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
44+
void verify_case(int Case, const string &Expected, const string &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
45+
void test_case_0() { int Arr0[] = {1,2,3,4}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "Possible"; verify_case(0, Arg1, shuffle(Arg0)); }
46+
void test_case_1() { int Arr0[] = {4,3,2,1}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "Possible"; verify_case(1, Arg1, shuffle(Arg0)); }
47+
void test_case_2() { int Arr0[] = {1,3,2,4}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "Impossible"; verify_case(2, Arg1, shuffle(Arg0)); }
48+
void test_case_3() { int Arr0[] = {1,4,2,5,3,6}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "Impossible"; verify_case(3, Arg1, shuffle(Arg0)); }
49+
void test_case_4() { int Arr0[] = {8,5,4,9,1,7,6,10,3,2}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "Possible"; verify_case(4, Arg1, shuffle(Arg0)); }
50+
51+
// END CUT HERE
52+
53+
};
54+
55+
// BEGIN CUT HERE
56+
int main(){
57+
58+
ShufflingCardsDiv2 ___test;
59+
___test.run_test(-1);
60+
}
61+
// END CUT HERE

ShufflingCardsDiv2.html

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<html><body bgcolor="#000000" text="#ffffff"><table><tr><td colspan="2"><h3>Problem Statement</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td>Fox Ciel likes shuffling cards.
2+
She uses a deck with 2N cards, numbered 1 through 2N.
3+
<br></br>
4+
Ciel always uses the same procedure when shuffling.
5+
One round of shuffling looks as follows:
6+
<ol>
7+
<li>She splits the deck into two piles: the top N cards will be pile A, the bottom N cards pile B.</li>
8+
<li>She takes pile A and rearranges the cards it contains arbitrarily.</li>
9+
<li>She takes pile B and rearranges the cards it contains arbitrarily.</li>
10+
<li>She interleaves the cards from the two piles, producing a single deck again. More precisely, if pile A has cards A1,A2,...,AN and pile B has cards B1,B2,...,BN then the new deck will be A1,B1,A2,B2,...,AN,BN. (Note that the first card has to come from pile A.)</li>
11+
</ol>
12+
13+
For example, let N=2 and suppose that Ciel starts with the sorted deck 1,2,3,4.
14+
One possible round of shuffling looks as follows:
15+
<ol>
16+
<li>She splits the deck into two piles: the cards 1,2 are pile A and the cards 3,4 are pile B.</li>
17+
<li>She rearranges pile A into 1,2. (I.e., she keeps the cards in their current order.)</li>
18+
<li>She rearranges pile B into 4,3.</li>
19+
<li>She merges the two piles, obtaining the deck 1,4,2,3.</li>
20+
</ol>
21+
22+
In the above example we have shown one of four possible outcomes of the shuffling process.
23+
After the first round of shuffling, Ciel could have that deck in one of these four orders:
24+
<ul>
25+
<li>1,3,2,4</li>
26+
<li>1,4,2,3</li>
27+
<li>2,3,1,4</li>
28+
<li>2,4,1,3</li>
29+
</ul>
30+
31+
You are given a vector &lt;int&gt; <b>permutation</b> which contains a permutation of the 2N cards.
32+
Ciel's deck is currently sorted: the cards are in the order 1,2,3,...,2N from top to bottom.
33+
Ciel wants to make exactly two rounds of shuffling.
34+
After the second round the order of cards in her deck should correspond to the given permutation.
35+
Return "Possible" (quotes for clarity) if this can be done and "Impossible" otherwise.</td></tr><tr><td colspan="2"><h3>Definition</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td>Class:</td><td>ShufflingCardsDiv2</td></tr><tr><td>Method:</td><td>shuffle</td></tr><tr><td>Parameters:</td><td>vector &lt;int&gt;</td></tr><tr><td>Returns:</td><td>string</td></tr><tr><td>Method signature:</td><td>string shuffle(vector &lt;int&gt; permutation)</td></tr><tr><td colspan="2">(be sure your method is public)</td></tr></table></td></tr><tr><td colspan="2"><h3>Limits</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td>Time limit (s):</td><td>2.000</td></tr><tr><td>Memory limit (MB):</td><td>256</td></tr><tr><td>Stack limit (MB):</td><td>256</td></tr></table></td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>permutation</b> will contain between 4 and 200 elements, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>The number of elements in <b>permutation</b> will be even.</td></tr><tr><td align="center" valign="top">-</td><td>The elements of <b>permutation</b> will form a permutation of the numbers 1 through 2N, where 2N is the number of elements in <b>permutation</b>.</td></tr><tr><td colspan="2"><h3>Examples</h3></td></tr><tr><td align="center" nowrap="true">0)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{1,2,3,4}</pre></td></tr></table></td></tr><tr><td><pre>Returns: &quot;Possible&quot;</pre></td></tr><tr><td><table><tr><td colspan="2">Fox Ciel can make the following two shuffles: {1,2,3,4} -> {1,3,2,4} -> {1,2,3,4}.
36+
<br></br>
37+
Note that she cannot simply keep the deck in sorted order, the shuffling procedure does not allow that.
38+
Luckily for Ciel, it is possible to shuffle the deck in the first round and to return the cards to their original places in the second round.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">1)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{4,3,2,1}</pre></td></tr></table></td></tr><tr><td><pre>Returns: &quot;Possible&quot;</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">2)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{1,3,2,4}</pre></td></tr></table></td></tr><tr><td><pre>Returns: &quot;Impossible&quot;</pre></td></tr><tr><td><table><tr><td colspan="2">Ciel can produce this permutation after the first round of shuffling.
39+
However, it is not possible to start with a sorted deck and to have this permutation of cards after two rounds of shuffling.
40+
</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">3)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{1,4,2,5,3,6}</pre></td></tr></table></td></tr><tr><td><pre>Returns: &quot;Impossible&quot;</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">4)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{8,5,4,9,1,7,6,10,3,2}</pre></td></tr></table></td></tr><tr><td><pre>Returns: &quot;Possible&quot;</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr></table><p>This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved. </p></body></html>

0 commit comments

Comments
 (0)