제출 #516577

#제출 시각아이디문제언어결과실행 시간메모리
516577leinad2Monochrome Points (JOI20_monochrome)C++17
100 / 100
1512 ms8056 KiB
#include<bits/stdc++.h>
using namespace std;
int n, i, j, k, a, b;
long long ans=0;
char S[400010];
vector<int>A, B;
int tree[400010];
void init(){for(int i=1;i<=2*n;i++)tree[i]=0;}
void update(int pos, int v){while(pos<=2*n)tree[pos]+=v,pos+=(pos&-pos);}
int get(int pos){int res=0;while(pos)res+=tree[pos],pos-=(pos&-pos);return res;}
long long f(int i)
{
    init();
    vector<pair<int, int> >V;
    for(int j=i;j<n;j++)V.push_back({A[j-i], B[j]});
    for(int j=0;j<i;j++)V.push_back({A[j+n-i], B[j]});
    for(auto &p:V)if(p.first>p.second)swap(p.first, p.second);
    sort(V.begin(), V.end());
    long long res=0;
    for(auto p:V)
    {
        int a=p.second;
        update(a, 1);
        res+=get(a-1)-get(p.first);
    }
    ans=max(ans, res);
    return res;
}
main()
{
    scanf("%d", &n);
    scanf("%s", &S);
    for(i=0;i<2*n;i++)
    {
        if(S[i]=='B')A.push_back(i+1);
        else B.push_back(i+1);
    }
    int a=0, b=n-1;
    while(a+3<=b)
    {
        int m1=(a+a+b)/3, m2=(a+b+b)/3;
        if(f(m1)>f(m2))b=m2;
        else a=m1;
    }
    for(i=a;i<=b;i++)f(i);
    cout<<ans;
}

컴파일 시 표준 에러 (stderr) 메시지

monochrome.cpp:29:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   29 | main()
      | ^~~~
monochrome.cpp: In function 'int main()':
monochrome.cpp:32:13: warning: format '%s' expects argument of type 'char*', but argument 2 has type 'char (*)[400010]' [-Wformat=]
   32 |     scanf("%s", &S);
      |            ~^   ~~
      |             |   |
      |             |   char (*)[400010]
      |             char*
monochrome.cpp:31:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   31 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
monochrome.cpp:32:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |     scanf("%s", &S);
      |     ~~~~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...