답안 #704095

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
704095 2023-03-01T14:44:20 Z 1075508020060209tc Growing Vegetable is Fun 3 (JOI19_ho_t3) C++14
0 / 100
6 ms 8276 KB
//#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define X first
#define Y second
#define pr rr
int lowbit(int x){return x&-x;}
int bit[500005];
int upd(int pl,int v){
while(pl<=500000){
    bit[pl]+=v;
    pl+=lowbit(pl);
}
}
int qsum(int x){
int ret=0;
while(x){
    ret+=bit[x];
    x-=lowbit(x);
}
return ret;
}

vector<int>rr;
vector<int>gr;
vector<int>yr;


int n;
string s;




signed main(){
cin>>n>>s;
s="*"+s;
for(int i=1;i<=n;i++){
    if(s[i]=='R'){
        pr.push_back(i);
    }
    if(s[i]=='G'){
        gr.push_back(i);
    }
}
if(pr.size()>=gr.size()+2){
    cout<<-1<<endl;
    return 0;
}
if(gr.size()>=pr.size()+2){
    cout<<-1<<endl;
    return 0;
}
int fans=1e16;
if(gr.size()>=pr.size()){
    int ans=0;
    for(int i=1;i<=n;i++){
        if(i%2==1){
            int pl=gr[(i+1)/2-1];
            ans+=pl-i+qsum(n)-qsum(pl);
            upd(pl,1);
        }else{
            int pl=pr[i/2-1];
            ans+=pl-i+qsum(n)-qsum(pl);
            upd(pl,1);
        }
    }
    fans=ans;
}
for(int i=1;i<=500000;i++){bit[i]=0;}
if(gr.size()<=pr.size()){
    int ans=0;
    for(int i=1;i<=n;i++){
         //   cout<<i<<endl;
        if(i%2==0){
            int pl=gr[(i+1)/2-1];
            ans+=pl-i+qsum(n)-qsum(pl);
            upd(pl,1);
        }else{
            int pl=pr[(i+1)/2-1];
            ans+=pl-i+qsum(n)-qsum(pl);
            upd(pl,1);
        }
    }
    fans=min(fans,ans);
}
cout<<fans;

}

Compilation message

joi2019_ho_t3.cpp: In function 'long long int upd(long long int, long long int)':
joi2019_ho_t3.cpp:15:1: warning: no return statement in function returning non-void [-Wreturn-type]
   15 | }
      | ^
# 결과 실행 시간 메모리 Grader output
1 Runtime error 5 ms 8276 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 5 ms 8276 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 6 ms 8276 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 5 ms 8276 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -