# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
569571 | esomer | Miners (IOI07_miners) | C++17 | 294 ms | 596 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
const int MOD = 1e9 + 7;
mt19937 gen(time(0));
/*void solve(){
int n; cin >> n;
string s; cin >> s;
string s1;
s1 += s[0];
string s2;
for(int i = 1; i < n; i++){
bool eq = 1;
for(int j = i - 1; j >= i - 2 && j >= 0; j--){
if(s[i] != s[j]) eq = 0;
}
if(eq) s2 += s[i];
else s1 += s[i];
}
int ans = 0;
for(int i = 0; i < s1.size(); i++){
bool one = 0;
bool two = 0;
bool three = 0;
for(int j = i; j >= 0 && j >= i - 2; j--){
if(s1[j] == 'F'){
if(!one){
ans++;
one = 1;
}
}else if(s1[j] == 'M'){
if(!two){
ans++;
two = 1;
}
}else if(!three){
ans++;
three = 1;
}
}
}
for(int i = 0; i < s2.size(); i++){
bool one = 0;
bool two = 0;
bool three = 0;
for(int j = i; j >= 0 && j >= i - 2; j--){
if(s2[j] == 'F'){
if(!one){
ans++;
one = 1;
}
}else if(s2[j] == 'M'){
if(!two){
ans++;
two = 1;
}
}else if(!three){
ans++;
three = 1;
}
}
}
cout << ans << endl;
}*/
int solve(int n, string& s){
string s1;
s1 += s[0];
string s2;
for(int i = 1; i < n; i++){
int ans1 = 0;
int ans2 = 0;
if(s1.size() > 1){
if(s1[s1.size() - 1] == s1[s1.size() - 2]){
if(s[i] == s1[s1.size() - 1]) ans1 = 1;
else ans1 = 2;
}else{
if(s1[s1.size() - 1] != s[i] && s1[s1.size() - 2] != s[i]) ans1 = 3;
else ans1 = 2;
}
}else if(s1.size() > 0){
if(s1[0] == s[i]) ans1 = 1;
else ans1 = 2;
}else ans1 = 1;
if(s2.size() > 1){
if(s2[s2.size() - 1] == s2[s2.size() - 2]){
if(s[i] == s2[s2.size() - 1]) ans2 = 1;
else ans2 = 2;
}else{
if(s2[s2.size() - 1] != s[i] && s2[s2.size() - 2] != s[i]) ans2 = 3;
else ans2 = 2;
}
}else if(s2.size() > 0){
if(s2[0] == s[i]) ans2 = 1;
else ans2 = 2;
}else ans2 = 1;
if(ans1 > ans2) s1 += s[i];
else s2 += s[i];
}
int ans = 0;
for(int i = 0; i < s1.size(); i++){
bool one = 0;
bool two = 0;
bool three = 0;
for(int j = i; j >= 0 && j >= i - 2; j--){
if(s1[j] == 'F'){
if(!one){
ans++;
one = 1;
}
}else if(s1[j] == 'M'){
if(!two){
ans++;
two = 1;
}
}else if(!three){
ans++;
three = 1;
}
}
}
for(int i = 0; i < s2.size(); i++){
bool one = 0;
bool two = 0;
bool three = 0;
for(int j = i; j >= 0 && j >= i - 2; j--){
if(s2[j] == 'F'){
if(!one){
ans++;
one = 1;
}
}else if(s2[j] == 'M'){
if(!two){
ans++;
two = 1;
}
}else if(!three){
ans++;
three = 1;
}
}
}
return ans;
}
int solve2(int n, string& s){
int best = 0;
for(int i = 0; i < (1 << (s.size())); i++){
string s1;
string s2;
for(int j = 0; j < s.size(); j++){
if(i & (1 << j)) s1 += s[j];
else s2 += s[j];
}
int ans = 0;
for(int i = 0; i < s1.size(); i++){
bool one = 0;
bool two = 0;
bool three = 0;
for(int j = i; j >= 0 && j >= i - 2; j--){
if(s1[j] == 'F'){
if(!one){
ans++;
one = 1;
}
}else if(s1[j] == 'M'){
if(!two){
ans++;
two = 1;
}
}else if(!three){
ans++;
three = 1;
}
}
}
for(int i = 0; i < s2.size(); i++){
bool one = 0;
bool two = 0;
bool three = 0;
for(int j = i; j >= 0 && j >= i - 2; j--){
if(s2[j] == 'F'){
if(!one){
ans++;
one = 1;
}
}else if(s2[j] == 'M'){
if(!two){
ans++;
two = 1;
}
}else if(!three){
ans++;
three = 1;
}
}
}
best = max(best, ans);
}
return best;
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//int tt; cin >> tt;
//int tt = 1;
//for(int i = 1; i <= tt; i++) solve();
/*for(int h = 0; h < 10000; h++){
int n = gen() % 10 + 1;
string s;
for(int i = 0; i < n; i++){
int x = gen() % 3;
if(x == 2) s+= 'F';
else if(x == 1) s+= 'B';
else s += 'M';
}
if(solve(n, s) != solve2(n, s)){
cout << s << endl;
cout << "Output: "<< solve(n, s) << " correct: "<< solve2(n, s) << endl;
return 0;
}
}*/
int n; cin >> n;
string s; cin >> s;
if(n <= 20) cout << solve2(n, s) << endl;
else cout << solve(n, s) << endl;
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |