目录
A. Elections
思路:
分类讨论。
代码:
#include <bits/stdc++.h>
typedef long long int LL;
using namespace std;
LL max(LL a,LL b,LL c)
{
LL maxn=max(a,b);
maxn=max(maxn,c);
return maxn;
}
int main()
{
LL t;
cin>>t;
while(t--)
{
LL a,b,c;
cin>>a>>b>>c;
int maxn=max(a,b,c);
if(a>b&&a>c)
{
cout<<0<<" "<<maxn-b+1<<" "<<maxn-c+1<<endl;
}
else if(b>a&&b>c)
{
cout<<maxn-a+1<<" "<<0<<" "<<maxn-c+1<<endl;
}
else if(c>a&&c>b)
{
cout<<maxn-a+1<<" "<<maxn-b+1<<" "<<0<<endl;
}
else
{
cout<<maxn-a+1<<" "<<maxn-b+1<<" "<<maxn-c+1<<endl;
}
}
}
B. Make it Divisible by 25
思路:
字符串处理。
reverse反转一下,然后找00,05,52,57 (正向的 00 50 25 75)
代码:
#include <bits/stdc++.h>
#define bbn 200005
#define maxint 2147483647
#define maxLLint 9223372036854775807
#define mod 1000000007 //1e9+7
const double eps=1e-7;
typedef long long int LL;
using namespace std;
int t;
void work()
{
cin>>t;
while(t--)
{
string s;
cin>>s;
int n=s.size();
int minn=maxint;
reverse(s.begin(),s.end());
for(int i=0; i<=n-1; i++)
{
if(s[i]=='0')
{
for(int j=i+1; j<=n-1; j++)
{
if(s[j]=='0'||s[j]=='5')
{
minn=min(minn,j-1);
}
}
}
if(s[i]=='5')
{
for(int j=i+1; j<=n-1; j++)
{
if(s[j]=='2'||s[j]=='7')
{
minn=min(minn,j-1);
}
}
}
}
cout<<minn<<'\n';
}
}
int main()
{
work();
return 0;
}
C. Save More Mice
思路:
贪心,每次救最靠近洞穴的老鼠。
预先sort排序一下老鼠。
代码:
#include <bits/stdc++.h>
#define bbn 400005
#define maxint 2147483647
#define maxLLint 9223372036854775807
#define mod 1000000007 //1e9+7
const double eps=1e-7;
typedef long long int LL;
using namespace std;
int t;
void work()
{
cin>>t;
while(t--)
{
int p,k;
int cat=0;
int mouse=0;
int a[bbn]= {};
cin>>p>>k;
for(int i=1; i<=k; i++)
{
cin>>a[i];
}
sort(a+1,a+k+1);
for(int i=k; i>=1; k--)
{
if(cat>=a[k])
{
break;
}
cat+=p-a[k];
mouse++;
}
cout<<mouse<<'\n';
}
}
int main()
{
work();
return 0;
}
D1. All are Same
思路:
求差值(差值>0)的最大公因数。
如果所有数相同,输出-1。
方便起见,用set去除重复数。
代码:
#include <bits/stdc++.h>
#define bbn 400005
#define maxint 2147483647
#define maxLLint 9223372036854775807
#define mod 1000000007 //1e9+7
const double eps=1e-7;
typedef long long int LL;
using namespace std;
int t;
int gcd(int a,int b)
{
return b!=0?gcd(b,a%b):a;
}
void work()
{
cin>>t;
while(t--)
{
int n;
set<int>s;
set<int>q;
cin>>n;
for(int i=1,x; i<=n; i++)
{
cin>>x;
s.insert(x);
}
if(s.size()==1)
{
cout<<-1<<endl;
continue;
}
for(auto i:s)
{
for(auto j:s)
{
if(i==j)
{
continue;
}
else
{
q.insert(abs(i-j));
}
}
}
int ans;
for(int i:q)
{
ans=i;
break;
}
for(int i:q)
{
ans=gcd(ans,i);
}
cout<<ans<<endl;
}
}
int main()
{
work();
return 0;
}