AtCoder Grand Contest 026 (AGC026) E - Synchronized Subsequence 贪心 动态规划

原文链接

题目传送门 - AGC026E

题意

  给定一个长度为 $2n$ 的字符串,包含 $n$ 个 $‘a‘$ 和 $n$ 个 $‘b‘$ 。

  现在,让你按照原顺序取出一些字符,按照原顺序组成新的字符串,输出所有满足条件的字符串中字典序最大的?(字典序: $b>a>""$)

  条件限制:当且仅当取了原序列的第 $i$ 个 $‘a‘$ 时,原序列的第 $i$ 个 $‘b‘$ 也被取了。

  $n\leq 3000$

题解

  

代码

#include <bits/stdc++.h>
using namespace std;
const int N=6005;
int n,p[N],pp[N],t[N];
int q[N],head,tail;
string res[N],now;
char s[N];
vector <int> a,b;
int main(){
	scanf("%d",&n);
	n<<=1;
	scanf("%s",s+1);
	a.clear(),b.clear();
	for (int i=1;i<=n;i++)
		if (s[i]==‘a‘)
			a.push_back(i);
		else
			b.push_back(i);
	for (int i=1;i<=n/2;i++)
		p[b[i-1]]=a[i-1],pp[a[i-1]]=b[i-1];
	for (int i=1;i<=n+1;i++)
		res[i]="";
	int tot=0;
	int nxt=n+1;
	for (int i=n;i>=1;i--){
		if (s[i]==‘a‘)
			tot++;
		else
			tot--;
		if (tot)
			continue;
		memset(t,0,sizeof t);
		head=1,tail=0;
		res[i]=res[nxt];
		for (int j=i;j<nxt;j++){
			if (s[j]==‘a‘){
				if (t[j]){
					now="";
					for (int k=i;k<nxt;k++)
						if (s[k]==‘b‘){
							if (t[k]||k>j)
								now+="b";
						}
						else if (t[k]||k>p[q[tail]])
							now+="a";
					res[i]=max(res[i],now+res[nxt]);
					head++;
					t[j]=t[pp[j]]=0;
				}
				continue;
			}
			if (p[j]<j)
				continue;
			q[++tail]=j;
			t[j]=t[p[j]]=1;
		}
		memset(t,0,sizeof t);
		if (tail==0){
			for (int j=i;j<nxt;j++){
				if (s[j]==‘b‘)
					continue;
				t[j]=t[pp[j]]=1;
				now="";
				for (int k=i;k<=pp[j];k++)
					if (t[k])
						now+=s[k];
				res[i]=max(res[i],now+res[nxt]);
				j=pp[j];
			}
		}
		nxt=i;
	}
	cout << res[1];
	return 0;
}

  

原文地址:https://www.cnblogs.com/zhouzhendong/p/AGC026E.html

时间: 2024-07-30 15:36:46

AtCoder Grand Contest 026 (AGC026) E - Synchronized Subsequence 贪心 动态规划的相关文章

Atcoder Grand Contest 026 (AGC026) F - Manju Game 博弈,动态规划

原文链接www.cnblogs.com/zhouzhendong/AGC026F.html 前言 太久没有发博客了,前来水一发. 题解 不妨设先手是 A,后手是 B.定义 \(i\) 为奇数时,\(a_i\) 为"奇数位上的数":\(i\) 为偶数时, \(a_i\) 为"偶数位上的数".定义左.右两端的数分别表示 \(a_1\) 和 \(a_n\). 考虑第一步: 首先,如果 A 取了左右某一个端点,那么他必然能取走和他取的点奇偶性相同的所有点. 然后,我们考虑

AtCoder Grand Contest #026 A - Colorful Slimes 2

Time Limit: 2 sec / Memory Limit: 1024 MB Score : 200200 points Problem Statement Takahashi lives in another world. There are slimes (creatures) of 1000010000 colors in this world. Let us call these colors Color 1,2,...,100001,2,...,10000. Takahashi

AtCoder Grand Contest 031 (AGC031) F - Permutation and Minimum 动态规划

原文链接www.cnblogs.com/zhouzhendong/p/AGC031F.html 草率题解 对于每两个相邻位置,把他们拿出来. 如果这两个相邻位置都有确定的值,那么不管他. 然后把所有的这些数拿出来,分为两类,一类是没有被填入的,一类是被填入的. 然后大力DP即可.由于没有被填入的可以任意排列,所以最后还要乘上一个阶乘. 代码 #include <bits/stdc++.h> #define clr(x) memset(x,0,sizeof x) #define For(i,a,

AtCoder Grand Contest 024 Problem E(动态规划)

www.cnblogs.com/shaokele/ AtCoder Grand Contest 024 Problem E Time Limit: 2 Sec Memory Limit: 1024 MB Description Find the number of the possible tuples of sequences (\(A_0,A_1,-,A_N\)) that satisfy all of the following conditions, modulo \(M\): ? Fo

AtCoder Grand Contest 025 Problem D

www.cnblogs.com/shaokele/ AtCoder Grand Contest 025 Problem D Time Limit: 2 Sec Memory Limit: 1024 MB Description Takahashi is doing a research on sets of points in a plane. Takahashi thinks a set \(S\) of points in a coordinate plane is a good set w

AtCoder Grand Contest 011

AtCoder Grand Contest 011 upd:这篇咕了好久,前面几题是三周以前写的... AtCoder Grand Contest 011 A - Airport Bus 翻译 有\(n\)个乘客到达了飞机场,现在他们都要坐车离开机场.第\(i\)个乘客到达的时间是\(T_i\),一个乘客必须在\([T_i,T_i+k]\)时刻做到车,否则他会生气.一辆车最多可以坐\(C\)个人.问最少安排几辆车可以让所有人都不生气. 题解 从前往后贪心即可. #include<iostream

AtCoder Grand Contest 014

AtCoder Grand Contest 014 A - Cookie Exchanges 有三个人,分别有\(A,B,C\)块饼干,每次每个人都会把自己的饼干分成相等的两份然后给其他两个人.当其中有一个人的饼干数量是奇数的时候停止,求会进行几次这样子的操作,或者会永远进行下去. 首先无解的情况一定是三个数都是相等的偶数. 否则直接暴力模拟就行了.(盲猜答案不会很大) 证明一下答案的范围:不妨令\(A\le B\le C\),那么最大值和最小值之间的差就是\(C-A\),那么执行完一次操作之后

【Atcoder Grand Contest 020 E】 Encoding Subsets

Atcoder Grand Contest 020 E 题意:给一个\(0-1\)字符串,如果其中有一段重复,就可以表示成\((\)这一块的表示\(\times\)出现次数\()\). 问这个字符串的所有子集中有多少种表示方法. 思路:考虑\(dp(s)\)表示字符串\(s\)的答案. 那么我们得考虑第一个表示成的位置是什么. ①第一位就是表示的第一位,不参与循环.那么转移到\(dp(s.substr(1))\),并且如果这位是\(1\),那么乘上\(2\),因为这位可能是\(0\). ②一个前

AtCoder Grand Contest 016

AtCoder Grand Contest 016 A - Shrinking 你可以进行一个串的变换,把一个长度为\(n\)的串\(S\)可以变成长度为\(n-1\)的串\(T\),其中\(T_i\)要么是\(S_i\)要么是\(S_{i+1}\). 现在问你最少进行多少次这个操作,能够使最终得到的\(T\)只由一个字符构成. \(|S|\le 100\) 首先枚举最终字符是哪一个.那么首先在\(S\)末尾加上一个这个字符,那么这个最小步数等于对于所有位置而言,离它最近的枚举的字符到这个位置的