ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

CodeForces - 1526D Kill Anton(模拟)

2021-05-29 10:02:17  阅读:222  来源: 互联网

标签:cnt 1526D int Anton pos ch Kill ban include


题目链接:https://vjudge.net/problem/CodeForces-1526D

题目大意:给出一个只有四种字母组成的字符串 A A A,要求将其重排列 B B B,使得贡献最大。贡献指的是,每次可以交换相邻的两个字母,问从 A A A 变成 B B B 的最小操作次数

题目分析:猜的结论,就是相同的字母一定连续,具体证明可以参考官方题解

然后思维上就没什么难度了,剩下的就是如何 O ( n ) O(n) O(n) 或者 O ( n l o g n ) O(nlogn) O(nlogn) 实现 c a l cal cal 函数用来计算两个字符串的贡献了

因为字符集比较小,而且还是连续的,所以我直接枚举 B B B 串,每次去 A A A 串从前往后扫,当一个 B B B 串中一个字符用完之后,将 A A A 串的游标置零再重新开始就可以了,常数是 4 4 4

就是实现的时候感觉蛮难调的

代码:

// Problem: D. Kill Anton
// Contest: Codeforces - Codeforces Round #723 (Div. 2)
// URL: https://codeforces.com/contest/1526/problem/D
// Memory Limit: 512 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast","inline","-ffast-math")
// #pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
#define lowbit(x) x&-x
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
template<typename T>
inline void read(T &x)
{
    T f=1;x=0;
    char ch=getchar();
    while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
    x*=f;
}
template<typename T>
inline void write(T x)
{
    if(x<0){x=~(x-1);putchar('-');}
    if(x>9)write(x/10);
    putchar(x%10+'0');
}
const int inf=0x3f3f3f3f;
const int N=1e6+100;
int cnt[4],a[N];
char str[]="AOTN";
int get_id(char ch) {
	if(ch=='A') {
		return 0;
	} else if(ch=='O') {
		return 1;
	} else if(ch=='T') {
		return 2;
	} else {
		return 3;
	}
}
string s;
bool ban[N];
LL check(string a) {
	LL ans=0;
	int pos=0;
	int cnt=1;
	for(int i=0;i<(int)a.size();i++) {
		if(i>0&&a[i]!=a[i-1]) {
			pos=0;
			cnt=!ban[pos];
		}
		while(s[pos]!=a[i]) {
			pos++;
			if(!ban[pos]) {
				cnt++;
			}
		}
		ban[pos]=true;
		ans+=cnt-1; 
		while(ban[pos]) {
			pos++;
		}
	}
	return ans;
}
int main()
{
#ifndef ONLINE_JUDGE
//	freopen("data.in.txt","r",stdin);
//	freopen("data.out.txt","w",stdout);
#endif
//	ios::sync_with_stdio(false);
	int w;
	cin>>w;
	while(w--) {
		memset(cnt,0,sizeof(cnt));
		cin>>s;
		for(auto it:s) {
			cnt[get_id(it)]++;
		}
		for(int i=0;i<4;i++) {
			a[i]=i;
		}
		LL mmax=-1;
		string ans;
		do {
			string ss;
			for(int i=0;i<4;i++) {
				for(int j=0;j<cnt[a[i]];j++) {
					ss+=str[a[i]];
				}
			}
			memset(ban,0,s.size()+5);
			LL tmp=check(ss);
			if(tmp>mmax) {
				mmax=tmp;
				ans=ss;
			}
		}while(next_permutation(a,a+4));
		cout<<ans<<endl;
	}
    return 0;
}

标签:cnt,1526D,int,Anton,pos,ch,Kill,ban,include
来源: https://blog.csdn.net/qq_45458915/article/details/117378804

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有