# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
741489 | arnold518 | Chorus (JOI23_chorus) | C++17 | 2 ms | 340 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1e6;
const ll INF = 1e12;
int N, K;
char S[MAXN*2+10];
int A[MAXN+10], C[MAXN+10];
ll B[MAXN+10];
ll ans;
ll f(int l, int r)
{
if(C[r]<l) return 0;
return (ll)r*(C[r]-l+1)-(B[C[r]]-B[l-1]);
}
pll dp[MAXN+10];
ll solve(ll lambda)
{
dp[0]={0, 0};
for(int i=1; i<=N; i++)
{
dp[i]={9e18, 0};
for(int j=0; j<i; j++)
{
dp[i]=min(dp[i], {dp[j].first+lambda+2*f(j+1, i), dp[j].second+1});
}
}
return dp[N].second;
}
int main()
{
scanf("%d%d", &N, &K);
scanf(" %s", S+1);
int t=0, k=0;
for(int i=1; i<=N+N; i++)
{
if(S[i]=='A') k++;
else A[++t]=k;
}
for(int i=1; i<=N; i++)
{
if(A[i]<i)
{
ans+=i-A[i];
A[i]=i;
}
B[i]=B[i-1]+A[i];
}
for(int i=1, j=1; i<=N; i++)
{
for(; j<=N && A[j]<i; j++);
C[i]=j-1;
}
ll lo=0, hi=INF;
while(lo+1<hi)
{
ll mid=lo+hi>>1;
if(solve(mid*2+1)<K) hi=mid;
else lo=mid;
}
solve(hi*2);
printf("%lld\n", ans+dp[N].first/2-K*hi);
}
컴파일 시 표준 에러 (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... |