Skip to content

Commit abefc45

Browse files
committed
srm 631 div 2 500
1 parent e3d81fe commit abefc45

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed

CatsOnTheLineDiv2.cpp

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
using namespace std;
11+
typedef pair<int,int> pi;
12+
typedef vector<int> vi;
13+
typedef vector<vi> vvi;
14+
typedef vector<string> vs;
15+
typedef vector<vs> vvs;
16+
17+
class CatsOnTheLineDiv2 {
18+
public:
19+
string getAnswer(vector <int> p, vector <int> c, int t)
20+
{
21+
int n = p.size();
22+
for(int c1 : c) if(2*t+1 < c1) return "Impossible";
23+
24+
vector<pi> v;
25+
for(int i = 0; i < n; i++)
26+
v.push_back(make_pair(p[i], c[i]));
27+
28+
sort(v.begin(), v.end());
29+
30+
for(int i = 0; i < n; i++){
31+
p[i] = v[i].first;
32+
c[i] = v[i].second;
33+
}
34+
35+
int x = p[0]-t, y = x+c[0]-1;
36+
37+
for(int i = 1; i < n; i++){
38+
int p1 = p[i], c1 = c[i];
39+
x = max(y+1, p1-t);
40+
y = x+c1-1;
41+
if(x > p1+t || y > p1+t) return "Impossible";
42+
}
43+
44+
return "Possible";
45+
}
46+
47+
// BEGIN CUT HERE
48+
public:
49+
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(); }
50+
private:
51+
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(); }
52+
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; } }
53+
void test_case_0() { int Arr0[] = {0}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {7}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 3; string Arg3 = "Possible"; verify_case(0, Arg3, getAnswer(Arg0, Arg1, Arg2)); }
54+
void test_case_1() { int Arr0[] = {0}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {8}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 2; string Arg3 = "Impossible"; verify_case(1, Arg3, getAnswer(Arg0, Arg1, Arg2)); }
55+
void test_case_2() { int Arr0[] = {0, 1}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {3, 1}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 0; string Arg3 = "Impossible"; verify_case(2, Arg3, getAnswer(Arg0, Arg1, Arg2)); }
56+
void test_case_3() { int Arr0[] = {5, 0, 2}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {2, 3, 5}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 2; string Arg3 = "Impossible"; verify_case(3, Arg3, getAnswer(Arg0, Arg1, Arg2)); }
57+
void test_case_4() { int Arr0[] = {5, 1, -10, 7, 12, 2, 10, 20}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {3, 4, 2, 7, 1, 4, 3, 4}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 6; string Arg3 = "Possible"; verify_case(4, Arg3, getAnswer(Arg0, Arg1, Arg2)); }
58+
59+
// END CUT HERE
60+
61+
};
62+
63+
// BEGIN CUT HERE
64+
int main(){
65+
66+
CatsOnTheLineDiv2 ___test;
67+
___test.run_test(-1);
68+
69+
}
70+
// END CUT HERE

CatsOnTheLineDiv2.txt

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
PROBLEM STATEMENT
2+
3+
There are some cats sitting on a straight line that goes from the left to the right.
4+
You are given two vector <int>s position and count.
5+
For each valid i, there are count[i] cats initially sitting at the point position[i].
6+
7+
8+
9+
10+
During each minute, each cat chooses and performs one of three possible actions: it may stay in its place, move one unit to the left (i.e., from x to x-1), or move one unit to the right (i.e., from x to x+1).
11+
(Note that there are no restrictions. In particular, different cats that are currently at the same point may make different choices.)
12+
13+
14+
15+
16+
You are also given an int time.
17+
The goal is to rearrange the cats in such a way that each point contains at most one cat.
18+
Return "Possible" if it's possible to achive the goal in time minutes, and "Impossible" otherwise (quotes for clarity).
19+
20+
21+
DEFINITION
22+
Class:CatsOnTheLineDiv2
23+
Method:getAnswer
24+
Parameters:vector <int>, vector <int>, int
25+
Returns:string
26+
Method signature:string getAnswer(vector <int> position, vector <int> count, int time)
27+
28+
29+
CONSTRAINTS
30+
-position will contain between 1 and 50 elements, inclusive.
31+
-position and count will contain the same number of elements.
32+
-Each element of position will be between -1000 and 1000, inclusive.
33+
-All elements of position will be distinct.
34+
-Each element of count will be between 1 and 1000, inclusive.
35+
-time will be between 0 and 1000, inclusive.
36+
37+
38+
EXAMPLES
39+
40+
0)
41+
{0}
42+
{7}
43+
3
44+
45+
Returns: "Possible"
46+
47+
There are 7 cats sitting at the origin in this case. There are also 7 different points that cats can reach in 3 minutes, so each cat can occupy a unique point. Thus, the answer is "Possible".
48+
49+
1)
50+
{0}
51+
{8}
52+
2
53+
54+
Returns: "Impossible"
55+
56+
Unlike the first test case, in this case there are 8 cats for 7 available points. Thus, the answer is "Impossible".
57+
58+
2)
59+
{0, 1}
60+
{3, 1}
61+
0
62+
63+
Returns: "Impossible"
64+
65+
66+
67+
3)
68+
{5, 0, 2}
69+
{2, 3, 5}
70+
2
71+
72+
Returns: "Impossible"
73+
74+
75+
76+
4)
77+
{5, 1, -10, 7, 12, 2, 10, 20}
78+
{3, 4, 2, 7, 1, 4, 3, 4}
79+
6
80+
81+
Returns: "Possible"
82+
83+

0 commit comments

Comments
 (0)