BOJ 2014新生暑假个人排位赛05 整合

44 篇文章 3 订阅

A. 平方的平均值


仔细读题= =


#include <cstdio>
#define MAXN 100005
#define For(i,m,n) for(int i=(m);i<(n);i++)
#define LL long long
/*author birdstorm*/
using namespace std;
 
int main()
{
    int a, n;
    while(~scanf("%d",&n)){
        LL ans=0;
        int minans=MAXN;
        For(i,0,n){
            scanf("%d",&a); if(a<0) a=-a;
            if(a<minans) minans=a;
        }
        printf("%lld\n",(long long)minans*(long long)minans);
    }
    return 0;
}


B. 立方体


数学, 可以暴力判出所有点的对角位置, 如果发现有一个点的对角缺失, 即可输出结果.


比赛的姿势比较难看就不献丑了...



C. 字符串重排


贪心构造, 贪心正确性比赛时没有证出来, 所以使用了dfs搜索, 还是过了

对于一个字典序最小的字符串, 如果在构造时出现剩余长度为奇数且有一个字母剩余数量超过为(剩余长度+1)的一半时, 显然只能填这个字符

当所有字母的剩余数量都小于剩余长度的一半时都填写字典序最小且不和前一个字符重复的字符, 这样一定可以构造完毕

实际测试只要第一步能构造, 后面的都能构造


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <climits>
  
#define MAXN 100005
#define eps 1e-5
#define MOD 1000000009
  
#define test
  
#define For(i,m,n) for(int i=(m);i<(n);i++)
#define vecfor(iter,a) for(vector<int>::iterator iter=a.begin();iter!=a.end();iter++)
#define rep(i,m,n) for(int i=(m);i<=(n);i++)
#define LL long long
  
/*author birdstorm*/
using namespace std;
const double pi=acos(-1.0);

int vis[30];
int sta[MAXN];
char s[MAXN];
int len, top;
bool judge(int b, int len)
{
    if(len==0){
        sta[top++]=b;
        return true;
    }
    int tmp, cnt=0;
    for(int i=25; i>=0; i--){
        if(vis[i]&&i!=b){
            if(vis[i]==(len+1)/2&&len%2==1){
                tmp=i;
                cnt++;
            }
            else if(vis[i]>(len+1)/2&&len%2==1) return false;
        }
    }
    if(cnt==1){
        vis[tmp]--;
        if(judge(tmp,len-1)){
            sta[top++]=tmp;
            return true;
        }
        else{
            vis[tmp]++;
            return false;
        }
    }
    else if(cnt==0){
        int i;
        for(i=0;i<26;i++){
            if(vis[i]&&i!=b){
                vis[i]--;
                if(judge(i,len-1)){
                    sta[top++]=i;
                    return true;
                }
                else vis[i]++;
            }
        }
    }
    return false;
}
int main()
{
    int n, t;
    scanf("%d",&t);
    while(t--){
        scanf("%s",s);
        top=0;
        memset(vis,0,sizeof vis);
        len=strlen(s);
        For(i,0,len){
            vis[s[i]-'a']++;
        }
        bool out=false;
        For(i,0,26){
            if(len%2==0&&vis[i]*2>len||len%2==1&&vis[i]*2>len+1){
                printf("-1\n");
                out=true;
                break;
            }
        }
        if(out) continue;
        if(!judge(-1,len)) printf("-1");
        else for(int i=top-1; i>=1; i--){
            printf("%c",sta[i]+'a');
        }
        putchar('\n');
    }
    return 0;
}


D. Fibnacci

直接应用矩阵快速幂 

dp[n]=dp[n-1]+dp[n-2]+n;

使用4*4矩阵

{0,0,0,1,0,1,0,0,0,1,1,0,1,0,1,1}
作为base即可, 能保证每一个矩阵的[0][0]项都是dp[n]


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <climits>
 
#define MAXN 100005
#define eps 1e-5
#define MOD 1000000007
 
#define test
 
#define For(i,m,n) for(int i=(m);i<(n);i++)
#define vecfor(iter,a) for(vector<int>::iterator iter=a.begin();iter!=a.end();iter++)
#define rep(i,m,n) for(int i=(m);i<=(n);i++)
#define LL long long
 
/*author birdstorm*/
using namespace std;
const double pi=acos(-1.0);

LL s[4][4]={0,0,0,1,0,1,0,0,0,1,1,0,1,0,1,1};
class mat {
    public:
    LL m[4][4];
    mat(){}
    void init(LL s1[4][4]){
        for(int i = 0 ; i < 4 ; i++){
            for(int j = 0 ; j < 4 ; j++){
                m[i][j] = s1[i][j];
           }
       }
    }
    friend mat operator*(mat &m1 ,mat &m2) {
        int i, j, k;
        mat temp;
        for (i = 0; i < 4; i++) {
            for (j = 0; j < 4; j++) {
                temp.m[i][j] = 0;
                for(k = 0 ; k < 4 ; k++)
                   temp.m[i][j] += (m1.m[i][k] * m2.m[k][j])%MOD;
                temp.m[i][j] %= MOD;
           }
        }
        return temp;
    }
    friend mat power(mat &M , LL n){
        mat ans;
        for(int i = 0 ; i < 4 ; i++){
            for(int j = 0 ; j < 4 ; j++){
                if(i == j) ans.m[i][j] = 1;
                else ans.m[i][j] = 0;
            }
        }
        while(n){
            if(n & 1) ans = ans * M;
            n = n >> 1;
            M = M * M;
        }
       return ans;
    }
};
LL s2[4][4]={1,0,0,0,1,0,0,0,2,0,0,0,1,0,0,0};
int main()
{
    int t;
    LL a;
    mat a1;
    scanf("%d",&t);
    while(t--){
        scanf("%lld",&a);
        if(a==0){
            printf("1\n");
            continue;
        }
        if(a==1){
            printf("1\n");
            continue;
        }
        a1.init(s);
        mat a2;
        a2.init(s2);
        mat a3=power(a1,a);
        a3=a3*a2;
        printf("%lld\n",a3.m[0][0]);
    }
    return 0;
}


E. 机智的学姐


飞机带翅膀, 学姐实在是太机智了= =

巨大模拟题 依次判断


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <climits>
#define For(i,m,n) for(int i=(m);i<(n);i++)
#define YES printf("Yes\n")
#define NO printf("No\n")
#define mem(s) memset(s,0,sizeof s)

/*author birdstorm*/
using namespace std;
bool fd;
int maxa[5], maxb[5], comba[5][20], combb[5][20], twocnt, a, b, g;
char s[30], xj[30], idx[100];
inline void solve(int lena, int *cnta, int lenb, int *cntb)
{
    For(i,1,16) For(j,1,5){
        if(cnta[i]>=j) maxa[j]=i;
        if(cntb[i]>=j) maxb[j]=i;
    }
    if(cnta[15]&&cnta[14]) maxa[4]=20;
    if(cntb[15]&&cntb[14]) maxb[4]=20;
    if(maxa[4]>maxb[4]){ YES ; return;}
    if(maxa[4]<maxb[4]){ NO ; return;}
    if(maxa[1]>=maxb[1]){ YES ; return;}
    if(maxa[3]>=maxb[3]&&maxa[3]){ YES ; return;}
    if(maxa[2]>=maxb[2]&&maxa[2]){ YES ; return;}
    For(k,5,13){
        g=k; fd=true;
        while(k<=12&&cnta[k]&&cnta[k-1]&&cnta[k-2]&&cnta[k-3]&&cnta[k-4]) k++, fd=false;
        if(fd) continue;
        k--;
        for(int len=k-g+5;len>=5;len--) comba[1][len]=k;
    }
    For(k,5,13){
        g=k; fd=true;
        while(k<=12&&cntb[k]&&cntb[k-1]&&cntb[k-2]&&cntb[k-3]&&cntb[k-4]) k++, fd=false;
        if(fd) continue;
        k--;
        for(int len=k-g+5;len>=5;len--) combb[1][len]=k;
    }
    For(len,5,13) if(comba[1][len]&&comba[1][len]>=combb[1][len]){ YES ; return;}
    For(k,3,13){
        g=k; fd=true;
        while(k<=12&&cnta[k]>=2&&cnta[k-1]>=2&&cnta[k-2]>=2) k++, fd=false;
        if(fd) continue;
        k--;
        for(int len=k-g+3;len>=3;len--) comba[2][len]=k;
    }
    For(k,3,13){
        g=k; fd=true;
        while(k<=12&&cntb[k]>=2&&cntb[k-1]>=2&&cntb[k-2]>=2) k++, fd=false;
        if(fd) continue;
        k--;
        for(int len=k-g+3;len>=3;len--) combb[2][len]=k;
    }
    For(len,3,13) if(comba[2][len]&&comba[2][len]>=combb[2][len]){ YES ; return;}
    For(k,2,13){
        g=k; fd=true;
        while(k<=12&&cnta[k]>=3&&cnta[k-1]>=3) k++, fd=false;
        if(fd) continue;
        k--;
        for(int len=k-g+2;len>=2;len--) comba[3][len]=k;
    }
    For(k,2,13){
        g=k; fd=true;
        while(k<=12&&cntb[k]>=3&&cntb[k-1]>=3) k++, fd=false;
        if(fd) continue;
        k--;
        for(int len=k-g+2;len>=2;len--) combb[3][len]=k;
    }
    For(len,2,13) if(comba[3][len]&&comba[3][len]>=combb[3][len]){ YES ; return;}
    For(len,2,13){
        if(!comba[3][len]) continue;
        a=0, b=0;
        if(comba[3][len]&&lena>=4*len) a=comba[3][len];
        if(combb[3][len]&&lenb>=4*len) b=combb[3][len];
        if(a&&a>=b){ YES ; return;}
    }
    For(len,2,13){
        if(!comba[3][len]) continue;
        a=0, b=0; twocnt=0;
        For(j,1,13) if(j!=comba[3][len]&&j!=comba[3][len]-1&&cnta[j]>=2)
            twocnt++;
        if(twocnt>=len) a=comba[3][len];
        if(!a) continue;
        twocnt=0;
        For(j,1,13) if(j!=combb[3][len]&&j!=combb[3][len]-1&&cntb[j]>=2)
            twocnt++;
        if(twocnt>=len) b=combb[3][len];
        if(a>=b){ YES ; return;}
    }
    NO ;
}
int cntx[30], cnts[30];
int main()
{
    freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    idx['Y']=15; idx['X']=14; idx['2']=13; idx['A']=12;
    idx['K']=11; idx['Q']=10; idx['J']=9; idx['T']=8;
    For(i,0,7) idx[i+'3']=i+1;
    while(~scanf("%s%s",xj,s)){
        mem(cntx); mem(cnts); mem(maxa); mem(maxb); mem(comba); mem(combb);
        int lenxj=strlen(xj), lens=strlen(s);
        For(i,0,lenxj) cntx[idx[xj[i]]]++;
        For(i,0,lens) cnts[idx[s[i]]]++;
        solve(lenxj,cntx,lens,cnts);
    }
    return 0;
}



题目数据比较弱, 有些东西不判可能可以水过去

欢迎来hack~


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值