

貪心題,奇數位的字符取可以取的最小,偶數位的字符取可以取的最大,特別當奇數位為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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/258046.html
標籤:其他
