A题:Binary Decimal
题意:给定n,问最少有几组仅由01组成的数字,相加等于n。
数据范围:多组输入t<1000,n<1e9
样例解释:
输入:
3
121
5
1000000000
输出:
2
2 1
3 1
-1
0
6
1 4
1 4
1 5
1 5
2 5
2 5
第一组:121=111+10
第二组:5=1+1+1+1+1
第三组:1000000000为本身
思路:判断每一位上的数字,取最大值即为答案。
分类:思维
通过代码:
#include<bits/stdc++.h>
#include<bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t--) {
int n,maxx=0;
cin>>n;
while(n>0) {//取每一位
maxx=max(n%10,maxx);
n=n/10;
}
cout<<maxx<<endl;
}
return 0;
}
B题:Putting Plates
题意:给定h,w为矩阵大小,在矩阵最外一圈放置1,1的周围(八个方向)不能有1,输出1最多的图。
数据范围:多组输入t<100,h,w<20
数据范围:多组输入t<1e4,|s|<26
样例解释:
输入:
3
3 5
4 4
5 6
输出:
10101
00000
10101
0100
0001
1000
0010
010101
000000
100001
000000
101010
思路:数据范围小,暴力+贪心。写一个check函数,判断周围是否有1。
分类:暴力
通过代码:
#include<bits/stdc++.h>
using namespace std;
int a[30][30];
int n,m;
int check(int x,int y) {
if(a[x+1][y]==1&&x+1<n)return 0;
if(a[x-1][y]==1&&x-1>=0)return 0;
if(a[x][y+1]==1&&y+1<m)return 0;
if(a[x][y-1]==1&&y-1>=0)return 0;
if(a[x+1][y+1]==1&&x+1<n&&y+1<m)return 0;
if(a[x+1][y-1]==1&&y-1>=0&&x+1<n)return 0;
if(a[x-1][y+1]==1&&y+1<m&&x-1>=0)return 0;
if(a[x-1][y-1]==1&&x-1>=0&&y-1>=0)return 0;
return 1;
}
int main() {
int t;
cin>>t;
while(t--) {
memset(a,0,sizeof a);
cin>>n>>m;
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
if(i==0||i==n-1||j==0||j==m-1) {
if(check(i,j)==1) {
a[i][j]=1;
}
}
}
}
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
cout<<a[i][j];
}
cout<<endl;
}
cout<<endl;
}
return 0;
} for(auto x:v)cout<<(char)x;
cout<<endl;
}
return 0;
}
C题:Pursuit
题意:给定数组长度n,数组an,bn。贡献为取k-k/4个元素的最大值。求最少需要多少轮,a可以超过b。
数据范围:多组输入t<1000,n<1e5,0<ai,bi<100
样例解释:
输入:
5
1
100
0
1
0
100
4
20 30 40 50
100 100 100 100
4
10 20 30 40
100 100 100 100
7
7 59 62 52 27 31 55
33 35 50 98 83 80 64
输出:
0
1
3
4
2
思路:贪心往a组每次+100,b组每次+0。维护一个数组指针来模拟即可。
分类:模拟
通过代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[300005],b[300005];
int main() {
ll t;
cin>>t;
while(t--) {
ll n;
cin>>n;
ll sum1=0,sum2=0;
for(int i=1; i<=n; i++) cin>>a[i];
for(int i=1; i<=n; i++) cin>>b[i];
sort(a+1,a+1+n);
sort(b+1,b+1+n);
for(int i=1; i<=n-n/4; i++) {
sum1+=a[n-i+1];//处理当前和
sum2+=b[n-i+1];
}
ll now=n-(n-n/4)+1;//当前位
ll x=now-1;
ll ans=0;
while(sum1<sum2) {
ans++;
if((ans+n)%4==0) {//满四个
sum1-=a[now];//减去最小的
now++;//前移
} else {
if(x>=0) {
sum2+=b[x];
x--;
}
}
sum1+=100;//贪心加100
}
cout<<ans<<endl;
}
return 0;
}