Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
382 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#include <sstream> | ||
#include <string> | ||
#include <vector> | ||
#include <map> | ||
#include <algorithm> | ||
#include <iostream> | ||
#include <cstdio> | ||
#include <cstdlib> | ||
#include <cmath> | ||
#include <utility> | ||
#include <set> | ||
#include <cctype> | ||
#include <queue> | ||
#include <stack> | ||
#include <numeric> | ||
using namespace std; | ||
class GirlsAndBoys { | ||
public: | ||
int sortThem(string row) { | ||
int ret = 0; | ||
int i, n = row.size(); | ||
|
||
int counter = 0; | ||
string tmp = row; | ||
while (1) { | ||
int localc = 0; | ||
|
||
for (i=0; i<n-1; i++) | ||
if (tmp[i] == 'B' && tmp[i+1] == 'G') { | ||
char c = tmp[i]; | ||
tmp [i] = tmp[i+1]; | ||
tmp[i+1] = c; | ||
localc++; | ||
} | ||
|
||
if (localc == 0) break; | ||
counter += localc; | ||
} | ||
|
||
ret = counter; | ||
|
||
tmp = row; | ||
counter = 0; | ||
while (1) { | ||
int localc = 0; | ||
|
||
for (i=0; i<n-1; i++) | ||
if (tmp[i] == 'G' && tmp[i+1] == 'B') { | ||
char c = tmp[i]; | ||
tmp [i] = tmp[i+1]; | ||
tmp[i+1] = c; | ||
localc++; | ||
} | ||
|
||
if (localc == 0) break; | ||
counter += localc; | ||
} | ||
|
||
ret = min(ret, counter); | ||
|
||
return ret; | ||
} | ||
|
||
|
||
}; | ||
|
||
|
||
// Powered by FileEdit | ||
// Powered by TZTester 1.01 [25-Feb-2003] | ||
// Powered by CodeProcessor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
// BEGIN CUT HERE | ||
// END CUT HERE | ||
#include <sstream> | ||
#include <string> | ||
#include <vector> | ||
#include <map> | ||
#include <algorithm> | ||
#include <iostream> | ||
#include <cstdio> | ||
#include <cstdlib> | ||
#include <cmath> | ||
#include <utility> | ||
#include <set> | ||
#include <cctype> | ||
#include <queue> | ||
#include <stack> | ||
#include <numeric> | ||
// BEGIN CUT HERE | ||
#include "cout.hpp" | ||
// END CUT HERE | ||
using namespace std; | ||
class RobotSimulation { | ||
public: | ||
int visited[500][500]; | ||
|
||
pair <int, int> travel(string program, int x, int y) { | ||
int i; | ||
int n = program.size(); | ||
|
||
for (i=0; i<n; i++) { | ||
if (program[i] == 'U') | ||
x += 1; | ||
else if (program[i] == 'D') | ||
x -= 1; | ||
else if (program[i] == 'L') | ||
y -= 1; | ||
else if (program[i] == 'R') | ||
y += 1; | ||
visited[x][y] = 1; | ||
} | ||
|
||
return make_pair(x, y); | ||
} | ||
|
||
int countVisited(void) { | ||
int ret = 0; | ||
int i, j; | ||
int n = 500; | ||
|
||
for (i=0; i<n; i++) | ||
for (j=0; j<n; j++) | ||
if (visited[i][j] == 1) | ||
ret++; | ||
|
||
return ret; | ||
} | ||
|
||
int cellsVisited(string program, int times) { | ||
int ret = 0; | ||
vector <int> counts; | ||
int i; | ||
|
||
memset(visited, 0, sizeof(visited)); | ||
|
||
visited[250][250] = 1; | ||
|
||
pair <int, int> res(make_pair(250, 250)); | ||
for (i=0; i<10; i++) { | ||
res = travel(program, res.first, res.second); | ||
int tmp = countVisited(); | ||
if (i > 0) | ||
tmp -= accumulate(counts.begin(), counts.end(), 0); | ||
counts.push_back(tmp); | ||
} | ||
|
||
for (i=0; i<min((int)counts.size(), times); i++) | ||
ret += counts[i]; | ||
|
||
int remain = times - counts.size(); | ||
if (remain >= 0) | ||
ret += counts[counts.size() - 1] * remain; | ||
|
||
return ret; | ||
} | ||
|
||
// BEGIN CUT HERE | ||
public: | ||
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(); | ||
if ((Case == -1) || (Case == 5)) test_case_5(); | ||
if ((Case == -1) || (Case == 6)) test_case_6(); | ||
if ((Case == -1) || (Case == 7)) test_case_7(); | ||
} | ||
private: | ||
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(); } | ||
void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } | ||
void test_case_0() { | ||
string Arg0 = "RRR"; | ||
int Arg1 = 100; | ||
int Arg2 = 301; | ||
verify_case(0, Arg2, cellsVisited(Arg0, Arg1)); } | ||
void test_case_1() { | ||
string Arg0 = "DDU"; | ||
int Arg1 = 100; | ||
int Arg2 = 102; | ||
verify_case(1, Arg2, cellsVisited(Arg0, Arg1)); } | ||
void test_case_2() { | ||
string Arg0 = "URLD"; | ||
int Arg1 = 100; | ||
int Arg2 = 3; | ||
verify_case(2, Arg2, cellsVisited(Arg0, Arg1)); } | ||
void test_case_3() { | ||
string Arg0 = "UUDUDDLLDR"; | ||
int Arg1 = 1; | ||
int Arg2 = 7; | ||
verify_case(3, Arg2, cellsVisited(Arg0, Arg1)); } | ||
void test_case_4() { | ||
string Arg0 = "UUDUDDLLDR"; | ||
int Arg1 = 12345678; | ||
int Arg2 = 37037039; | ||
verify_case(4, Arg2, cellsVisited(Arg0, Arg1)); } | ||
void test_case_5() { | ||
string Arg0 = "RRUUULLDD"; | ||
int Arg1 = 3603602; | ||
int Arg2 = 10810815; | ||
verify_case(5, Arg2, cellsVisited(Arg0, Arg1)); } | ||
void test_case_6() { | ||
string Arg0 = "RRR"; | ||
int Arg1 = 100; | ||
int Arg2 = 301; | ||
verify_case(6, Arg2, cellsVisited(Arg0, Arg1)); } | ||
void test_case_7() { | ||
string Arg0 = "RRRRRRRRRR"; | ||
int Arg1 = 200000000; | ||
int Arg2 = 301; | ||
verify_case(7, Arg2, cellsVisited(Arg0, Arg1)); } | ||
|
||
// END CUT HERE | ||
|
||
}; | ||
// BEGIN CUT HERE | ||
int main() { | ||
RobotSimulation ___test; | ||
___test.run_test(-1); | ||
} | ||
// END CUT HERE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
// BEGIN CUT HERE | ||
// END CUT HERE | ||
#include <sstream> | ||
#include <string> | ||
#include <vector> | ||
#include <map> | ||
#include <algorithm> | ||
#include <iostream> | ||
#include <cstdio> | ||
#include <cstdlib> | ||
#include <cmath> | ||
#include <utility> | ||
#include <set> | ||
#include <cctype> | ||
#include <queue> | ||
#include <stack> | ||
#include <numeric> | ||
// BEGIN CUT HERE | ||
#include "cout.hpp" | ||
// END CUT HERE | ||
using namespace std; | ||
class SequenceMerger { | ||
public: | ||
vector <vector <long long> >pseqs; | ||
|
||
vector <int> getVal(vector <string> seqs, vector <int> positions) { | ||
vector <int> ret; | ||
pseqs.clear(); | ||
int i, j; | ||
int n = seqs.size(), m = positions.size(); | ||
|
||
// parse | ||
for (i=0; i<n; i++) { | ||
istringstream sin(seqs[i]); | ||
char type; | ||
vector <long long> tmp; | ||
sin >> type; | ||
if (type == 'A' || type == 'G') { | ||
long long A, B, C; | ||
sin >> A >> B >> C; | ||
tmp.push_back(A), tmp.push_back(B), tmp.push_back(C); | ||
pseqs.push_back(tmp); | ||
} else { | ||
long long l; | ||
while (sin >> l) | ||
tmp.push_back(l); | ||
sort(tmp.begin(), tmp.end()); | ||
pseqs.push_back(tmp); | ||
} | ||
} | ||
|
||
// binary search | ||
for (i=0; i<m; i++) { | ||
int left = 0, right = 1000000000; | ||
while (left < right - 1) { | ||
int mid = (left + right) / 2; | ||
int counter = 0; | ||
for (j=0; j<n; j++) { | ||
if (seqs[j][0] == 'A') { | ||
long long x = (mid - pseqs[j][0]) / pseqs[j][1]; | ||
counter += min(x, pseqs[j][2]-1); | ||
} else if (seqs[j][0] == 'G') { | ||
long long x = log(mid / pseqs[j][0]) / log(pseqs[j][1]); | ||
counter += min(x, pseqs[j][2]-1); | ||
} else { | ||
int k = 0; | ||
while (pseqs[j][k] < mid) | ||
counter++, k++; | ||
} | ||
} | ||
if (counter >= positions[i]) | ||
right = mid; | ||
else | ||
left = mid; | ||
} | ||
if (right > 1000000000) ret.push_back(-1); | ||
else ret.push_back(right); | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
// BEGIN CUT HERE | ||
public: | ||
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(); | ||
if ((Case == -1) || (Case == 5)) test_case_5(); | ||
if ((Case == -1) || (Case == 6)) test_case_6(); | ||
} | ||
private: | ||
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(); } | ||
void verify_case(int Case, const vector <int> &Expected, const vector <int> &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } } | ||
void test_case_0() { | ||
string Arr0[] = {"E 1 1000000000 1000000001"}; | ||
vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); | ||
int Arr1[] = {1,2,3,4}; | ||
vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); | ||
int Arr2[] = {1, 1000000000, -1, -1 }; | ||
vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); | ||
verify_case(0, Arg2, getVal(Arg0, Arg1)); } | ||
void test_case_1() { | ||
string Arr0[] = {"A 1 1 10", "G 1 2 4"}; | ||
vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); | ||
int Arr1[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; | ||
vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); | ||
int Arr2[] = {1, 1, 2, 2, 3, 4, 4, 5, 6, 7, 8, 8, 9, 10, -1 }; | ||
vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); | ||
verify_case(1, Arg2, getVal(Arg0, Arg1)); } | ||
void test_case_2() { | ||
string Arr0[] = {"A 1000000000 1000000000 1000000000","G 100000000000000000 1000000000000 100000000000000", "E 1000000000000000 10000000 10 1111"}; | ||
vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); | ||
int Arr1[] = {1,2,3,4,5,6,7,8,9,10}; | ||
vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); | ||
int Arr2[] = {10, 1111, 10000000, 1000000000, -1, -1, -1, -1, -1, -1 }; | ||
vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); | ||
verify_case(2, Arg2, getVal(Arg0, Arg1)); } | ||
void test_case_3() { | ||
string Arr0[] = {"G 1 1 999999999", "E 2"}; | ||
vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); | ||
int Arr1[] = {1,999999999,1000000000}; | ||
vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); | ||
int Arr2[] = {1, 1, 2 }; | ||
vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); | ||
verify_case(3, Arg2, getVal(Arg0, Arg1)); } | ||
void test_case_4() { | ||
string Arr0[] = {"A 100 341 1524", "G 1 3 45235", "E 653 87 12341 8785 123 543"}; | ||
vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); | ||
int Arr1[] = {100000,1,10,10000,100,1000}; | ||
vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); | ||
int Arr2[] = {-1, 1, 441, -1, 28403, 334621 }; | ||
vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); | ||
verify_case(4, Arg2, getVal(Arg0, Arg1)); } | ||
void test_case_5() { | ||
string Arr0[] = {"E 1 1000000000 1000000001"}; | ||
vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); | ||
int Arr1[] = {1,2,3,4}; | ||
vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); | ||
int Arr2[] = {1, 1000000000, -1, -1 }; | ||
vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); | ||
verify_case(5, Arg2, getVal(Arg0, Arg1)); } | ||
void test_case_6() { | ||
string Arr0[] = {"E 1 1000000000 1000000001"}; | ||
vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); | ||
int Arr1[] = {1,2,3,4}; | ||
vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); | ||
int Arr2[] = {1, 1000000000, -1, -1 }; | ||
vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); | ||
verify_case(6, Arg2, getVal(Arg0, Arg1)); } | ||
|
||
// END CUT HERE | ||
|
||
}; | ||
// BEGIN CUT HERE | ||
int main() { | ||
SequenceMerger ___test; | ||
___test.run_test(-1); | ||
} | ||
// END CUT HERE |