C - Flippy Sequence
水题
#include<bits/stdc++.h>
using namespace std;
int a[10000000];
bool c[10000000];
int main()
{
int T;
scanf("%d",&T);
int n;
int cnt;
long long l;
while(T--)
{
scanf("%d\n",&n);
cnt=0;
l=1;
for(int i=0;i<n;++i)a[i]=getchar();
getchar();
for(int i=0;i<n;++i)c[i]=(getchar()!=a[i]);
if(n==1)
{
if(c[0])cout<<"0\n";
else cout<<"1\n";
}
else
{
if(c[0])cnt=1;
for(int i=1;i<n;++i)
if(c[i]&&c[i-1])l++;
else if(c[i]&&(!c[i-1])){cnt++;l=1;}
//cout<<"cnt"<<cnt<<'\n';
if(cnt==0)cout<<(long long)n*(long long)(n+1)/2<<'\n';
if(cnt==1)cout<<((n-1)*2)<<'\n';
if(cnt==2)cout<<"6\n";
if(cnt>2)cout<<"0\n";
}
}
return 0;
}
E - Plants vs. Zombies
二分答案+贪心构造
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
long long a[maxn], h[maxn], n, m, mx;
void read_ll(long long &x){
char c=getchar();
x=0;
while(c==' '||c=='\n'||c=='\r'){
c=getchar();
}
while(c!=' '&&c!='\n'&&c!='\r'){
x=x*10+c-'0';
c=getchar();
}
return ;
}
void read(){
mx=0;
read_ll(n); read_ll(m);
for (long long i=1;i<=n;++i){
read_ll(a[i]);
mx=max(mx,a[i]);
}
a[n+1]=0;
return ;
}
inline bool pj(long long x){
long long res=m, cost;
long long temp, nex;
nex=0;
for (int i=0;i<=n;++i){
temp=nex; nex=x;
if (res<0){
return false;
}
if (temp<=0){
if (i==n){
break;
}
nex-=a[i+1];
res--;
continue;
}
cost = temp/a[i];
if (temp%a[i]!=0){
cost++;
}
res-=cost;
res-=cost;
nex-=cost*a[i+1];
if (i+1>=n){
if (i+1==n){
if (nex<=0){
if (res>=0){
return true;
}else{
return false;
}
}else{
res--;
nex-=a[i+1];
continue;
}
}else{
if (res>=0){
return true;
}else{
return false;
}
}
}else{
res--;
nex-=a[i+1];
}
if(res<0){
return false;
}
}
return true;
}
void sol(){
long long l,r,mid,ans=0;
l=0;r=mx*m;
if (m<n){
printf("0\n");
return ;
}
while(1){
mid=(l+r)>>1;
if (l+1==r)
break;
if(pj(mid))
{
l=mid;
}else{
r=mid;
}
}
printf("%lld\n",l);
return ;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
read();
sol();
}
return 0;
}
F - Tournament
构造题,最晚开的,因为时候不早了(要被看楼大爷赶人了)就先嘴炮了
因为如果1和2打了,3和4打了,那么如果1和3打必须2和4打!(很重要的前提条件)
那就抱团啊,首先抱2人团,互打,打完这个团就和这个团抱起来挑下一个团。
最少次数即为最小的那个团所能打的次数。
J - Books
#include<iostream>
#include<cstdio>
#define ll long long
using namespace std;
const int maxn = 1e5 +50;
ll a[maxn];
int cnt = 0;
int main(){
int T;
scanf("%d",&T);
while(T--){
int n,m;scanf("%d%d",&n,&m);
cnt = 0;
int num = 0;
for(int i=0;i<n;++i) {
ll t;
scanf("%I64d",&t);
if(t) a[cnt++] = t;
else num++;
}//num = 0的个数
if(n==m){
printf("Richman\n");continue;
}
int res = m - num;
if(res<0){
printf("Impossible\n");continue;
}
ll ans = 0;
int i;
for(i=0;i<cnt;++i){
if(res){
ans+=a[i];res--;
}
else break;//
}
if(res==0){
ll m = 1e9 + 7;
for(i;i<cnt;++i){
if(m > a[i]) m = a[i];
}
printf("%lld\n",ans + m - 1);
}
}
}
M - Function and Function
#include<iostream>
#include<cstdio>
#define ll long long
using namespace std;
int c[10] = {1,0,0,0,1,0,1,0,2,1};
int g(int x,int k){
if(x==0){
return k&1;
}
else if(k==0) return x;
int t = 0;
while(x){
t+=c[x%10];
x/=10;
}
return g(t,k-1);
}
int main(){
int T;
cin>>T;
while(T--){
int x,k;
scanf("%d%d",&x,&k);
int ans = 0;ans = g(x,k);
printf("%d\n",ans);
}
}