Facebook hacker cup qualification round 题解

来源:互联网 发布:js this获取name 编辑:程序博客网 时间:2024/04/30 04:31

除了第三题有点难度之外,整体还是很简单的,满分水过。。。


A 找到第一个#,计算边长,验证一下就行了。其实,20组数据,6分钟内提交,人眼去看也差不多了。。。

int main(){    int t, n;    string str[50];    cin>>t;    for(int test=1; test<=t; test++){        bool result = true;        pair<int, int> pos;        int len = 0;        cin>>n;        for(int i=0; i<n; i++) cin>>str[i];        int row = n;        int col = str[0].size();        bool fir = false;        for(int i=0; i<row; i++){            if(fir==true) break;            for(int j=0; j<col; j++){                if(fir == true) break;                if(str[i][j]=='#'){                    pos = make_pair(i, j);                    fir = true;                    break;                }            }        }        int end = pos.second;        for(end=pos.second; end<col; end++){            if(str[pos.first][end]!='#'){                break;            }        }        len = end-pos.second;        //cout<<"len: "<<len<<endl;        int x = pos.first; int y = pos.second;        for(int i=0; i<row; i++){            for(int j=0; j<col; j++){                if((i-x)<len && (j-y)<len && i>=x && j>=y                    && str[i][j]!='#')                     result=false;                if((i-x)>=len || (j-y)>=len || i<x || j<y){                    if(str[i][j] == '#')                        result = false;                }                if(result == false) break;            }        }        if(result == true)            cout<<"Case #"<<test<<": YES"<<endl;        else cout<<"Case #"<<test<<": NO"<<endl;    }return 0;}

B 简单模拟一下就行了,由于对效率要求不高,怎么方便怎么来

int main(){    int t, n, m, p;    // shot, height, name    vector<pair<int, pair<int, string> > > v;    cin>>t;    for(int tt=1; tt<=t; tt++){        cin>>n>>m>>p;        v.clear();        int shot, h;        string name;        for(int i=0; i<n; i++){            cin>>name>>shot>>h;            v.push_back(make_pair(shot, make_pair(h, name)));        }        sort(v.begin(), v.end());        reverse(v.begin(), v.end());        // time, number        vector<pair<int, int> > team[2];        team[0].clear(); team[1].clear();        for(int i=0; i<n; i++){            if(i%2 == 0) team[0].push_back(make_pair(0, i));            else team[1].push_back(make_pair(0, i));        }           vector<string> res;        res.clear();        for(int k=0; k<2; k++){            vector<pair<int, int> > cur, rest;            cur.clear(); rest.clear();            for(int i=0; i<p; i++)                cur.push_back(team[k][i]);            for(int i=p; i<team[k].size(); i++)                            rest.push_back(team[k][i]);            for(int i=1; i<=m; i++){                // there is no rest player                if(rest.size() == 0) break;                pair<int, int> outNum, inNum;                for(int j=0; j<cur.size(); j++)                    cur[j].first++;                sort(cur.begin(), cur.end(), greater<pair<int, int> >());                sort(rest.begin(), rest.end());                outNum = cur[0]; inNum = rest[0];                cur.erase(cur.begin()); rest.erase(rest.begin());                cur.push_back(inNum);                rest.push_back(outNum);            }            for(int i=0; i<cur.size(); i++){                int index = cur[i].second;                res.push_back(v[index].second.second);            }        }// end outer for loop        sort(res.begin(), res.end());        cout<<"Case #"<<tt<<":";        for(int i=0; i<res.size(); i++)            cout<<" "<<res[i];        cout<<endl;    }return 0;}

C 动态规划,关键是注意到给定的概率值只到三位小数,所以状态是有限的。。。

import java.text.DecimalFormat;import java.util.Scanner;public class facebook {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubScanner cin = new Scanner(System.in);int t = cin.nextInt();double[][][][] dp = new double[205][110][1005][2];for(int tt=1; tt<=t; tt++){//initializationint K = cin.nextInt();for(int i=0; i<2*K+1; i++)for(int j=0; j<K+5; j++)for(int k=0; k<dp[i][j].length; k++)for(int kk=0; kk<dp[i][j][k].length; kk++)dp[i][j][k][kk] = 0;double ps = cin.nextDouble();double pr = cin.nextDouble();double oripi = cin.nextDouble();int pimul = (int) Math.round(oripi*1000);double pu = cin.nextDouble();int pumul = (int) Math.round(pu*1000);double pw = cin.nextDouble();double pd = cin.nextDouble();int pdmul = (int) Math.round(pd*1000);double pI = cin.nextDouble();//int pImul = (int) Math.round(pI*1000);dp[1][1][pimul][1] = oripi*ps+(1.0-oripi)*pr;dp[1][0][pimul][0] = oripi*(1.0-ps)+(1.0-oripi)*(1.0-pr);for(int i=2; i<=2*K-1; i++){int upper = Math.min(K, i);for(int j=0; j<=upper; j++){if((i-1-j) >= K) continue;for(int pp=0; pp<=1000; pp++){if(j > 0)dp[i][j][pp][1] = dp[i-1][j-1][pp][1]*(1.0-pw)+dp[i-1][j-1][pp][0]*(1.0-pI);dp[i][j][pp][0] = dp[i-1][j][pp][1]*(1.0-pw)+dp[i-1][j][pp][0]*(1.0-pI);if(pp==0){for(int tmp=0; tmp<=pdmul; tmp++){dp[i][j][pp][0] += dp[i-1][j][pp+tmp][0]*pI;if(j > 0)dp[i][j][pp][1] += dp[i-1][j-1][pp+tmp][0]*pI;}}if(pp==1000){for(int tmp=0; tmp<=pumul; tmp++){dp[i][j][pp][0] += dp[i-1][j][pp-tmp][1]*pw;if(j > 0)dp[i][j][pp][1] += dp[i-1][j-1][pp-tmp][1]*pw;}}if(pp-pumul>=0 && pp!=0 && pp!=1000){if(j > 0)dp[i][j][pp][1] += dp[i-1][j-1][pp-pumul][1]*pw;dp[i][j][pp][0] += dp[i-1][j][pp-pumul][1]*pw;}if(pp+pdmul<=1000 && pp!=0 && pp!=1000){if(j > 0)dp[i][j][pp][1] += dp[i-1][j-1][pp+pdmul][0]*pI;dp[i][j][pp][0] += dp[i-1][j][pp+pdmul][0]*pI;}double oopi = (double)pp/1000.0;dp[i][j][pp][1] *= oopi*ps+(1.0-oopi)*pr;dp[i][j][pp][0] *= oopi*(1.0-ps)+(1.0-oopi)*(1.0-pr);}// end pp loop}// end j loop}// end i loopdouble res = 0;for(int i=K; i<=2*K-1; i++){for(int pp=0; pp<=1000; pp++){res += dp[i][K][pp][1];}}DecimalFormat df = new DecimalFormat("0.000000");System.out.println("Case #"+tt+": "+df.format(res));}}// end main method}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 新员工老员工出现矛盾怎么办 手机移动4g网慢怎么办 碰到素质低的老公怎么办 小孩做事拖拉 没时间观念怎么办 execl表中日期加3.5怎么办 狗狗喜欢咬人怎么办 烧烤一顿吃多了怎么办 派派背包食物不足怎么办 未转变者下不了怎么办 小升初户籍与房产不一致怎么办 小孩在外地读书怎么办计生证明 泉州居住证要半年小孩读书怎么办 孩子上学有兰山户口没有房产怎么办 培训机构跑路了怎么办 报的培训班跑路怎么办 巡视组举报后会怎么办 巡视组交办不办怎么办 分手以后还要不要联系忘不了怎么办 父母穷且不上进怎么办 惹父母生气了该怎么办 小孩戒奶不吃奶粉怎么办 孩子听不进去话怎么办 异性好朋友喜欢自己亲吻自己怎么办 对方对你反感了怎么办 家长偷看孩子日记老师怎么办 儿子与父母相冲怎么办 初二孩子不争气老师打他怎么办? 孩子被老师打又怎么办 家里2个孩子打架怎么办 一年级的学生特别会顶嘴怎么办 私立学校的学生顶嘴老师该怎么办 孩子做错事家长不道歉怎么办 孩孑语文成绩差怎么办 高三了孩子不愿意补课怎么办 四年级的孩子上课喜欢讲小话怎么办 五年级孩子太叛逆怎么办 二年级话唠孩子怎么办 家有老人带孩子怎么办 不会看孩子。孩子一哭就害怕怎么办 孩子不老实爱动怎么办 4周孩子脾气大怎么办