首页 > 其他分享 >CodeForces 1841C Ranom Numbers

CodeForces 1841C Ranom Numbers

时间:2023-06-16 21:55:06浏览次数:61  
标签:Ranom int max 1841C typedef long CodeForces mp define

洛谷传送门

CF 传送门

先反转 \(s\) 串,然后考虑 dp,设 \(f_{i, j, 0/1}\) 为考虑了 \([1, i]\),前缀最大值为 \(j\),是否修改过字符的最大得分。

转移讨论是否在这个位置修改即可。

时间复杂度 \(O(n)\)。

code
// Problem: C. Ranom Numbers
// Contest: Codeforces - Educational Codeforces Round 150 (Rated for Div. 2)
// URL: https://codeforces.com/group/bXwyZVR0kC/contest/1841/problem/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mems(a, x) memset((a), (x), sizeof(a))

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<ll, ll> pii;

const int maxn = 200100;
const int mp[] = {1, 10, 100, 1000, 10000};

int n, a[maxn];
ll f[maxn][5][2];
char s[maxn];

void solve() {
	scanf("%s", s + 1);
	n = strlen(s + 1);
	reverse(s + 1, s + n + 1);
	for (int i = 1; i <= n; ++i) {
		a[i] = s[i] - 'A';
	}
	for (int i = 0; i <= n; ++i) {
		for (int j = 0; j < 5; ++j) {
			for (int k = 0; k < 2; ++k) {
				f[i][j][k] = -1e18;
			}
		}
	}
	f[0][0][0] = 0;
	for (int i = 1; i <= n; ++i) {
		for (int j = 0; j < 5; ++j) {
			for (int k = 0; k < 2; ++k) {
				if (f[i - 1][j][k] < -1e17) {
					continue;
				}
				if (j > a[i]) {
					f[i][j][k] = max(f[i][j][k], f[i - 1][j][k] - mp[a[i]]);
				} else {
					f[i][a[i]][k] = max(f[i][a[i]][k], f[i - 1][j][k] + mp[a[i]]);
				}
			}
			for (int k = 0; k < 5; ++k) {
				if (f[i - 1][j][0] < -1e17) {
					continue;
				}
				if (j > k) {
					f[i][j][1] = max(f[i][j][1], f[i - 1][j][0] - mp[k]);
				} else {
					f[i][k][1] = max(f[i][k][1], f[i - 1][j][0] + mp[k]);
				}
			}
		}
	}
	ll ans = -1e18;
	for (int i = 0; i < 5; ++i) {
		for (int j = 0; j < 2; ++j) {
			ans = max(ans, f[n][i][j]);
		}
	}
	printf("%lld\n", ans);
}

int main() {
	int T = 1;
	scanf("%d", &T);
	while (T--) {
		solve();
	}
	return 0;
}

标签:Ranom,int,max,1841C,typedef,long,CodeForces,mp,define
From: https://www.cnblogs.com/zltzlt-blog/p/17486585.html

相关文章

  • CodeForces 1839E Decreasing Game
    洛谷传送门CF传送门不会,不知道该如何评价。确实是自己的问题。这种题肯定考虑先/后手必胜的充要条件。直接给出结论:若\(a\)能被分成两个和相等的可重集,后手必胜,否则先手必胜。感性理解一下,如果\(a\)能被分成两个和相等的可重集,先手选一个数,后手选另一个可重集中的数。......
  • Educational Codeforces Round 150 (Rated for Div. 2) 题解
    https://codeforces.com/contest/1841https://codeforces.com/contest/1841/problemsD.PairsofSegmentshttps://codeforces.com/contest/1841/problem/D因为\(n\)只有\(2000\),所以考虑枚举每一对\((i,j)\)满足区间有交集并且\(i\neqj\)。如果有交集,就合并。然后......
  • Codeforces Round 877 (Div.2) 题解 A - D
    A.BlackboardList题目大意起初黑板上有两个数,现在不断选取两个数作出他们俩差的绝对值并写在黑板上,如此往复直到黑板上有\(n\)个数。现在给定这\(n\)个数,问起初两数的其中一个数是多少。解题思路我们分两种可能:要么这两个数有负数,要么没有。有负数的情况,因为每次写下......
  • Educational Codeforces Round 9-C. The Smallest String Concatenation(字符串排序)
    原题链接C.TheSmallestStringConcatenationtimelimitpertestmemorylimitpertestinputoutputn strings a1, a2, ..., an.You'dliketoconcatenatethemtogetherinsomeordersuchthattheresultings......
  • Codeforces Round #143 (Div. 2)-D. Magic Box
    原题链接D.MagicBoxtimelimitpertestmemorylimitpertestinputoutputOnedayVasyawasgoinghomewhenhesawaboxlyingontheroad.Theboxcanberepresentedasa......
  • Codeforces Round #320 (Div. 2) - D. "Or" Game
    原题链接D."Or"GametimelimitpertestmemorylimitpertestinputoutputYouaregiven n numbers a1, a2, ..., an.Youcanperformatmost k operations.For......
  • Codeforces Round #416 (Div. 2)-C. Vladik and Memorable Trip
    原题链接C.VladikandMemorableTriptimelimitpertestmemorylimitpertestinputoutputVladikoftentravelsbytrains.HerememberedsomeofhistripsespeciallywellandIwouldliketotellyouaboutone......
  • Codeforces Round #415 (Div. 2)-C. Do you want a date?
    原题链接C.Doyouwantadate?timelimitpertestmemorylimitpertestinputoutputn1 to n.Sothe i-thhackedcomputerislocatedatthepoint xi.Moreoverthecoordinatesofallcomputersaredistinct.L......
  • Codeforces Round #221 (Div. 2)-D. Maximum Submatrix 2
    原题链接D.MaximumSubmatrix2timelimitpertestmemorylimitpertestinputoutputYouaregivenamatrixconsistingofdigitszeroandone,itssizeis n × m.Youare......
  • Educational Codeforces Round 20-C. Maximal GCD
    原题链接C.MaximalGCDtimelimitpertestmemorylimitpertestinputoutputn.Youshouldcreatesuch strictlyincreasing sequenceof k positivenumbers a1, a2, ..., ak,thattheirsumisequalto nGr......