Codeforces Round #657 (Div. 2)题解总结

文章目录

  • A. Acacius and String
  • B. Dubious Cyrpto
  • C. Choosing flowers

A. Acacius and String

time limit per test1 second
memory limit per test512 megabytes
inputstandard input
outputstandard output
Acacius is studying strings theory. Today he came with the following problem.

You are given a string s of length n consisting of lowercase English letters and question marks. It is possible to replace question marks with lowercase English letters in such a way that a string “abacaba” occurs as a substring in a resulting string exactly once?

Each question mark should be replaced with exactly one lowercase English letter. For example, string “a?b?c” can be transformed into strings “aabbc” and “azbzc”, but can’t be transformed into strings “aabc”, “a?bbc” and “babbc”.

Occurrence of a string t of length m in the string s of length n as a substring is a index i (1≤i≤n−m+1) such that string s[i…i+m−1] consisting of m consecutive symbols of s starting from i-th equals to string t. For example string “ababa” has two occurrences of a string “aba” as a substring with i=1 and i=3, but there are no occurrences of a string “aba” in the string “acba” as a substring.

Please help Acacius to check if it is possible to replace all question marks with lowercase English letters in such a way that a string “abacaba” occurs as a substring in a resulting string exactly once.

Input
First line of input contains an integer T (1≤T≤5000), number of test cases. T pairs of lines with test case descriptions follow.

The first line of a test case description contains a single integer n (7≤n≤50), length of a string s.

The second line of a test case description contains string s of length n consisting of lowercase English letters and question marks.

Output
For each test case output an answer for it.

In case if there is no way to replace question marks in string s with a lowercase English letters in such a way that there is exactly one occurrence of a string “abacaba” in the resulting string as a substring output “No”.

Otherwise output “Yes” and in the next line output a resulting string consisting of n lowercase English letters. If there are multiple possible strings, output any.

You may print every letter in “Yes” and “No” in any case you want (so, for example, the strings yEs, yes, Yes, and YES will all be recognized as positive answer).

Example
inputCopy
6
7
abacaba
7
???
11
aba?abacaba
11
abacaba?aba
15
asdf???f???qwer
11
abacabacaba
outputCopy
Yes
abacaba
Yes
abacaba
Yes
abadabacaba
Yes
abacabadaba
No
No
Note
In first example there is exactly one occurrence of a string “abacaba” in the string “abacaba” as a substring.

In second example seven question marks can be replaced with any seven lowercase English letters and with “abacaba” in particular.

In sixth example there are two occurrences of a string “abacaba” as a substring.

题意:
给定一个字符串,含有小写字母和? ,可以把?替换成任意小写字母,要判断字符串中’abacaba’这个字串是否有且只有出现过一次。

思路:
暴力查找,先判断原本字符串中’abacaba’出现的次数,如果大于1次,则输出no。如果等于一次就输出‘yes’。否则就对字符串进行修改。再进行判断

代码:

#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
int inf = 0x3f3f3f3f;
const int N = 2e5 + 7;
int t, h, n;
int a[N];
int ans, res, cnt;
ll l, r, m;
bool check(long long n) 
{
	if (n >= l && n <= r)return true;
	else return false;
}
string s = "abacaba";
int find(string str) 
{
	int i, j;
	int cnt = 0;
	for (i = 0; i <= str.size() - 7; i++) {
		int flag = 1;

		for (j = 0; j < 7; j++) {
			if (s[j] != str[i + j]) {
				flag = 0;
				break;
			}
		}
		if (flag)
			cnt++;
	}
	return cnt;
}

int main() {
	int t;
	cin >> t;
	while (t--) 
	{
		int n;
		cin >> n;
		string str;
		cin >> str;
		int cnt = find(str);
		int i, j;
		if (cnt > 1) 
		{
			cout << "No" << endl;
			continue;
		}
		if (cnt == 1) 
		{
			cout << "Yes" << endl;
			for (i = 0; i < str.size(); i++)
			{
				if (str[i] != '?')cout << str[i];
				else cout << 'z';
			}
			cout << endl;
			continue;
		}

		string tmp = str;
		int flag1 = 1;
		for (i = 0; i <= str.size() - 7; i++) {
			int flag = 1;
			str = tmp;
			for (j = 0; j < 7; j++) {
				if (str[i + j] == '?') str[i + j] = s[j];
				if (str[i + j] != s[j])flag = 0;
			}
			if (flag) {
				cnt = find(str);
				if (cnt == 1) {
					flag1 = 0;
					cout << "Yes" << endl;
					for (i = 0; i < str.size(); i++) {
						if (str[i] != '?')cout << str[i];
						else cout << 'z';
					}
					cout << endl;
				}

				if (flag1 == 0)break;
			}
		}
		if (flag1) {
			cout << "No" << endl;
		}

	}
	return 0;
}

B. Dubious Cyrpto

time limit per test1 second
memory limit per test512 megabytes
inputstandard input
outputstandard output
Pasha loves to send strictly positive integers to his friends. Pasha cares about security, therefore when he wants to send an integer n, he encrypts it in the following way: he picks three integers a, b and c such that l≤a,b,c≤r, and then he computes the encrypted value m=n⋅a+b−c.

Unfortunately, an adversary intercepted the values l, r and m. Is it possible to recover the original values of a, b and c from this information? More formally, you are asked to find any values of a, b and c such that

a, b and c are integers,
l≤a,b,c≤r,
there exists a strictly positive integer n, such that n⋅a+b−c=m.
Input
The first line contains the only integer t (1≤t≤20) — the number of test cases. The following t lines describe one test case each.

Each test case consists of three integers l, r and m (1≤l≤r≤500000, 1≤m≤1010). The numbers are such that the answer to the problem exists.

Output
For each test case output three integers a, b and c such that, l≤a,b,c≤r and there exists a strictly positive integer n such that n⋅a+b−c=m. It is guaranteed that there is at least one possible solution, and you can output any possible combination if there are multiple solutions.

Example
inputCopy
2
4 6 13
2 3 1
outputCopy
4 6 5
2 2 3
Note
In the first example n=3 is possible, then n⋅4+6−5=13=m. Other possible solutions include: a=4, b=5, c=4 (when n=3); a=5, b=4, c=6 (when n=3); a=6, b=6, c=5 (when n=2); a=6, b=5, c=4 (when n=2).

In the second example the only possible case is n=1: in this case n⋅2+2−3=1=m. Note that, n=0 is not possible, since in that case n is not a strictly positive integer.

题意:
有三个整数a,b,c。满足l<=a,b,c<=r 还有一个整数m=n*a+b-c。n是严格大于0的正整数。题面给定l,r,m。要求出a,b,c的值

思路:
先枚举a,再讨论b-c。在 l 到 r 的范围枚举a,那么这个时候的b-c有两种情况:n*a ; (n+1)*a。但由于n必须大于0,所以我们就得先判断(n+1)*a,因为这种情况一定是大于0的。再保证m%a(即b-c)在 r - l的范围内就可以了

代码:

#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
int inf = 0x3f3f3f3f;
const int N = 2e5 + 7;
int t, h, n;
int a[N];
int ans, res, cnt;
int main() {
	int t;
	cin >> t;
	while (t--) 
	{
		ll l, r, m, a, b, c;
		cin >> l >> r >> m;
		for (ll i = l; i <= r; i++)
		{
			a = i;
			if (a - m % a <= r - l)
				c = r, b = r - (a - m % a);
			else
				b = r, c = r - m % a;
			if (b <= r && b >= l && c <= r && c >= l)
				break;
		}
		cout << a << " " << b << " " << c << endl;

	}
	return 0;
}

C. Choosing flowers

time limit per test1 second
memory limit per test512 megabytes
inputstandard input
outputstandard output
Vladimir would like to prepare a present for his wife: they have an anniversary! He decided to buy her exactly n flowers.

Vladimir went to a flower shop, and he was amazed to see that there are m types of flowers being sold there, and there is unlimited supply of flowers of each type. Vladimir wants to choose flowers to maximize the happiness of his wife. He knows that after receiving the first flower of the i-th type happiness of his wife increases by ai and after receiving each consecutive flower of this type her happiness increases by bi. That is, if among the chosen flowers there are xi>0 flowers of type i, his wife gets ai+(xi−1)⋅bi additional happiness (and if there are no flowers of type i, she gets nothing for this particular type).

Please help Vladimir to choose exactly n flowers to maximize the total happiness of his wife.

Input
The first line contains the only integer t (1≤t≤10000), the number of test cases. It is followed by t descriptions of the test cases.

Each test case description starts with two integers n and m (1≤n≤109, 1≤m≤100000), the number of flowers Vladimir needs to choose and the number of types of available flowers.

The following m lines describe the types of flowers: each line contains integers ai and bi (0≤ai,bi≤109) for i-th available type of flowers.

The test cases are separated by a blank line. It is guaranteed that the sum of values m among all test cases does not exceed 100000.

Output
For each test case output a single integer: the maximum total happiness of Vladimir’s wife after choosing exactly n flowers optimally.

Example
inputCopy
2
4 3
5 0
1 4
2 2

5 3
5 2
4 2
3 1
outputCopy
14
16
Note
In the first example case Vladimir can pick 1 flower of the first type and 3 flowers of the second type, in this case the total happiness equals 5+(1+2⋅4)=14.

In the second example Vladimir can pick 2 flowers of the first type, 2 flowers of the second type, and 1 flower of the third type, in this case the total happiness equals (5+1⋅2)+(4+1⋅2)+3=16.

题意:
有m种花,每种花有第一次购买获得的数值与第k次购买获得的数值(k>1),问你买n朵花获得的最大数值是多少。
思路:
我们可以枚举每个物品,如果这个物品的bi大于aj的个数大于等于n个,也就是说全部取a最优,就可以把前n大的a累加作为答案,如果个数不足n个的话,就把符合条件的所有aj累加后再加上,bi乘剩余所需件数,作为答案进行比较
代码:

#include
using namespace std;
const int maxn=1e5+5;
int t;
vector < pair<long long ,long long> >v;
vector <long long> fir;
long long suf[maxn];
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        fir.clear(); v.clear();
        long long n,m,x,y;
        scanf("%lld%lld",&n,&m);
        for(int i=1;i<=m;i++)
        {
            scanf("%lld%lld",&x,&y);
            v.push_back(make_pair(x,y));
            fir.push_back(x);
        }
        sort(fir.begin(),fir.end());
        for(int i=0;i<m;i++) suf[i]=fir[i];
        for(int i=1;i<m;i++)
            suf[i]+=suf[i-1];
        long long ans=0;
        for(int i=0;i<m;i++)
        {
            int cntn=n;
            long long fi=v[i].first;
            long long se=v[i].second;
            int pos=upper_bound(fir.begin(),fir.end(),se)-fir.begin();
            if(m-pos>=cntn)
            {
                long long aans=suf[m-1];
                if(m-cntn>=1)
                    aans-=suf[m-cntn-1];
                ans=max(ans,aans);
            }
            else
            {
                long long aans=suf[m-1];
                if(pos>0)
                     aans-=suf[pos-1];
                cntn-=m-pos;
                if(pos!=m)
                {
                    if(fi<fir[pos])
                    {
                        aans+=fi;
                        cntn--;
                    }
                }
                else
                {
                    aans+=fi;
                    cntn--;
                }
                aans+=(se*cntn);
                ans=max(ans,aans);
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
 }

你可能感兴趣的:(题解)