답안 #741499

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
741499 2023-05-14T09:07:02 Z arnold518 Chorus (JOI23_chorus) C++17
0 / 100
1 ms 340 KB
#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};
	deque<int> DQ;
	DQ.push_back(0);
	for(int i=1; i<=N; i++)
	{
		while(DQ.size()>1 && dp[DQ[0]].first+lambda+2*f(DQ[0]+1, i)>dp[DQ[1]].first+lambda+2*f(DQ[1]+1, i)) DQ.pop_front();
		dp[i]={dp[DQ.front()].first+lambda+2*f(DQ.front()+1, i), dp[DQ.front()].second+1};
		DQ.push_back(i);
	}
	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=-1, 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);
}

Compilation message

chorus.cpp: In function 'int main()':
chorus.cpp:69:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   69 |   ll mid=lo+hi>>1;
      |          ~~^~~
chorus.cpp:41:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 |  scanf("%d%d", &N, &K);
      |  ~~~~~^~~~~~~~~~~~~~~~
chorus.cpp:42:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   42 |  scanf(" %s", S+1);
      |  ~~~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -