A. Magical Sticks
题目链接
https://codeforces.com/contest/1371/problem/A
思路
稍微想一下,很明显的求中位数即可
代码
#include<cstdio>
#include<iostream>
#include<math.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
long long n;
cin>>n;
cout<<(n+1)/2<<endl;
}
return 0;
}
B. Magical Calendar
题目链接
https://codeforces.com/contest/1371/problem/B
思路
一周的长短为1,一种可能性,为2,选第一个或者第二个两种,一周长是n,可以从第i∈[1,n]天开始涂色,即可求得公式为r*(r+1)/ 2,
当r大于等于n时,就只有一种可能,从第一天开始放,放满n天。
代码
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
long long n, r;
cin>>n>>r;
if(r>=n){
r=min(n-1, r);
cout<<r*(r+1)/2+1<<endl;
}
else
cout<<r*(r+1)/2<<endl;
}
return 0;
}
C. A Cookie for You
题目链接
https://codeforces.com/contest/1371/problem/C
思路
1、让第二类客人先吃,最多吃min(a,b)
2、满足第二类客人后,让第一类客人吃多的,相等后即可交替吃,最多将两种饼干吃完
检查满足两种情况即可
代码
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
long long a, b, n, m;
cin >> a >> b >> n >> m;
int flag = 1;
if(a+b<n+m)
flag=0;
else if (min(a,b)<m)
flag=0;
else if((a+b-m)<a)
flag=0;
if(flag)
cout<<"Yes"<<endl;
else
cout<<"NO"<<endl;
}
}
D. Grid-00100
题目链接
https://codeforces.com/contest/1371/problem/D
思路
1、k能整除n时,每行每列相同,为0
对角线上放1即可
2、不能整除时,行和列都会至少相差1个,为2
余下的数字继续按照上面的方法去放
将图像作如下修改方便理解,即将左下三角形移到右下,主对角线即为最右的边,当放1大于n时,对n取余(下标从0开始)
代码
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int n, k, a[309][309];
memset(a,0,sizeof(a));
cin>>n>>k;
int y=k%n;
int c=k/n;
cout<< (y==0 ? 0:2) <<endl;
for(int i=0; i<y; i++)
for(int j=i; j<i+c+1; j++)
a[i][j%n]=1;
for(int i=y; i<n; i++)
for(int j=i; j<i+c; j++)
a[i][j%n]=1;
for(int i=0; i<n; i++){
for(int j=0; j<n; j++)
cout<<a[i][j];
cout<<endl;
}
}
return 0;
}