結果

問題 No.2553 Holidays
ユーザー cho435cho435
提出日時 2023-12-08 00:20:13
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 7 ms / 2,000 ms
コード長 1,276 bytes
コンパイル時間 2,363 ms
コンパイル使用メモリ 212,692 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-09-27 02:28:48
合計ジャッジ時間 3,775 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 55
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i=0;i<(int)(n);i++)

int main(){
	int n,m;
	string s;
	cin>>n>>m>>s;
	vector<pair<char,int>> vp;
	int ans=0;
	{
		char bf=s.at(0);
		int nm=0;
		for(char c:s){
			if(c=='o') ans++;
			if(bf==c) nm++;
			else{
				vp.push_back({bf,nm});
				bf=c;
				nm=1;
			}
		}
		vp.push_back({bf,nm});
	}
	vector<pair<int,int>> vv;
	rep(i,vp.size()){
		if(vp.at(i).first=='-'){
			int ct=0;
			if(vp.at(i-1).first=='o') ct++;
			if(vp.at(i+1).first=='o') ct++;
			if(ct==2&&vp.at(i).second==1){
				ans++;
				continue;
			}
			vv.push_back({ct,vp.at(i).second});
		}
	}
	sort(vv.begin(),vv.end(),[&](auto p,auto q){
		auto[a,x]=p;
		auto[b,y]=q;
		if(a==2&&b==2){
			if(x%2==1&&y%2==1){
				return x<y;
			}
			return x%2>y%2;
		}
		if(a==0&&b==0){
			return x>y;
		}
		return a>b;
	});
	int tef=0;
	for(auto[t,nm]:vv){
		if(t==2){
			while(m>0&&nm>=2){
				m--;
				ans+=2;
				nm-=2;
			}
			if(nm==1) ans++;
		}else if(t==1){
			while(m>0&&nm>=2){
				m--;
				ans+=2;
				nm-=2;
			}
			if(nm==1) tef++;
		}else{
			if(m>0){
				m--;
				ans++;
				nm--;
				while(m>0&&nm>=2){
					m--;
					ans+=2;
					nm-=2;
				}
				if(nm==1) tef++;
			}
		}
	}
	ans+=min(tef,m);
	cout<<ans<<endl;
}
0