contest 1.15

A.小迟的比赛

最优策略永远是努力应战,dp[i][j]表示前i轮赢了j局的概率,dp[i][j]=dp[i-1][j]*(1-p[i][j])+dp[i-1][j-1]*p[i-1][j-1]

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;

double p[1005][1005];
double dp[1005][1005];

int main()
{
    int n;scanf("%d",&n);
    for(int i=1;i<=n;i++){
        for(int j=0;j<=i-1;j++){
            scanf("%lf",&p[i][j]);
        }
    }
    dp[1][1]=p[1][0];
    dp[1][0]=1-p[1][0];
    for(int i=2;i<=n;i++){
        for(int j=0;j<=i;j++){
           if(j==0) dp[i][j]=dp[i-1][j]*(1-p[i][j]);
           else if(j==i) dp[i][j]=dp[i-1][j-1]*p[i][j-1];
           else dp[i][j]=dp[i-1][j]*(1-p[i][j])+dp[i-1][j-1]*p[i][j-1];
           //printf("%.2f
",dp[i][j]);
        }
    }
    double ans=0;
    for(int i=0;i<=n;i++){
        ans=ans+i*dp[n][i];
    }
    printf("%.2f
",ans);
    return 0;
}
View Code

B.You Like Cake

meet in middle 搜索 

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
typedef long long ll;
using namespace std;

ll n,v;
ll p[45];
ll all[2000000],cnt=0;
ll ans=0;

void dfs1(ll x,ll tot){
  if(tot>v) return;
  if(x>=n/2){
    all[++cnt]=tot;
    return;
  }
  dfs1(x+1,tot);
  dfs1(x+1,tot+p[x+1]);
}

void dfs2(ll x,ll tot){
  if(tot>v) return;
  if(x>=n){
    ll tmp=v-tot;
    ll p=upper_bound(all+1,all+1+cnt,tmp)-all;
    ans=max(ans,tot+all[p-1]);
    return;
  }
  dfs2(x+1,tot);
  dfs2(x+1,tot+p[x+1]);
}

int main()
{
    scanf("%lld%lld",&n,&v);
    for(ll i=1;i<=n;i++) scanf("%lld",&p[i]);
    dfs1(1,0);
    dfs1(1,p[1]);
    sort(all+1,all+1+cnt);
    //for(ll i=1;i<=cnt;i++) printf("%lld
",all[i]);
    dfs2(n/2+1,0);
    dfs2(n/2+1,p[n/2+1]);
    printf("%lld
",v-ans);
    return 0;
}
View Code

A.阿瓦的手套加强版

并查集

#include <iostream>
#include<cstdio>
#include<cstring>
typedef long long ll;
#define mod 998244353
using namespace std;
 
ll fa[100005];
 
ll findd(ll x){
  if(x==fa[x]) return x;
  else return fa[x]=findd(fa[x]);
}
 
ll qpow(ll a,ll b){
  ll ret=1;
  while(b){
    if(b&1) ret=ret*a%mod;
    a=a*a%mod;
    b>>=1;
  }
  return ret;
}
 
int main()
{
    ll n,m,T;scanf("%lld%lld%lld",&n,&m,&T);
    for(ll i=1;i<=n;i++) fa[i]=i;
    ll x,y;
    for(ll i=1;i<=T;i++){
        scanf("%lld%lld",&x,&y);
        ll fax=findd(x),fay=findd(y);
        if(fax==fay) continue;
        else fa[fax]=fay;
    }
    ll cnt=0;
    for(ll i=1;i<=n;i++){
        if(fa[i]==i) cnt++;
    }
    ll ans=qpow(m,cnt);
    printf("%lld
",ans);
    return 0;
}
View Code

B.阿卡吃百奇

规律

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    
    public static void main(String[] args) {
        Scanner cin=new Scanner(System.in);
        int t=cin.nextInt();
        for(int i=1;i<=t;i++){
            int r=cin.nextInt();
            if(r==1){
                System.out.println(1);
                continue;
            }
            BigInteger ans;
            int tmp=(r-1)%3;
            if(tmp==0){
                int b=(r-1)/3-1;
                ans=BigInteger.valueOf(1);
                BigInteger a=BigInteger.valueOf(3);
                while(b!=0){
                    if((b&1)!=0) ans=ans.multiply(a);
                    a=a.multiply(a);
                    b=b>>1;
                }
                ans=ans.multiply(BigInteger.valueOf(4));
            }
            else{
                int b=(r-1)/3;
                ans=BigInteger.valueOf(1);
                BigInteger a=BigInteger.valueOf(3);
                while(b!=0){
                    if((b&1)!=0) ans=ans.multiply(a);
                    a=a.multiply(a);
                    b=b>>1;
                }
                if(tmp==1) ans=ans.multiply(BigInteger.valueOf(2));
                else ans=ans.multiply(BigInteger.valueOf(3));
            }
            System.out.println(ans);
        }
    }
    
}
View Code

H.Jollo

枚举

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
 
int a[5],b[5],c[5];
int vis[55];
 
int main()
{
    int t;scanf("%d",&t);
    while(t--){
      memset(vis,0,sizeof(vis));
      scanf("%d%d%d%d%d",&a[1],&a[2],&a[3],&b[1],&b[2]);
      vis[a[1]]=vis[a[2]]=vis[a[3]]=vis[b[1]]=vis[b[2]]=1;
      int i;
      for(i=1;i<=52;i++){
          if(vis[i]) continue;
          c[1]=b[1],c[2]=b[2],c[3]=i;
          int cnt=0;
          sort(a+1,a+4);sort(c+1,c+4);
          for(int p1=3,p2=3;p1>=1&&p2>=1;){
            if(a[p1]>c[p2]){
                cnt++;
                p1--,p2--;
            }
            else p2--;
          }
          if(cnt>=2) continue;
          else break;
      }
      if(i<=52) printf("%d
",i);
      else printf("-1
");
    }
    return 0;
}
View Code

F.小Z的省选

 模拟

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
typedef long long ll;
using namespace std;

ll id;
char res[10];
char times[35];
char first_sub[100005][35];
char first_ac[100005][35];

ll days1[13]={366,31,29,31,30,31,30,31,31,30,31,30,31};
ll days2[13]={365,31,28,31,30,31,30,31,31,30,31,30,31};

bool run(ll y){
  if((y%4==0&&y%100!=0)||y%400==0) return 1;
  return 0;
}

struct Node{
  ll y,m,d;
  bool operator <(const Node& b)const{
    if(y!=b.y) return y<b.y;
    else if(m!=b.m) return m<b.m;
    else return d<b.d;
  }
};

Node get_times(char s[]){
  ll y1=0,m1=0,d1=0;
  ll flag=0;
  ll len=(ll)strlen(s);
  for(ll k=0;k<len;k++){
      if(s[k]=='/') {flag++;continue;}
      if(flag==0) y1=y1*10+(s[k]-'0');
      else if(flag==1) d1=d1*10+(s[k]-'0');
      else m1=m1*10+(s[k]-'0');
  }
  return Node{y1,m1,d1};
}

int main()
{
    ll n,m;scanf("%lld%lld",&n,&m);
    for(ll i=1;i<=n;i++){
        first_sub[i][0]='n';
        first_ac[i][0]='n';
    }
    for(ll i=1;i<=m;i++){
        scanf("%d",&id);
        scanf("%s%s",res,times);
        if(res[0]!='C'){
            if(first_sub[id][0]=='n'||get_times(times)<get_times(first_sub[id]))
              strcpy(first_sub[id],times);
        }
        if(res[0]=='A'){
            if(first_ac[id][0]=='n'||get_times(times)<get_times(first_ac[id]))
              strcpy(first_ac[id],times);
        }
    }
    ll ans=0;
    for(ll i=1;i<=n;i++){
        if(first_ac[i][0]=='n') continue;
        ll y1=get_times(first_sub[i]).y,m1=get_times(first_sub[i]).m,d1=get_times(first_sub[i]).d;
        ll y2=get_times(first_ac[i]).y,m2=get_times(first_ac[i]).m,d2=get_times(first_ac[i]).d;
        ll tot=0;
        if(y1==y2){
            if(m1==m2) tot=(d2-d1+1);
            else{
                ll tmp1;
                if(run(y1)) tmp1=days1[m1]-d1+1;
                else tmp1=days2[m1]-d1+1;
                ll tmp2=d2;
                ll tmp3=0;
                for(ll k=m1+1;k<=m2-1;k++){
                    if(run(y1)) tmp3+=days1[k];
                    else tmp3+=days2[k];
                }
                tot=tmp1+tmp2+tmp3;
            }
        }
        else{
            ll tmp1=0;
            for(ll k=1;k<=m1-1;k++){
                if(run(y1)) tmp1+=days1[k];
                else tmp1+=days2[k];
            }
            tmp1+=d1;
            if(run(y1)) tmp1=days1[0]-tmp1+1;
            else tmp1=days2[0]-tmp1+1;
            ll tmp2=0;
            for(ll k=1;k<=m2-1;k++){
                if(run(y2)) tmp2+=days1[k];
                else tmp2+=days2[k];
            }
            tmp2+=d2;
            ll tmp3=0;
            if(y2-1>=y1+1){
              ll num=(y2-1)/4-(y1)/4-((y2-1)/100-(y1)/100)+((y2-1)/400-(y1)/400);
              tmp3=(y2-1-(y1+1)+1)*365+num;
            }
            tot=tmp1+tmp2+tmp3;
        }
        ans+=tot;
    }
    printf("%lld
",ans);
    return 0;
}
View Code
转载请注明出处:https://www.cnblogs.com/lllxq/
原文地址:https://www.cnblogs.com/lllxq/p/10270825.html