A - Vanya and Cards
题意:已知有n张卡片,每张卡片的最大值为x,求还要多少张卡片才可以使得整个数组的和为0
思路:先将当前数组的和求出来取绝对值,再看还要多少张卡片,每张都去最大值去减。
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e3+1000;
int a[maxn];
int main()
{
int t,n,i,j,sum=0,x;
cin>>n>>x;
for(i=0;i<n;i++){
cin>>a[i];
sum+=a[i];
}
int need=abs(sum);
if(sum%x==0) cout<<need/x<<endl;
else{
cout<<need/x+1<<endl;
}
return 0;
}
B - Sereja and Contests
题意:codeforces上有两种比赛,div1和div2,每场比赛有对应的标识符(通常是一个数),然后div1通常是和div2共同进行的,所以他们的标识符相差为1,已知他参加过的比赛,求他最多和最少能错过多少场div2
思路:最多错过的场数div2就是没有参加的场数,而最少错过的,就用最多错过的场数去判断其中有没有相邻的,那么就可以当作一场div2和div1相邻,最小场数–.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,i,j,t;
int d1,x,k,xx,yy,cnt1=0;
cin>>x>>k;
n=x;
map<int ,int >m1;
for(i=0;i<k;i++){
cin>>d1;
if(d1==1){
cin>>xx>>yy; cnt1+=2;
m1[xx]++,m1[yy]++;
}
else {
cin>>xx; cnt1++; m1[xx]++;
}
}
x-=cnt1; x--;
int mn=x;
for(i=2;i<n;i++){
if(m1[i]==0&&m1[i-1]==0){
mn--; i++;
}
}
cout<<mn<<" "<<x<<endl;
return 0;
}
C. Team
题意:构造一个 01 序列,包含 n 个 0,m 个 1 要求不存在连续 2 个 0,或 3 个 1,1 <= n; m<=1000000
思路:主要是判断n和m的大小关系:
①:n和m相等时,输出10101010…即可
②:n>m时,0比1多时,只有多1个才能有答案像:010
③:n<m时,如果m>(n+1)2,无解
如果m>=(2n) 可以输出110110110…
如果2*n>m>n时,想把1010101插空放好,然后看剩下来几个 1再从头放变成1101101010.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,i,m,j;
cin>>n>>m;
if(n==m){
for(i=0;i<n;i++)
cout<<"10";
cout<<endl;
}
else if(n>m){
if(m==(n-1)){
int cnt1=m;
for(i=0;i<n-1;i++){
cout<<"01"; cnt1--;
}
cout<<0<<endl;
}
else {
cout<<-1<<endl;
}
}
else {
if(m>(n+1)*2){
cout<<-1<<endl;
}
else if(m>=2*n) {
int cnt1=m;
for(i=0;i<n;i++){
cout<<"110";
cnt1-=2;
}
for(i=0;i<cnt1;i++){
cout<<1;
}
cout<<endl;
}
else if(2*n>m&&m>n){
int need1=n,need2=m; //m=1
for(i=1;i<=n;i++){
need2--,need1--;
}
need2--;
int m1=need2;
for(i=1;i<=n;i++){
if(need2>0){
cout<<"110";
need2--;
}
else {
cout<<"10";
}
}
cout<<1<<endl;
}
}
return 0;
}