#include<bits/stdc++.h>
using namespace std;#definelllonglong#definelson(x)(1<< x)#definerson(x)(1<< x |1)#definefo(i,n)for(int i =1;i <= n;i++)#defineyescout <<"yes"<< endl#definenocout <<"no"<< endlconstint MAXN =2e5+10;intmain(){int t;cin >> t;while(t--){int mil =1e9,mir =1e9,mi =1e9;int n;cin >> n;fo(i,n){int x;cin >> x;
string str;cin >> str;if(str =="01")
mir =min(x,mir);elseif(str =="10")
mil =min(x,mil);elseif(str =="11")
mi =min(mi,x);
mi =min(mi,mil + mir);}if(mi ==1e9)
cout <<-1<< endl;else
cout << mi << endl;}}
D. Gold Rush
题意:一堆n个物品,一次操作可以分为大的一堆是小的一堆的两倍的两堆,问能否分出m个物品的堆
思路:小的一堆为
n
/
3
n/3
n/3,大的为
2
n
/
3
2n/3
2n/3,那么将n除3,如果比m小就乘2,然后2的数量不大于3的数量,直到n不能除3或相等为止,如果到最后都没找到,那么说明不能,否则可以
#include<bits/stdc++.h>
using namespace std;#definelllonglong#definelson(x)(1<< x)#definerson(x)(1<< x |1)#definefo(i,n)for(int i =0;i <= n;i++)#defineyescout <<"yes"<< endl#definenocout <<"no"<< endlintmain(){int t;cin >> t;while(t--){int n,m;cin >> n >> m;if(m > n){
no;continue;}
bool f =0;int time =0;if(n == m)
f =1;while(n %3==0){
n /=3;
time++;while(n < m && time){
time--;
n *=2;}if(n == m)
f =1;}if(n == m)
f =1;if(f)
yes;else
no;}}
E. The Lakes
题意:找最大的相连的块
思路:dfs
#include<bits/stdc++.h>
using namespace std;#definelllonglong#definelson(x)(1<< x)#definerson(x)(1<< x |1)#definefo(i,n)for(int i =1;i <= n;i++)#defineyescout <<"yes"<< endl#definenocout <<"no"<< endlconstint MAXN =1e3+10;int a[MAXN][MAXN];int n,m;int now =0;voiddfs(int i,int j){if(!a[i][j])return;
now += a[i][j];
a[i][j]=0;if(i +1<= n)dfs(i +1,j);if(i -1>=1)dfs(i -1,j);if(j +1<= m)dfs(i,j +1);if(j -1>=1)dfs(i,j -1);}intmain(){int t;cin >> t;while(t--){
cin >> n >> m;int ma =0;fo(i,n)fo(j,m) cin >> a[i][j];fo(i,n)fo(j,m){if(a[i][j]){
now =0;dfs(i,j);
ma =max(now,ma);}}
cout << ma << endl;}}