题解(657.div2)

题解(657.div2)

A:Acacius and String

Acacius is studying strings theory. Today he came with the following problem.
You are given a string ss of length nn 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 mm 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 nn 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).

这道题也是一道简单的数学题。

#include<bits/stdc++.h>
#define LL long long
#define U unsigned
bool isp(int x){
	if(x<2)return 0;
	for(int i=2;i<=sqrt(x);i++){
		if(x%i==0) return 0;
	}
	return 1;
}
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
using namespace std;
const string str="abacaba";
int n;
bool check(string &x){
	int cnt=0;
	for(int i=0;i+7<=n;i++)
		if(x.substr(i,7)==str)cnt++;
	return cnt==1?1:0;
}
int main(){
	int T=read();
	while(T--){
		n=read();
		string s;
		cin>>s;
		bool flag=0;
		for(int i=0;i+6<n;i++){
			string tmps=s;
			bool flag1=1;
			for(int j=0;j<n;j++){
				if(tmps[j]=='?'){
					if(j>=i&&j-i<7) tmps[j]=str[j-i];
                    else tmps[j]='d';
				}	
			}
			if(check(tmps)){
				cout<<"Yes"<<endl<<tmps<<endl;
				flag=1;
				break;
			}
		}
		if(flag)continue;
		cout<<"No"<<endl;
	}
	return 0;
}

B:Dubious Cyrpto

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 thata, 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≤10000000000). 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.

这也是一道数学题,需要根据题目要求来创造一个算法实现。

#include<bits/stdc++.h>
#define LL long long
#define U unsigned
bool isp(int x){
	if(x<2)return 0;
	for(int i=2;i<=sqrt(x);i++){
		if(x%i==0) return 0;
	}
	return 1;
}
inline LL read()
{
	LL x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
using namespace std;
int main() {
	LL T=read();
	while(T--){
		LL l=read(),r=read(),m=read();
		for(int i=l;i<=r;++i){
			if(m>i){
				if(m%i<=r-l){
					cout<<i<<" "<<l+m%i<<" "<<l<<endl;
					break;
				}else if(i-m%i<=r-l){
					cout<<i<<" "<<r-(i-m%i)<<" "<<r<<endl;
					break;
				}
			}else if(i>=m&&i-m<=r-l){
				cout<<i<<" "<<r-i+m<<" "<<r<<endl;
				break;
			}
		}
	}
	return 0;
}



C:Choosing flowers

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.

这道题是一道数学题,但是相对的更加难。结合了二分。

#include<bits/stdc++.h>
#define LL long long
#define U unsigned
bool isp(int x){
	if(x<2)return 0;
	for(int i=2;i<=sqrt(x);i++){
		if(x%i==0) return 0;
	}
	return 1;
}
inline LL read()
{
	LL x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
using namespace std;
int a[300000],b[300000],aa[300000],s[300000];
int main() {
	LL T=read();
	while(T--){
		LL n=read(),m=read();
		for(LL i=1;i<=m;i++){
			a[i]=read(),b[i]=read();
			aa[i]=a[i];
		}
		sort(aa+1,aa+m+1);
		for(LL i=1;i<=m;i++){
			s[i]=s[i-1]+aa[i];
		}
		LL ans=0;
		for(LL i=1;i<=m;i++){
			LL sum=0,q=n,pos=lower_bound(aa+1,aa+m+1,b[i])-aa,tmpq=min(q,m-pos+1);
			sum+=s[m]-s[m-tmpq];
			q-=tmpq;
			if(q==0){
				ans=max(ans,sum);
			}else{
				if(aa[pos]>a[i]||pos==m+1){ 
					sum+=a[i];
					q--;
				}
				sum+=q*b[i];
				ans=max(ans,sum);
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

D:New Passenger Trams

There are many freight trains departing from Kirnes planet every day. One day on that planet consists of h hours, and each hour consists of m minutes, where m is an even number. Currently, there are n freight trains, and they depart every day at the same time: i-th train departs at hi hours and mi minutes.
The government decided to add passenger trams as well: they plan to add a regular tram service with half-hour intervals. It means that the first tram of the day must depart at 0 hours and t minutes, where 0≤t<m2, the second tram departs m2 minutes after the first one and so on. This schedule allows exactly two passenger trams per hour, which is a great improvement.
To allow passengers to board the tram safely, the tram must arrive 𝑘 minutes before. During the time when passengers are boarding the tram, no freight train can depart from the planet. However, freight trains are allowed to depart at the very moment when the boarding starts, as well as at the moment when the passenger tram departs. Note that, if the first passenger tram departs at 0 hours and t minutes, where t<k, then the freight trains can not depart during the last k−t minutes of the day.
A schematic picture of the correct way to run passenger trams. Here h=2 (therefore, the number of passenger trams is 2h=4), the number of freight trains is n=6. The passenger trams are marked in red (note that the spaces between them are the same). The freight trains are marked in blue. Time segments of length k before each passenger tram are highlighted in red. Note that there are no freight trains inside these segments.
Unfortunately, it might not be possible to satisfy the requirements of the government without canceling some of the freight trains. Please help the government find the optimal value of t to minimize the number of canceled freight trains in case all passenger trams depart according to schedule.
Input
The first line of input contains four integers n, h, m, k (1≤n≤100000, 1≤h≤109, 2≤m≤109, m is even, 1≤k≤m2) — the number of freight trains per day, the number of hours and minutes on the planet, and the boarding time for each passenger tram.
n lines follow, each contains two integers hi and mi (0≤hi<h, 0≤mi<m) — the time when i-th freight train departs. It is guaranteed that no freight trains depart at the same time.
Output
The first line of output should contain two integers: the minimum number of trains that need to be canceled, and the optimal starting time t Second line of output should contain freight trains that need to be canceled.

这道题是一道结合了维护区间的数学题,也是比较难。

#include<bits/stdc++.h>
#define LL long long
#define U unsigned
bool isp(LL x){
	if(x<2)return 0;
	for(LL i=2;i<=sqrt(x);i++){
		if(x%i==0) return 0;
	}
	return 1;
}
inline LL read()
{
	LL x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
using namespace std;
int main(){
	LL n=read(),h=read(),m=read()/2,k=read();
	vector<pair<LL,LL> >vec;
	for(LL i=1;i<=n;i++){
		LL hi=read(),mi=read();
		vec.push_back({mi%m,i});
		vec.push_back({mi%m+m,i});
	}
	sort(vec.begin(),vec.end());
	pair<LL,LL>ans={0x7f7f7f7f,-1};
	LL tmp=0;
	for(LL i=0;i<2*n;i++){
		while(vec[i].first-vec[tmp].first>=k){
			tmp++;
		}
		if(i>=n){
			ans=min(ans,{i-tmp,vec[i].first});
		}
	}
	cout<<ans.first<<" "<<ans.second%m<<endl;
	for(LL i=0;i<2*n;i++){
		LL l=ans.second-k,r=ans.second;
		if(vec[i].first<r&&vec[i].first>l){
			cout<<vec[i].second;
		}
	}
	return 0;
}


E:Inverse Genealogy

Ashish has a tree consisting of n nodes numbered 1 to n rooted at node 1. The i-th node in the tree has a cost ai, and binary digit bi is written in it. He wants to have binary digit ci written in the i-th node in the end.
To achieve this, he can perform the following operation any number of times:
Select any k nodes from the subtree of any node u and shuffle the digits in these nodes as he wishes, incurring a cost of k⋅au. Here, he can choose k ranging from 1 to the size of the subtree of uu.
He wants to perform the operations in such a way that every node finally has the digit corresponding to its target.
Help him find the minimum total cost he needs to spend so that after all the operations, every node u has digit cucu written in it, or determine that it is impossible.
Input
First line contains a single integer n (1≤n≤2⋅105) denoting the number of nodes in the tree.
i-th line of the next nn lines contains 3 space-separated integers ai, bi, ci (1≤ai≤109,0≤bi,ci≤1) — the cost of the i-th node, its initial digit and its goal digit.
Each of the next n−1 lines contain two integers u, v (1≤u,v≤n, u≠v), meaning that there is an edge between nodes u and v in the tree.
Output
Print the minimum total cost to make every node reach its target digit, and −1 if it is impossible.

目前不会啊啊啊啊啊啊啊啊啊(布施明脸)

F:Chess Strikes Back

Ildar and Ivan are tired of chess, but they really like the chessboard, so they invented a new game. The field is a chessboard 2n×2m: it has 2n rows, 2m columns, and the cell in row i and column j is colored white if i+j is even, and is colored black otherwise.
The game proceeds as follows: Ildar marks some of the white cells of the chessboard as unavailable, and asks Ivan to place n×m kings on the remaining white cells in such way, so that there are no kings attacking each other. A king can attack another king if they are located in the adjacent cells, sharing an edge or a corner.
Ildar would like to explore different combinations of cells. Initially all cells are marked as available, and then he has q queries. In each query he either marks a cell as unavailable, or marks the previously unavailable cell as available. After each query he would like to know whether it is possible to place the kings on the available cells in a desired way. Please help him!
Input
The first line of input contains three integers n, m, q (1≤n,m,q≤200000) — the size of the board and the number of queries.
q lines follow, each of them contains a description of a query: two integers i and j, denoting a white cell on the board (1≤i≤2n, 1≤j≤2m, i+j is even). If the cell (i,j) was available before the query, then it becomes unavailable. Otherwise, if the cell was unavailable, it becomes available.
Output
Output q lines, ii-th line should contain answer for a board after i queries of Ildar. This line should contain “YES” if it is possible to place the kings on the available cells in the desired way, or “NO” otherwise.

这题用到了线段树,剩余按照题意和改动线段树即可。

#include<bits/stdc++.h>
#define LL long long
#define U unsigned
#define x first
#define y second
bool isp(int x){
	if(x<2)return 0;
	for(int i=2;i<=sqrt(x);i++){
		if(x%i==0) return 0;
	}
	return 1;
}
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
using namespace std;
int n=read(),m=read(),q=read(),mintree[1200000],maxtree[1200000];
map<pair<int,int>,bool>Map;
bool flag[1200000];
set<int>lset[300000],rset[300000];
void push_up(int x){
	maxtree[x]=max(maxtree[x*2],maxtree[x*2+1]);
	mintree[x]=min(mintree[x*2],mintree[x*2+1]);
	flag[x]=flag[x*2]|flag[x*2+1]|(maxtree[x*2+1]>=mintree[x*2]);
}
void build(int now,int l,int r){
	if(l==r){
		maxtree[now]=0,mintree[now]=m+1;
	}else{
		int mid=(l+r)/2;
		build(now*2,l,mid);
		build(now*2+1,mid+1,r);
		push_up(now);
	}
	return ;
}
void update(int now,int l,int r,int t){
	if(l==r){
		maxtree[now]=rset[l].size()?*rset[l].rbegin():0;
		mintree[now]=lset[l].size()?*lset[l].begin():m+1;
		if(maxtree[now]>=mintree[now])flag[now]=1;
		else flag[now]=0;
		return ;
	}else{
		int mid=(l+r)/2;
		if(t<=mid)update(now*2,l,mid,t);
		if(t>mid)update(now*2+1,mid+1,r,t);
		push_up(now);
	}
	return ;
}
int main(){
	build(1,1,n);
	while(q--){
		int x=read()+1,y=read()+1;
		if(Map[pair<int,int>(first,second)]){
			if(y&1)rset[x/2].erase(y/2);
			else lset[x/2].erase(y/2);
		}
		else{
			if(y&1)rset[x/2].insert(y/2);
			else lset[x/2].insert(y/2);
		}
		Map[pair<int,int>(first,second)]^=1;
		update(1,1,n,x/2);
		if(!flag[1])cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值