ICL2019E

https://www.codechef.com/ICL2019/problems/ICL1906

 两个整数,[0,1e5]
操作1是让两个数同时减1(只有都大于0的时候才可以用)
操作2可以让一个数乘2
问让两个数都变成0的最小操作次数 
 
直接贪心。能乘就乘。
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int t,x,y;
 4 int main(){
 5     ios::sync_with_stdio(false);
 6     cin>>t;
 7     while (t--){
 8         cin>>x>>y;
 9         if(x==0&&y==0){
10             cout<<0<<endl;
11             continue;
12         }
13         if(x==0||y==0){
14             cout<<-1<<endl;
15             continue;
16         }
17         if(x>y)swap(x,y);
18         int ans = 0;
19         while (x!=y){
20             if(x*2<=y){
21                 x*=2;
22                 ans++;
23             }else{
24                 y--,x--;
25                 ans++;
26             }
27         }
28         ans+=x;
29         cout<<ans<<endl;
30     }
31 }
View Code

猜你喜欢

转载自www.cnblogs.com/MXang/p/10624773.html