Skip to content

Commit 069be80

Browse files
committed
add Code Jam solutions
1 parent 8c5a5ea commit 069be80

30 files changed

+1612
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << "{"; for (typename vector<T>::const_iterator vi = v.begin(); vi != v.end(); ++vi) { if (vi != v.begin()) os << ", "; os << *vi; } os << "}"; return os; }
5+
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { os << '(' << p.first << ", " << p.second << ')'; return os; }
6+
7+
typedef long long ll;
8+
typedef long double ld;
9+
typedef pair<int,int> pii;
10+
11+
#define optimize ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
12+
#define endl "\n"
13+
14+
#define fi first
15+
#define se second
16+
#define pb push_back
17+
18+
#define all(x) x.begin(),x.end()
19+
#define ms(x,a) memset(x,a,sizeof(x))
20+
21+
#define INF 0x3f3f3f3f
22+
#define INFLL 0x3f3f3f3f3f3f3f3f
23+
24+
#define mod 1000000007LL
25+
#define MAXN 1010
26+
27+
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
28+
29+
//#define _DEBUG
30+
// #ifdef _DEBUG
31+
// #endif
32+
33+
/* -------------------------------- Solution starts below -------------------------------- */
34+
//Subtasks 1 - AC
35+
//Subtasks 2 - AC
36+
//Subtasks 3 - AC
37+
38+
ll T,N,M,K,X,Y;
39+
string s;
40+
41+
int dp[MAXN][2], ok[MAXN][2];
42+
43+
ll solve(int i, bool b){
44+
45+
if(i == N-1) return 0;
46+
47+
ll ans = 0;
48+
49+
if(ok[i][b]) return dp[i][b];
50+
51+
if(s[i + 1] == 'C'){
52+
if(!b) ans = solve(i+1, 0);
53+
else ans = Y + solve(i+1, 0);
54+
}
55+
else if(s[i + 1] == 'J'){
56+
if(!b) ans = X + solve(i+1, 1);
57+
else ans = solve(i+1, 1);
58+
}
59+
else{
60+
if(!b) ans = min( solve(i+1, 0), X + solve(i+1, 1) );
61+
else ans = min( Y + solve(i+1, 0), solve(i+1, 1) );
62+
}
63+
64+
ok[i][b] = true;
65+
return dp[i][b] = ans;
66+
}
67+
68+
void solve(int caso){
69+
70+
ms(dp, 0);
71+
ms(ok, 0);
72+
73+
ll ans = INFLL;
74+
75+
if(s[0] == 'C'){
76+
ans = solve(0, 0);
77+
}
78+
else if(s[0] == 'J'){
79+
ans = solve(0, 1);
80+
}
81+
else{
82+
ans = min(ans, solve(0, 0));
83+
84+
ms(dp, 0);
85+
ms(ok, 0);
86+
87+
ans = min(ans, solve(0, 1));
88+
}
89+
90+
cout << "Case #" << caso << ": " << ans << endl;
91+
}
92+
93+
int main(){
94+
95+
optimize;
96+
97+
cin >> T;
98+
99+
for(int i=1;i<=T;i++){
100+
cin >> X >> Y >> s;
101+
102+
N = s.size();
103+
104+
solve(i);
105+
}
106+
107+
return 0;
108+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << "{"; for (typename vector<T>::const_iterator vi = v.begin(); vi != v.end(); ++vi) { if (vi != v.begin()) os << ", "; os << *vi; } os << "}"; return os; }
5+
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { os << '(' << p.first << ", " << p.second << ')'; return os; }
6+
7+
typedef long long ll;
8+
typedef long double ld;
9+
typedef pair<int,int> pii;
10+
11+
#define optimize ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
12+
#define endl "\n"
13+
14+
#define fi first
15+
#define se second
16+
#define pb push_back
17+
18+
#define all(x) x.begin(),x.end()
19+
#define ms(x,a) memset(x,a,sizeof(x))
20+
21+
#define INF 0x3f3f3f3f
22+
#define INFLL 0x3f3f3f3f3f3f3f3f
23+
24+
#define mod 1000000007LL
25+
#define MAXN 200010
26+
27+
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
28+
29+
//#define _DEBUG
30+
// #ifdef _DEBUG
31+
// #endif
32+
33+
/* -------------------------------- Solution starts below -------------------------------- */
34+
//Subtasks 1 - AC
35+
36+
ll T,N,M,K;
37+
int A[MAXN];
38+
39+
int ordem(int x){
40+
int p = x, cnt = 0;
41+
42+
for(int i=x+1;i<N;i++) if(A[p] > A[i]) p = i;
43+
44+
cnt = p - x + 1;
45+
46+
for(int i = x; i < p ; i++, p--) swap(A[i],A[p]);
47+
48+
return cnt;
49+
}
50+
51+
void solve(int caso){
52+
53+
int ans = 0;
54+
55+
for(int i=0;i<N-1;i++) {
56+
ans += ordem(i);
57+
}
58+
59+
cout << "Case #" << caso << ": " << ans << endl;
60+
}
61+
62+
int main(){
63+
64+
optimize;
65+
66+
cin >> T;
67+
68+
for(int i=1;i<=T;i++){
69+
cin >> N;
70+
71+
for(int j=0;j<N;j++) cin >> A[j];
72+
73+
solve(i);
74+
}
75+
76+
return 0;
77+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << "{"; for (typename vector<T>::const_iterator vi = v.begin(); vi != v.end(); ++vi) { if (vi != v.begin()) os << ", "; os << *vi; } os << "}"; return os; }
5+
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { os << '(' << p.first << ", " << p.second << ')'; return os; }
6+
7+
typedef long long ll;
8+
typedef long double ld;
9+
typedef pair<int,int> pii;
10+
11+
#define optimize ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
12+
#define endl "\n"
13+
14+
#define fi first
15+
#define se second
16+
#define pb push_back
17+
18+
#define all(x) x.begin(),x.end()
19+
#define ms(x,a) memset(x,a,sizeof(x))
20+
21+
#define INF 0x3f3f3f3f
22+
#define INFLL 0x3f3f3f3f3f3f3f3f
23+
24+
#define mod 1000000007LL
25+
#define MAXN 123
26+
27+
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
28+
29+
//#define _DEBUG
30+
// #ifdef _DEBUG
31+
// #endif
32+
33+
/* -------------------------------- Solution starts below -------------------------------- */
34+
//Subtasks 1 - AC
35+
//Subtasks 2 - AC
36+
37+
ll T,N,M,K;
38+
vector<int> A;
39+
40+
bool f(){
41+
42+
A.clear();
43+
44+
if(M < N-1) return 0;
45+
46+
for(int i=1;i<=N;i++) A.pb(i);
47+
48+
int ptr = 0, ptr2 = N-1;
49+
int menor = 1, maior = N, qtd = 0;
50+
51+
M = M - (N-1);
52+
53+
bool lado = false;
54+
55+
while(ptr <= ptr2){
56+
//cerr << pii(menor,maior) << " " << pii(ptr, ptr2) << " " << M << " ";
57+
if(!lado){
58+
//cerr << 1 << endl;
59+
if(M >= N - qtd - 1) {
60+
M = M - (N - qtd - 1) ;
61+
A[ptr2--] = menor++;
62+
lado ^= 1;
63+
}
64+
else {
65+
A[ptr2--] = maior--;
66+
}
67+
}
68+
else{
69+
//cerr << 2 << endl;
70+
if(M >= N - qtd - 1) {
71+
M = M - (N - qtd - 1);
72+
A[ptr++] = menor++;
73+
lado ^= 1;
74+
}
75+
else {
76+
A[ptr++] = maior--;
77+
}
78+
}
79+
80+
qtd++;
81+
82+
//cerr << A << endl << endl;
83+
}
84+
85+
return !M;
86+
}
87+
88+
void solve(int caso){
89+
90+
if(f()){
91+
cout << "Case #" << caso << ": ";
92+
for(int i=0;i<N;i++) cout << A[i] << " \n"[i==N-1];
93+
}
94+
else{
95+
cout << "Case #" << caso << ": IMPOSSIBLE\n";
96+
}
97+
98+
}
99+
100+
int main(){
101+
102+
optimize;
103+
104+
cin >> T;
105+
106+
for(int i=1;i<=T;i++){
107+
cin >> N >> M;
108+
109+
solve(i);
110+
}
111+
112+
return 0;
113+
}

0 commit comments

Comments
 (0)