2020-07-03 牛客編程挑戰

昨天多校訓練在牛客組隊,看到有個一戰到底編程挑戰,有些好奇就進去看看,做出來倆,都是模擬,第三個是動態規劃,看了看沒思路就走了,4號早晨起來補了一下。

(有幾個latex公式我暫時不太會搞,寫出來可能看着有點蠢,見諒)

1.簡單題(重要極限+快速冪)

 

 解:題目中的O就是常見的重要極限$lim_{x\to \infty}(1+\frac{1}{x})^x = e$,e的值是從math.h直接拿過來的,精度挺可以的,拿來直接快速冪就好了,後面保留小數那裏我寫的有點蠢,用cout的格式化輸出會更好一些。

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
double Pow(double x,int y){
    double res=1;
    while(y){
        if(y&1)res=res*x;
        x=x*x;
        y>>=1;
    }
    return res;
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        double ans=(double)y*Pow(M_E,x);
        if(z==1)printf("%.1f\n",ans);
        if(z==2)printf("%.2f\n",ans);
        if(z==3)printf("%.3f\n",ans);
        if(z==4)printf("%.4f\n",ans);
        if(z==5)printf("%.5f\n",ans);
    }
    return 0;
}

2.消息列表(模擬)

 

 解:這個題也沒什麼難度,我思路就是拿一個結構體存儲每個人的信息(這個人的編號,消息總條數,是否置頂,最後一次發消息的時間),這樣再用一個map,把所有人標記到,避免重複加入結構體。這樣的話,最後只要將結構體排序就可以了,但是被卡住了空間,只能用vector,或者用牛客的自測調試多試幾次,卡着空間限制應該也能過,不然的話vector也沒法過是吧。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<vector>
#include<algorithm>
#define maxn 1000001
#define INF 1000000
using namespace std;
struct node{
    string s;
    int last;
    int tot;
};
vector<node>q;
map<string,int>p;
int T,n,cnt;
char s1[10];
string s2;
bool cmp(node u,node v){
    if(u.last!=v.last)return u.last>v.last;
}
int main(){
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        cnt=0;
        vector<node>().swap(q);
        p.clear();
        for(int i=1;i<=n;i++){
            scanf("%s",s1);
            cin>>s2;
            if(!p[s2]){
                node a;
                cnt++;
                p[s2]=cnt;
                a.s=s2;
                a.last=0;
                a.tot=0;
                q.push_back(a);
            }
            int now=p[s2]-1;
            if(s1[0]=='r'){
                if(q[now].last>=INF)q[now].last=i+INF;
                else q[now].last=i;
                q[now].tot++;
            }
            else if(s1[0]=='v'){
                q[now].tot=0;
            }
            else if(s1[0]=='u'){
                if(q[now].last<INF)q[now].last+=INF;
            }
            else if(s1[1]=='o'){
                if(q[now].last>=INF)q[now].last-=INF;
            }
            else if(s1[1]=='e'){
                q[now].tot=0;
                q[now].last=0;
            }
        }
        sort(q.begin(),q.end(),cmp);
//        cout<<cnt<<endl;
        for(int i=0;i<cnt;i++){
            if(q[i].last==0)break;
            cout<<q[i].s<<" ";
            printf("%d\n",q[i].tot);
        }
        puts("");
    }
    return 0;
}

3.隕石的祕密(動態規劃)

 

 解:這個題當時是一點思路也沒有,現在看了題解覺得有點意思了,但是還不太很懂,想找個人給我講講,先把代碼貼上來吧

#include<iostream>
#include<cstdio>
#define mod 11380
using namespace std;
int n,l1,l2,l3,D,f[11][11][11][31];
int main(){
    scanf("%d%d%d%d",&l1,&l2,&l3,&D);
    f[0][0][0][0]=1;
    for(int i=0;i<=l1;i++){
        for(int j=0;j<=l2;j++){
            for(int k=0;k<=l3;k++){
                for(int l=1;l<=D;l++){
                    if(i!=0||j!=0||k!=0){
                        int tmp=0;
                        for(int a=0;a<k;a++){
                            tmp=(tmp+f[i][j][k-a-1][l]*f[0][0][a][l-1])%mod;
                        }
                        for(int b=0;b<j;b++){
                            for(int a=0;a<=k;a++)
                            tmp=(tmp+f[i][j-b-1][k-a][l]*f[0][b][a][l-1])%mod;
                        }
                        for(int c=0;c<i;c++){
                            for(int b=0;b<=j;b++){
                                for(int a=0;a<=k;a++){
                                    tmp=(tmp+f[i-c-1][j-b][k-a][l]*f[c][b][a][l-1])%mod;
                                }
                            }
                        }
                        f[i][j][k][l]=tmp;
                    }
                    else f[i][j][k][l]=1;
                }
            }
        }
    }
    int ans=((f[l1][l2][l3][D]-f[l1][l2][l3][D-1])%mod+mod)%mod;
    printf("%d\n",ans);
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章