A题
http://codeforces.com/contest/1360/problem/A
思路:题目要求最小长度为多大的正方形可以包括这两个给出的矩形。找出矩形中最大的边 a作为矩形的长,如果 a>2b(b 为矩形的宽),则 正方形的长为 a,否则正方形的长为 2b,由边长即可求面积
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
int t;
scanf("%d",&t);
while(t--){
int a,b;
scanf("%d%d",&a,&b);
if(a==b){
printf("%d\n",4*a*a);
continue;
}
if(a<b){
int temp;
temp = a;
a = b;
b = temp;
}
if(a>2*b){
printf("%d\n",a*a);
}else{
printf("%d\n",4*b*b);
}
}
return 0;
}
B题
http://codeforces.com/contest/1360/problem/B
思路:题目要求找出子数组A的最大值-子数组B的最小值之差的绝对值最小,其实只要找出数组中相邻元素之差最小,然后将两个元素分别放到子数组A、B当中即可
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <climits>
using namespace std;
int arr[100];
int main()
int t;
scanf("%d",&t);
while(t--){
int num;
scanf("%d",&num);
for(int i = 0; i<num; i++){
scanf("%d",&arr[i]);
}
int Min = INT_MAX;
//找出相邻数最小的就行
sort(arr,arr+num);
for(int i = 1; i<num; i++){
Min = min(Min,arr[i]-arr[i-1]);
}
printf("%d\n",Min);
}
return 0;
}
C题
http://codeforces.com/contest/1360/problem/C
思路:满足(x,y) 相似的条件:(1)x,y同为奇数或偶数(2)x,y的绝对值相差为 1 。若数组中偶数的个数为偶数,由于数组的长度 n 为偶数,所以奇数的个数也为偶数,则数组中的奇数和偶数各自两两配对即可,否则的话,将数组降序排序,判断是否满足两个数的差为1,如果以上两种情况都不满足,输出“NO”
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
int main(){
int t;
scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);
int count = 0;
int arr[105];
for(int i=0;i<n; i++){
scanf("%d",&arr[i]);
if(arr[i]%2){
count++;
}
}
if(count%2==0){
printf("YES\n");
}else{
sort(arr,arr+n);
int flag = 0;
for(int i = 1; i<n; i++){
if(arr[i]-arr[i-1]==1){
flag = 1;
break;
}
}
if(flag)
printf("YES\n");
else
printf("NO\n");
}
}
return 0;
}
D题
http://codeforces.com/contest/1360/problem/D
思路:枚举 n 的所有因子,寻找最大的公约数 m,所求 packages = n/m
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
//找最大的约数
int main(){
int t;
scanf("%d",&t);
while(t--){
int a,b;
scanf("%d%d",&a,&b);
if(a<=b){
printf("1\n");
continue;
}
int flag = a;
int val = (int)sqrt(a);
int temp = min(b,val);
for(int i = 2; i<=temp; i++){
if(a%i==0){
if(a/i<=b)
flag = min(i,flag);
if(i<=b){
flag = min(flag,a/i);
}
}
}
printf("%d\n",flag);
}
return 0;
}
E题
http://codeforces.com/contest/1360/problem/E
思路:在方阵中,如果发射了一个炮弹,那么存在两种情况:一是没有阻碍直到边界停止 ;二是停在某个位置(非边界)
假设炮弹停在了a[i][j]的位置,那么在炮弹的右方(a[i+1][j])或下方(a[i][j+1])有炮弹,不然该炮弹不可能在a[i][j]的位置上停止。因此,遍历每个位置(边界可以不用判断),判断是否存在存在炮弹的位置右方或下方却没有炮弹的情况。
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
char arr[100][100];
int main(){
int t;
scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);
for(int i = 0; i<n; i++){
for(int j = 0; j<n; j++){
cin>>arr[i][j];
}
}
int flag = 1;
for(int i = 0;i<n-1; i++){
for(int j = 0; j<n-1; j++){
if(arr[i][j]=='1'){
if(arr[i][j+1]=='0' && arr[i+1][j]=='0'){
//右边和下边是否有炮弹
flag = 0;
break;
}
}
}
}
if(flag)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
F题
http://codeforces.com/contest/1360/problem/F
思路:如果给定的n个长度相同的字符串字符不同的个数 <=1,则选择其中一个作为输出结果即可满足题意。如果 >1,选取其中一个字符串,对字符串的每一个位置枚举26个字母,比较是否存在与其他给定的字符串不同的字母个数小于等于1的情况,如果有,则可中断循环,输出 “YES”,否则,则说明不存在此种情况。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
string str[20];
int m,n;
bool check(string s){
for(int i = 1;i<m; i++){
int count = 0;
for(int j = 0;j<s.size();j++){
if(s[j]!=str[i][j])
count++;
}
if(count>1)
return 0;
}
return 1;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&m,&n);
for(int i = 0; i<m; i++){
cin>>str[i];
}
string temp;
temp= str[0];//temp等于其中一个字符串
int flag = 0;
flag = check(temp);
if(flag){
cout<<str[0]<<endl;
}else{
flag = 0;
for(int i = 0; i<n; i++){
temp = str[0];
for(int j = 0;j<26; j++){
temp[i] = 'a'+j;
if(check(temp)){
flag = 1;
cout<<temp<<endl;
break;
}
}
if(flag){
break;
}
}
if(!flag){
printf("-1\n");
}
}
}
return 0;
}
G题
http://codeforces.com/contest/1360/problem/G
思路:题意是让我们构造 n 行 m 列的数组,每行有 a 个1,每列有 b 个1,即每行的和为 a,每列的和为 b。因为每个 1 既在行上又在列上,所以只有当 na = mb时有解。输出序列可以采用交错循环的方法,每一行的输出1的位置在上一行的最后一个1之后。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main(){
int t;
scanf("%d",&t);
while(t--){
int n,m,a,b;
scanf("%d%d%d%d",&n,&m,&a,&b);
if(n*a != m*b){
printf("NO\n");
}else{
printf("YES\n");
int pos = 0;
for(int i = 0; i<n; i++){
string s(m,'0');
for(int j = 0; j<a; j++){
s[(j+pos)%m] = '1';
}
cout<<s<<endl;
pos = (pos+a)%m;
}
}
}
return 0;
}
H题
http://codeforces.com/contest/1360/problem/H
思路:假设最终所求得的中位数是 mid,如果去掉的数都比 mid(0–n-1)小的话,那么中位数的位置不变,如果比mid大,那么可以将去掉的数按照从大到小排序,并比mid大的数移到末尾,相应的中位数左移一位即可(可以动手在草稿纸上模拟)
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
#define LL long long
char ans[61];
bool compare(int a,int b){
return a>b;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
int n,m;
scanf("%d%d",&n,&m);
LL val[105];
//将输入的二进制转化为十进制
for(int i = 0; i<n; i++){
LL sum = 0;
string s;
cin>>s;
for(int j = 0;j<s.size();j++){
if(s[j]=='1'){
sum+= 1LL<<(s.size()-j-1);
}
}
val[i] = sum;
}
LL left = n,right = (1LL<<m)-1;
LL mid = (left+right)/2;
//从大到小排序
sort(val,val+n,compare);
for(int i = 0;i<n; i++){
if(val[i]>=mid){
mid--;
}
}
ans[m]='\0';
//将得到的最终结果转化为 2进制输出
for(int k = m-1;k>=0;k--){
ans[k] = (mid%2)+'0';
mid = mid/2;
}
cout<<ans<<endl;
}
}