Codeforces Round #855 (Div. 3)补题ing

C1. Powering the Hero (easy version)

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

This is an easy version of the problem. It differs from the hard one only by constraints on nn and tt.

There is a deck of nn cards, each of which is characterized by its power. There are two types of cards:

  • a hero card, the power of such a card is always equal to 00;
  • a bonus card, the power of such a card is always positive.

You can do the following with the deck:

  • take a card from the top of the deck;
  • if this card is a bonus card, you can put it on top of your bonus deck or discard;
  • if this card is a hero card, then the power of the top card from your bonus deck is added to his power (if it is not empty), after that the hero is added to your army, and the used bonus discards.

Your task is to use such actions to gather an army with the maximum possible total power.

Input

The first line of input data contains single integer tt (1≤t≤10001≤t≤1000) — the number of test cases in the test.

The first line of each test case contains one integer nn (1≤n≤50001≤n≤5000) — the number of cards in the deck.

The second line of each test case contains nn integers s1,s2,…,sns1,s2,…,sn (0≤si≤1090≤si≤109) — card powers in top-down order.

It is guaranteed that the sum of nn over all test cases does not exceed 50005000.

Output

Output tt numbers, each of which is the answer to the corresponding test case — the maximum possible total power of the army that can be achieved.

Example

input

Copy

 

5

5

3 3 3 0 0

6

0 3 3 0 0 3

7

1 2 3 0 4 5 0

7

1 2 5 0 4 3 0

5

3 1 0 0 4

output

Copy

6
6
8
9
4

Note

In the first sample, you can take bonuses 11 and 22. Both hero cards will receive 33 power. If you take all the bonuses, one of them will remain unused.

In the second sample, the hero's card on top of the deck cannot be powered up, and the rest can be powered up with 22 and 33 bonuses and get 66 total power.

In the fourth sample, you can take bonuses 11, 22, 33, 55 and skip the bonus 66, then the hero 44 will be enhanced with a bonus 33 by 55, and the hero 77 with a bonus 55 by 44. 4+5=94+5=9.

优先队列

priority_queue<int> q;//保持队列头为最大元素

priority_queue<int, vector<int>, greater<int>> q;//保持队列头为最小元素

由于没有直接清空队列元素的函数,所以要自己写一个

void clear(priority_queue <int> &q) {
    priority_queue <int> empty;
    swap(empty,q);
}

AC代码如下 

#include<bits/stdc++.h>
using namespace std;
priority_queue<int>q;
int t,n;
void clear(priority_queue <int> &q) {
    priority_queue <int> empty;
    swap(empty,q);
}
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>n;
		int ai;
		long long ans=0;
		for(int i=0;i<n;i++){
			cin>>ai;
			if(ai!=0)q.push(ai);
			else if(q.size()){
				ans+=q.top();
				q.pop();
			}
		}
		clear(q);
		cout<<ans<<endl;
	}
}

C2. Powering the Hero (hard version)

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

This is a hard version of the problem. It differs from the easy one only by constraints on nn and tt.

There is a deck of nn cards, each of which is characterized by its power. There are two types of cards:

  • a hero card, the power of such a card is always equal to 00;
  • a bonus card, the power of such a card is always positive.

You can do the following with the deck:

  • take a card from the top of the deck;
  • if this card is a bonus card, you can put it on top of your bonus deck or discard;
  • if this card is a hero card, then the power of the top card from your bonus deck is added to his power (if it is not empty), after that the hero is added to your army, and the used bonus discards.

Your task is to use such actions to gather an army with the maximum possible total power.

Input

The first line of input data contains single integer tt (1≤t≤1041≤t≤104) — the number of test cases in the test.

The first line of each test case contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of cards in the deck.

The second line of each test case contains nn integers s1,s2,…,sns1,s2,…,sn (0≤si≤1090≤si≤109) — card powers in top-down order.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105.

Output

Output tt numbers, each of which is the answer to the corresponding test case — the maximum possible total power of the army that can be achieved.

感觉题干和easy version差不多,交了一回一样的代码过了,应该只是测试量大了一点。

D. Remove Two Letters

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Dmitry has a string ss, consisting of lowercase Latin letters.

Dmitry decided to remove two consecutive characters from the string ss and you are wondering how many different strings can be obtained after such an operation.

For example, Dmitry has a string "aaabcc". You can get the following different strings: "abcc"(by deleting the first two or second and third characters), "aacc"(by deleting the third and fourth characters),"aaac"(by deleting the fourth and the fifth character) and "aaab" (by deleting the last two).

Input

The first line of input data contains a single integer tt (1≤t≤1041≤t≤104) — number of test cases.

The descriptions of the test cases follow.

The first line of the description of each test case contains an integer nn (3≤n≤2⋅1053≤n≤2⋅105).

The second line of the description of each test case contains a string ss of length nn consisting of lowercase Latin letters.

It is guaranteed that the sum of nn for all test cases does not exceed 2⋅1052⋅105.

Output

For each test case print one integer — the number of distinct strings that can be obtained by removing two consecutive letters.

Example

input

Copy

 

7

6

aaabcc

10

aaaaaaaaaa

6

abcdef

7

abacaba

6

cccfff

4

abba

5

ababa

output

Copy

4
1
5
3
3
3
1

Note

The first example is explained in the statement.

In the third example, the following strings are obtained: "cdef", "adef", "abef", "abcf", "abcd".

In the seventh example, any deletion will result in the string "aba".

根据题意一功能删除n-1次两个连续的字母,所以最大种类数就是n-1,此外如果a[i]==a[i+2],删除a[i]、a[i+1]和删除a[i+1]、a[i+2]的结果是相同的,所以不妨一开始就设ans=n-1,每次a[i]==a[i+2]就让ans减1。

AC代码如下

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int t,n;
char a[200005];
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>n;
		cin>>a;
		int ans=n-1;
		for(int i=0;i<n-2;i++){
			if(a[i]==a[i+2])ans--;
		}
		cout<<ans<<'\n';
	}
}

E1. Unforgivable Curse (easy version)

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

This is an easy version of the problem. In this version, kk is always 33.

The chief wizard of the Wizengamot once caught the evil wizard Drahyrt, but the evil wizard has returned and wants revenge on the chief wizard. So he stole spell ss from his student Harry.

The spell — is a nn-length string of lowercase Latin letters.

Drahyrt wants to replace spell with an unforgivable curse — string tt.

Drahyrt, using ancient magic, can swap letters at a distance kk or k+1k+1 in spell as many times as he wants. In this version of the problem, you can swap letters at a distance of 33 or 44. In other words, Drahyrt can change letters in positions ii and jj in spell ss if |i−j|=3|i−j|=3 or |i−j|=4|i−j|=4.

For example, if s=s= "talant" and t=t= "atltna", Drahyrt can act as follows:

  • swap the letters at positions 11 and 44 to get spell "aaltnt".
  • swap the letters at positions 22 and 66 to get spell "atltna".

You are given spells ss and tt. Can Drahyrt change spell ss to tt?

Input

The first line of input gives a single integer TT (1≤T≤1041≤T≤104) — the number of test cases in the test.

Descriptions of the test cases are follow.

The first line contains two integers n,kn,k (1≤n≤2⋅1051≤n≤2⋅105, k=3k=3) — the length spells and the number kk such that Drahyrt can change letters in a spell at a distance kk or k+1k+1.

The second line gives spell ss — a string of length nn consisting of lowercase Latin letters.

The third line gives spell tt — a string of length nn consisting of lowercase Latin letters.

It is guaranteed that the sum of nn values over all test cases does not exceed 2⋅1052⋅105. Note that there is no limit on the sum of kk values over all test cases.

Output

For each test case, output on a separate line "YES" if Drahyrt can change spell ss to tt and "NO" otherwise.

You can output the answer in any case (for example, lines "yEs", "yes", "Yes" and "YES" will be recognized as positive answer).

Example

input

Copy

 

7

6 3

talant

atltna

7 3

abacaba

aaaabbc

12 3

abracadabraa

avadakedavra

5 3

accio

cicao

5 3

lumos

molus

4 3

uwjt

twju

4 3

kvpx

vxpk

output

Copy

YES
YES
NO
YES
NO
YES
NO

Note

The first example is explained in the condition.

In the second example we can proceed as follows:

  • Swap the letters at positions 22 and 55 (distance 33), then we get the spell "aaacbba".
  • Swap the letters at positions 44 and 77 (distance 33), then you get the spell "aaaabbc".

In the third example, we can show that it is impossible to get the string tt from the string ss by swapping the letters at a distance of 33 or 44.

In the fourth example, for example, the following sequence of transformations is appropriate:

  • "accio" →→ "aocic" →→ "cocia" →→ "iocca" →→ "aocci" →→ "aicco" →→ "cicao"

In the fifth example, you can show that it is impossible to get the string ss from the string tt.

In the sixth example, it is enough to swap the two outermost letters.

easy version中k的值恒定为3,也就是说两个字符间能发生交换的最小距离为3。

比如n=5时,第三个字符不能与任何字符发生交换。

        n=4时,第二、三个字符不能与任何字符发生交换。

当n>=6时,所有字符都可以找到能交换的对应字符,个人觉得这个有点像华容道,所有字符之间都可以做到交换位置的效果,所以如果两个字符串中每个字母的数量都相等,则认为可以成功变换。

确认每个字符串中各个字符的数量很容易,难点在于当n<6时如何找到不会发生变换的位置。

0 1 2 3 4中2不会发生变化

0 1 2 3中1、2不会发生变化

0 1 2中都不会发生变化

仔细观察可以发现规律:[n-3,3)区间内的字符不会变化,所以只要单独确认这两个字符串的这个区间内是否相等就可以了。可以推广到[n-k,k)。

当n<k时该区间内的n-k<0,所以当n<k时要单独判断。

AC代码

#include<bits/stdc++.h>
using namespace std;
const int N=1000005;
char a[200005],b[200005];
map<char,int>aa,bb;
int t,n,k;
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>n>>k;
		cin>>a>>b;
		if(n<k){// ab ab
			int fla=1;
			for(int i=0;i<n;i++){
				if(a[i]!=b[i]){
					fla=0;
					break;
				}
			}
			if(fla)cout<<"YES\n";
			else cout<<"NO\n";
			continue;
		}
		for(int i=0;i<n;i++){
			aa[a[i]]++;
			bb[b[i]]++;
		}
		int flag=1;
		for(char i='a';i<='z';i++){
			if(aa[i]!=bb[i]){
				flag=0;
				break;
			}
		}
		if(flag==1){
			for(int i=n-k;i<k;i++){
				if(a[i]!=b[i]){
					flag=0;
					break;
				}
			}
		}
		if(flag)cout<<"YES\n";
		else cout<<"NO\n";
		aa.clear();
		bb.clear();
	}
}

E2. Unforgivable Curse (hard version)

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

This is a complex version of the problem. This version has no additional restrictions on the number kk.

The chief wizard of the Wizengamot once caught the evil wizard Drahyrt, but the evil wizard has returned and wants revenge on the chief wizard. So he stole spell ss from his student Harry.

The spell — is a nn-length string of lowercase Latin letters.

Drahyrt wants to replace spell with an unforgivable curse — string tt.

Dragirt, using ancient magic, can swap letters at a distance kk or k+1k+1 in spell as many times as he wants. In other words, Drahyrt can change letters in positions ii and jj in spell ss if |i−j|=k|i−j|=k or |i−j|=k+1|i−j|=k+1.

For example, if k=3,s=k=3,s= "talant" and t=t= "atltna", Drahyrt can act as follows:

  • swap the letters at positions 11 and 44 to get spell "aaltnt".
  • swap the letters at positions 22 and 66 to get spell "atltna".

You are given spells ss and tt. Can Drahyrt change spell ss to tt?

Input

The first line of input gives a single integer TT (1≤T≤1041≤T≤104) — the number of test cases in the test.

Descriptions of the test cases are follow.

The first line contains two integers n,kn,k (1≤n≤2⋅1051≤n≤2⋅105, 1≤k≤2⋅1051≤k≤2⋅105) — the length spells and the number kk such that Drahyrt can change letters in a spell at a distance kk or k+1k+1.

The second line gives spell ss — a string of length nn consisting of lowercase Latin letters.

The third line gives spell tt — a string of length nn consisting of lowercase Latin letters.

It is guaranteed that the sum of nn values over all test cases does not exceed 2⋅1052⋅105. Note that there is no limit on the sum of kk values over all test cases.

Output

For each test case, output on a separate line "YES" if Drahyrt can change spell ss to tt and "NO" otherwise.

You can output the answer in any case (for example, lines "yEs", "yes", "Yes" and "YES" will be recognized as positive answer).

Example

input

Copy

 

7

6 3

talant

atltna

7 1

abacaba

aaaabbc

12 6

abracadabraa

avadakedavra

5 3

accio

cicao

5 4

lumos

molus

4 3

uwjt

twju

4 3

kvpx

vxpk

output

Copy

YES
YES
NO
YES
NO
YES
NO

Note

The first case is explained in the condition.

In the second case, we can swap adjacent letters, so we can sort the string using bubble sorting, for example.

In the third case, we can show that from the string ss we cannot get the string tt by swapping letters at a distance of 66 or 77.

In the fourth case, for example, the following sequence of transformations is appropriate:

  • "accio" →→ "aocic" →→ "cocia" →→ "iocca" →→ "aocci" →→ "aicco" →→ "cicao"

In the fifth case, we can show that it is impossible to get the string ss from the string tt.

In the sixth example, it is enough to swap the two outermost letters.

原理同easy version,只是区间推广为了[n-k,k),交esay version的代码也能过。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值