Codeforces Round #657 (Div. 2) A-D

A:枚举哪7个连续字串是abacaba即可,注意一些细节处理

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ls (o<<1)
#define rs (o<<1|1)
#define pb push_back
const double PI= acos(-1.0);
const int M = 1e5+7;
 
char s[110];
char t[10]="abacaba";
char p[110];
int main()
{
	ios::sync_with_stdio(false);
  	cin.tie(0);
  	int T;
  	cin>>T;
  	while(T--)
  	{
  		int n;
  		memset(p,0,sizeof(p));
  		cin>>n>>p;
  		bool z=true;
  		for(int i=0;i+6<n;i++)
  		{
  			memset(s,0,sizeof(s));
  			for(int j=0;j<n;j++)s[j]=p[j];
  			bool f=true;
  			for(int j=i;j<i+7;j++)
  			{
  				if(s[j]=='?'||s[j]==t[j-i])continue;
  				else
  				{
  					f=false;
  					break;
				}
			}
			if(!f)continue;
		//	cout<<i<<"  -= "<<endl;
			for(int j=i;j<i+7;j++)s[j]=t[j-i];
			
			for(int j=0;j+6<n;j++)
			{
				if(j==i)continue;
				int nm=0;
				for(int k=j;k<j+7;k++)
				{
					if(s[k]=='?'||s[k]!=t[k-j])continue;
					else nm++;
				}
				if(nm==7)
				{
					f=false;
					break;
				}
			}
			if(f)
			{
				z=false;
				cout<<"Yes"<<endl;
				for(int j=0;j<n;j++){
					if(s[j]=='?')s[j]='d';
					cout<<s[j];
				}
				cout<<endl;
				break;
			}
		}
		if(z)cout<<"No"<<endl;
	}
	return 0;
}

B:枚举a,然后b-c最多上下浮动 r-l,所以只需要找到m-n*a的最小绝对值,看b-c是否能凑成即可。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ls (o<<1)
#define rs (o<<1|1)
#define pb push_back
const double PI= acos(-1.0);
const int M = 1e5+7;
/*
int head[M],cnt=1;
void init(){cnt=1,memset(head,0,sizeof(head));}
struct EDGE{int to,nxt,w;}ee[M*2];
void add(int x,int y,int w){ee[++cnt].nxt=head[x],ee[cnt].w=w,ee[cnt].to=y,head[x]=cnt;}
*/
 
int main()
{
	ios::sync_with_stdio(false);
  	cin.tie(0);
  	int t;
  	cin>>t;
  	while(t--)
  	{
  		ll l,r,m;
  		cin>>l>>r>>m;
  		int a,b,c;
  		for(int i=l;i<=r;i++)
  		{
  			a=i;
  			int tp=a-m%a;
  			b=l,c=l+tp;
  			if(c<=r)
  			{
  				cout<<a<<" "<<b<<" "<<c<<endl;
  				break;
			}
			
			tp=a-tp;
  			b=r;c=r-tp;
  			if(c>=l)
  			{
  				cout<<a<<" "<<b<<" "<<c<<endl;
  				break;
			}
  			
		}
	}
	return 0;
}

C:显然,至多只有一种花买一朵以上。(因为买一朵以上时的收益为b[i],显然直接所有都用收益最高的那一种花即可)

然后枚举哪一种花买一朵以上然后处理下即可。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ls (o<<1)
#define rs (o<<1|1)
#define pb push_back
const double PI= acos(-1.0);
const int M = 1e5+7;
/*
int head[M],cnt=1;
void init(){cnt=1,memset(head,0,sizeof(head));}
struct EDGE{int to,nxt,w;}ee[M*2];
void add(int x,int y,int w){ee[++cnt].nxt=head[x],ee[cnt].w=w,ee[cnt].to=y,head[x]=cnt;}
*/
struct node{
	int a,b;
	bool operator < (const node & r)const{
		return a<r.a;
	}
}p[M];
ll sm[M];
int main()
{
	ios::sync_with_stdio(false);
  	cin.tie(0);
  	int t;
	cin>>t;
	while(t--)
	{
		int n,m;
		cin>>n>>m;
		for(int i=1;i<=m;i++)cin>>p[i].a>>p[i].b;
		sort(p+1,p+1+m);
		for(int i=1;i<=m;i++)sm[i]=sm[i-1]+p[i].a;
		sm[m+1]=sm[m];
		ll mx=0;
		for(int i=1;i<=m;i++)//m枚举哪一种花买至少一朵
		{
			int pos=lower_bound(p+1,p+1+m,node{p[i].b,0})-p;
			int res=n-(m+1-pos);
			ll tp=0;
			if(p[i].a<p[i].b)res--,tp+=p[i].a;
			if(res<0)
			{
				mx=max(mx,sm[m]-sm[m-n]);
				continue;
			}
			tp+=sm[m+1]-sm[pos-1];
		//	cout<<res<<"  "<<pos<< "  "<<tp<<endl;
			tp+=(ll)p[i].b*res;
			mx=max(mx,tp);
		}
		cout<<mx<<endl;
	} 
	return 0;
}

D:

显然,客车占用时间段与某一量客车的发车时间重合最优。

枚举起始时间t,然后二分处理即可。

不过我写的比较麻烦。

更简单的做法:把时间%m/2,然后尺取即可。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
const int M = 1e5+7;
struct node{
	int h,m,id;
	bool operator <(const node & r)const{
	//	if(h==r.h)return m<r.h;
		return m<r.m;
	}
}p[M];
int a[M];
int main()
{
	ios::sync_with_stdio(false);
  	cin.tie(0);
  	int n,h,m,k;
  	cin>>n>>h>>m>>k;
  	for(int i=1;i<=n;i++)cin>>p[i].h>>p[i].m,p[i].id=i;
  	sort(p+1,p+1+n);
  	int mi=n+1,ans=0;//最少需要取消的货车数量 /t最终取值 
  	for(int i=1;i<=n;i++)a[i]=p[i].m;
	int tp,id,nm,l;
	for(int i=1;i<=n;i++)
	{
		tp=p[i].m;
		{
			nm=0;
			tp %= m/2;
			id=upper_bound(a+1,a+1+n,tp-k)-a;
			int r=lower_bound(a+1,a+1+n,tp)-a;
			nm+=max(0,r-id);//(t-k,t)
			if(tp<k){
				id=upper_bound(a+1,a+1+n,m-(k-tp))-a;
				nm+=max(0,n+1-id);//(m-(k-tp),m)
			}
			l=upper_bound(a+1,a+1+n,tp+m/2-k)-a;
			id=lower_bound(a+1,a+1+n,tp+m/2)-a;
			nm+=max(0,id-l);//(t+m/2-k,t+m/2)
		//	cout<<" - -- -- - "<<id<<" "<<l<<endl;
			if(tp+m/2>m){
				id=lower_bound(a+1,a+1+n,tp+m/2-m)-a;
				nm+=max(id-1,0);
			}
		}
		//cout<<i<<" "<<nm<<" "<<tp<<endl;
		if(nm<mi)mi=nm,ans=tp;
		tp=(p[i].m+k)%m;
		{
			nm=0;
			tp %= m/2;
			id=upper_bound(a+1,a+1+n,tp-k)-a;
			int r=lower_bound(a+1,a+1+n,tp)-a;
			nm+=max(0,r-id);//(t-k,t)
			if(tp<k){
				id=upper_bound(a+1,a+1+n,m-(k-tp))-a;
				nm+=max(0,n+1-id);//(m-(k-tp),m)
			}
			l=upper_bound(a+1,a+1+n,tp+m/2-k)-a;
			id=lower_bound(a+1,a+1+n,tp+m/2)-a;
			nm+=max(0,id-l);//(t+m/2-k,t+m/2)
		//	cout<<" - -- -- - "<<id<<" "<<l<<endl;
			if(tp+m/2>m){
				id=lower_bound(a+1,a+1+n,tp+m/2-m)-a;
				nm+=max(id-1,0);
			}
		}
		if(nm<mi)nm=mi,ans=tp;
	}
	cout<<mi<<" "<<ans<<endl;
	vector<int>v;
	int t=ans;
	for(int i=1;i<=n;i++)
	{
		int z=a[i],id=p[i].id;
		bool f=false;
		if(z<t&&z>t-k)f=true;
		if(z>t-k+m)f=true;
		if(z>t+m/2-k&&z<t+m/2)f=true;
		//cout<<" ==  = "<<tp+m/2-k<<"  "<<t
		if(z<t+m/2-m)f=true;
		if(f)v.pb(id);
	//	cout<<i<<" "<<z<<" "<<f<<"  "<<t<<endl;
	}
	sort(v.begin(),v.end());
	for(auto x:v)cout<<x<<" ";
	cout<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/bjfu170203101/article/details/107477911
今日推荐