Codeforces Round #700 (Div. 2)-AB题

在这里插入图片描述
在这里插入图片描述
贪心题,奇数位的字符取可以取的最小,偶数位的字符取可以取的最大。特别当奇数位为a,偶数位位z的时候,因为可取范围内的最小为b,最大为y,其余的奇数位改成a,偶数位改成z

#include<bits/stdc++.h>
using namespace std;
int main()
{
    
    
	ios::sync_with_stdio(false);
	cin.tie(0);
	int t;
	cin>>t;
	while(t--)
	{
    
    
		char s[55];
		cin>>s;
		int n=strlen(s);
		for(int i=1;i<=n;i++)
		{
    
    
			if(i%2==0)
			{
    
    
				if(s[i-1]=='z')
				s[i-1]='y';
				else
				s[i-1]='z';
			}
			else
			{
    
    
				if(s[i-1]=='a')
				s[i-1]='b';
				else
				s[i-1]='a';
			}
		}
		cout<<s<<endl;
	}
	return 0;
}

在这里插入图片描述
在这里插入图片描述
贪心题,因为最后一下可以被怪兽杀死,所以让攻击力最高的怪兽打最后一下,这样保证最优解。

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
struct node
{
    
    
	long long ai,bi;
};
bool cmp(node a,node b)
{
    
    
	return a.ai<b.ai;
}
node arr[N];
int main()
{
    
    
	ios::sync_with_stdio(false);
	cin.tie(0);
	int t;
	long long  a,b,n;
	cin>>t;
	while(t--)
	{
    
    
		cin>>a>>b>>n;
		for(int i=1;i<=n;i++)
		{
    
    
			cin>>arr[i].ai;
		}
		for(int i=1;i<=n;i++)
		{
    
    
			cin>>arr[i].bi; 
		}
		sort(arr+1,arr+n+1,cmp);
		int flag=1;
		int h=1;
		while(1)//注意判断退出条件
		{
    
    
			if(h<=n&&arr[h].bi<=0)
			{
    
    
				h++;
			}
			else if(h<=n&&arr[h].bi>0)
			{
    
    
				arr[h].bi-=a;
				b-=arr[h].ai;
			}
			if(b<=0)
			{
    
    
				if(h==n&&arr[h].bi<=0)
				break;
				else
				{
    
    
					flag=0;
					break;
				}
			}
			if(h==n+1)
			{
    
    
				break;
			}
		}
		if(flag)
		cout<<"YES"<<endl;
		else
		cout<<"NO"<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_51768569/article/details/113753753