QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#556297#6539. Treasure BoxyqrWA 3ms7844kbC++203.2kb2024-09-10 16:40:462024-09-10 16:40:47

Judging History

你现在查看的是最新测评结果

  • [2024-09-10 16:40:47]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:7844kb
  • [2024-09-10 16:40:46]
  • 提交

answer

#include<stdio.h>
#include<ctype.h>
#include<algorithm>
namespace IO {
	constexpr int bufsize = 230005;
	char buf[bufsize], *f1, *f2;
	char gtchar() {return f1 == f2 && (f2 = buf + fread(f1 = buf, 1, bufsize, stdin)) == buf? EOF: *f1++;}
	template<typename T> void read(T &ret)
	{
		int f = ret = 0;
		char ch = gtchar();
		while(!isdigit(ch)) f = ch == '-', ch = gtchar();
		while(isdigit(ch)) ret = (ret << 3) + (ret << 1) + (ch ^ 48), ch = gtchar();
	}
	template<typename T, typename ...t> void read(T &a, t &...b) {read(a), read(b...);}
}using IO::read, IO::gtchar;
typedef long long ll;
constexpr int maxn = 1000005;
int T, n, mid, h[maxn], nxt[maxn];
ll C, mn, sum[maxn], s[maxn], val[maxn];
char str[maxn];
void chmin(int &a, int b) {if(a > b) a = b;}
void chmin(ll &a, ll b) {if(a > b) a = b;}
int min(int a, int b) {return a > b? b: a;}
void solve()
{
	for(int i = 1; i <= n; i++) sum[i] = sum[i - 1] + (str[i] == str[n - i + 1]? 0: h[i]);
	for(int i = 1; i <= mid; i++) s[i] = s[i - 1] + (str[i] == str[n - i + 1]? 0: min(h[i], h[n - i + 1]));
	int fir = nxt[1], lst = n - fir + 1;
	mn = 1e15;//x->fir->y(x<=mid)
	for(int i = lst; i >= n - i + 1; i--) chmin(mn, C * i - s[n - i] + sum[n - i]);
	for(int i = fir; i <= n - i + 1; i++) chmin(val[i], C * (i - 2 * fir) + s[mid] + mn);//, printf("update %d:%lld\n", i, C * (i - 2 * fir) + s[mid] + mn);
//	puts("===");
	mn = 1e15;//x->y->fir(x>mid)
	for(int i = lst; i >= n - i + 1; i--)
	{
		chmin(mn, 2 * C * i + sum[n - i] - s[n - i]);
		chmin(val[i], s[mid] - C * (i + fir) + mn);//, printf("update %d:%lld\n", i, s[mid] - C * (i + fir) + mn);
	}
//	puts("===");//x<=mid
	for(int i = mid; i >= fir; i--) chmin(val[i], s[mid] - C * (i + fir) + mn);//, printf("update %d:%lld\n", i, s[mid] - C * (i + fir) + mn);
//	puts("===");//x->fir->mid(x<=mid)
	int tmp = n - nxt[n - mid + 1] + 1;
	for(int i = fir; i <= n - i + 1; i++) chmin(val[i], sum[mid] + C * (tmp - fir) + C * min(i - fir, i < tmp? tmp - i: (i - tmp)));//, printf("update %d:%lld\n", i, sum[mid] + C * (mid - fir) + C * min(i - fir, i < mid? mid - i: (i - mid)));
//	puts("===");
	mn = 1e15;//x->y(x<fir<mid<=y)
	for(int i = lst; i >= n - i + 1; i--) chmin(mn, sum[n - i] - s[n - i] + C * i);
	for(int i = 1; i < fir; i++) chmin(val[i], s[mid] - C * i + mn);//, printf("update %d:%lld\n", i, s[mid] - C * i + mn);
	for(int i = 1; i < fir; i++) chmin(val[i], sum[mid] + C * (tmp - i));//, printf("update %d:%lld\n", i, sum[mid] + C * (tmp - i));
//	puts("\n");
}
int main()
{
	read(T);
	while(T--)
	{
		read(n, C), mid = n >> 1, nxt[n + 1] = 0;
		for(int i = 1; i <= n; i++) val[i] = 1e15;
		char ch = gtchar();
		while(!isalpha(ch)) ch = gtchar();
		for(int i = 1; i <= n; i++) str[i] = ch, ch = gtchar();
		for(int i = n; i; i--) nxt[i] = str[i] == str[n - i + 1]? nxt[i + 1]: i;
		for(int i = 1; i <= n; i++) read(h[i]);
		if(!nxt[1])
		{
			puts("0");
			continue;
		}
		solve();
//		for(int i = 1; i <= n; i++) printf("%lld ", val[i]);
//		puts("");
		std::reverse(h + 1, h + 1 + n), std::reverse(val + 1, val + 1 + n);
		solve();
		std::reverse(val + 1, val + 1 + n);
		for(int i = 1; i <= n; i++) printf("%lld ", val[i]);
		puts("");
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 5696kb

input:

2
5 1
ABCDE
7 1 4 5 1
5 1
ABCDA
7 1 4 5 1

output:

6 5 6 6 5 
2 1 2 3 4 

result:

ok 10 numbers

Test #2:

score: 0
Accepted
time: 0ms
memory: 5676kb

input:

1
10 4
AABABBABAB
10 3 4 10 7 1 8 10 7 6

output:

10 14 18 22 26 22 18 14 10 6 

result:

ok 10 numbers

Test #3:

score: -100
Wrong Answer
time: 3ms
memory: 7844kb

input:

10000
10 4
ABBBAABABB
6 10 3 9 1 8 5 4 4 10
10 4
BABBAAABAB
3 7 5 9 2 3 9 8 4 1
10 4
ABBBAAABAA
3 2 4 8 2 9 1 9 6 3
10 4
BBAABBAABB
8 8 10 7 1 1 4 9 6 4
10 4
BAABBBBBAB
5 7 3 5 4 2 1 8 2 8
10 4
ABAAAABABA
10 8 2 7 5 3 7 8 10 5
10 4
BBABABAABA
10 6 10 2 2 6 4 4 3 9
10 4
BBBAAAABBB
7 2 2 8 9 10 10 6 7...

output:

17 21 17 21 25 29 26 22 26 22 
21 17 13 9 13 13 9 13 17 21 
22 18 22 18 22 19 15 19 15 19 
0
11 7 3 7 11 15 12 8 12 16 
19 15 11 7 11 11 7 11 15 19 
30 34 38 34 30 34 38 42 39 35 
0
11 7 3 7 11 13 9 5 9 13 
8 4 8 12 16 14 10 6 2 6 
18 14 10 6 10 14 10 14 18 22 
27 23 19 23 19 19 23 24 28 32 
17 13 9...

result:

wrong answer 32nd numbers differ - expected: '0', found: '11'