CUGBACM Codeforces Tranning 2 題解,cugbacmcodeforces

來源:互聯網
上載者:User

CUGBACM Codeforces Tranning 2 題解,cugbacmcodeforces

連結:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=62027#overview

描述:現場過了四道,最後一題類比退火。

題解:

A.Chat Server's Outgoing Traffic

題意:一個聊天室,每次操作有三種選擇,第一種是有人進入,第二種有人出去,第三種有人發言,每次發言產生一定影響,影響為人數*字數,問總價值多少。

思路:水,類比即可。

代碼:

#include <algorithm>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <ctime>#include <ctype.h>#include <iostream>#include <map>#include <queue>#include <set>#include <stack>#include <string>#include <vector>#define eps 1e-8#define INF 0x7fffffff#define maxn 10005#define PI acos(-1.0)#define seed 31//131,1313//#define LOCALtypedef long long LL;typedef unsigned long long ULL;using namespace std;int main(){    #ifdef LOCAL    freopen("in.txt", "r", stdin);    //freopen("out.txt", "w", stdout);    #endif    int ans = 0,recent = 0;    char ss[105];    while(gets(ss))    {        if(ss[0]=='+')            recent ++;        else if(ss[0]=='-')            recent --;        else        {            int len=strlen(ss);            int pos = -1;            for(int i=0;i<len;i++)            {                if(ss[i]==':')                    pos = i;            }            ans += recent * (len-pos-1);        }    }    printf("%d\n",ans);    return 0;}
B.Center Alignment

題意:給N行字串,要求用婊起來(233

思路:還是類比,如果某行字串左右空格數不一致,先左邊少一個。對了,第一發沒把LOCAL注釋掉,很,b的WA的了1發

代碼:

#include <algorithm>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <ctime>#include <ctype.h>#include <iostream>#include <map>#include <queue>#include <set>#include <stack>#include <string>#include <vector>#define eps 1e-8#define INF 0x7fffffff#define maxn 10005#define PI acos(-1.0)#define seed 31//131,1313//#define LOCALtypedef long long LL;typedef unsigned long long ULL;using namespace std;int main(){    #ifdef LOCAL    freopen("in.txt", "r", stdin);    //freopen("out.txt", "w", stdout);    #endif    int top = 0;    char ss[1005][1005];    int len[1005];    int M_len = 0;    while(gets(ss[top++]))    {        len[top-1]=strlen(ss[top-1]);        if(len[top-1]>M_len)            M_len=len[top-1];    }    top--;    for(int i=0;i<M_len+2;i++)        putchar('*');    puts("");    bool flag = 0;    for(int i=0;i<top;i++)    {        int sub1,sub2;        putchar('*');        if((M_len-len[i])%2==0)        {            sub1=(M_len-len[i])/2;            sub2=(M_len-len[i])/2;        }        else        {            if(flag == 0)            {                sub1 = (M_len-len[i])/2;                sub2 = M_len-len[i]-sub1;                flag = 1;            }            else            {                sub2 = (M_len-len[i])/2;                sub1 = M_len-len[i]-sub2;                flag = 0;            }        }        for(int j=0;j<sub1;j++)            putchar(' ');        printf("%s",ss[i]);        for(int j=0;j<sub2;j++)            putchar(' ');        putchar('*');        puts("");    }        for(int i=0;i<M_len+2;i++)        putchar('*');    return 0;}
C.Mysterious Present

題意:有若干個信封和一個卡片,卡片或信封能放入另一個信封裡的條件是卡片或信封的長寬嚴格小於另一個信封的長寬。問最多可以用到多少信封。

思路:最長上升子序列,先按長排序,然後從大到小對於每個信封找到放入哪個比他大的信封能使他的外套最多,並且記錄路徑。

代碼:

#include <algorithm>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <ctime>#include <ctype.h>#include <iostream>#include <map>#include <queue>#include <set>#include <stack>#include <string>#include <vector>#define eps 1e-8#define INF 0x7fffffff#define maxn 10005#define PI acos(-1.0)#define seed 31//131,1313//#define LOCALtypedef long long LL;typedef unsigned long long ULL;using namespace std;struct Bag{    int x,y,num;} bag[5005];bool cmp(Bag a,Bag b){    return a.x<b.x;}int main(){#ifdef LOCAL    freopen("in.txt", "r", stdin);    //freopen("out.txt", "w", stdout);#endif    int tot,x,y;    int from[5005],t[5005];    scanf("%d%d%d",&tot,&x,&y);    for(int i=0; i<tot; i++)    {        scanf("%d%d",&bag[i].x,&bag[i].y);        bag[i].num = i;    }    sort(bag,bag+tot,cmp);//    for(int i=0;i<tot;i++)//        cout<<bag[i].x<<" "<<bag[i].y<<" "<<bag[i].num<<endl;    for(int i=tot-1; i>=0; i--)    {        t[i]=1;        from[i]=i;        for(int j=i+1; j<tot; j++)        {            if(bag[i].x<bag[j].x&&bag[i].y<bag[j].y&&t[j]+1>t[i])            {                t[i]=t[j]+1;                from[i]=j;            }        }    }    int head = -1,m = 0;    for(int i=0; i<tot; i++)    {        if(bag[i].x>x&&bag[i].y>y&&t[i]>m)        {            m=t[i];            head=i;        }    }    printf("%d\n",m);    if(m!=0)    {        while(from[head]!=head)        {            printf("%d ",bag[head].num+1);            head=from[head];        }        printf("%d",bag[head].num+1);        printf("\n");    }    return 0;}


D.Lorry

題意:給出兩種若干條救生船,第一種救生船體積是一單位,第二種是兩單位,每艘船有各自能容納的人數,現給定體積,問最多能容納多少人。

思路:貪心,兩種情況,一種是第一種救生船隻剩一艘,那麼只要第二種能容納的比該艘多,那麼就不斷的選第二種,否則選第一種。另一種是如果第一種容納最多的船和容納次多的船的容納人數之和大於第二種容納最多的,選第一種,否則選第二種。

代碼:

#include <algorithm>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <ctime>#include <ctype.h>#include <iostream>#include <map>#include <queue>#include <set>#include <stack>#include <string>#include <vector>#define eps 1e-8#define INF 0x7fffffff#define maxn 10005#define PI acos(-1.0)#define seed 31//131,1313//#define LOCALtypedef long long LL;typedef unsigned long long ULL;using namespace std;struct Bag{    int ca;    int num;} ta[100005],tb[100005];bool cmp(Bag a,Bag b){    return a.ca>b.ca;}int main(){#ifdef LOCAL    freopen("in.txt", "r", stdin);    //freopen("out.txt", "w", stdout);#endif    int a,v;    scanf("%d%d",&a,&v);    int topa = 0 , topb = 0;    for(int i=1; i<=a; i++)    {        int x,y;        scanf("%d%d",&x,&y);        if(x==2)        {            ta[topa].num=i;            ta[topa++].ca=y;        }        else        {            tb[topb].num=i;            tb[topb++].ca=y;        }    }    sort(ta,ta+topa,cmp);    sort(tb,tb+topb,cmp);    int posa = 0 , posb = 0 ;    int now = 0;    int ans = 0;    if(topa*2+topb<=v)    {        for(int i=0; i<topa; i++)            ans+=ta[i].ca;        for(int i=0; i<topb; i++)            ans+=tb[i].ca;        printf("%d\n",ans);        bool flag3 = 0;        for(int i=0;i<topa;i++)            {                if(flag3 == 0)                {                    printf("%d",ta[i].num);                    flag3 = 1;                }                else printf(" %d",ta[i].num);            }            for(int i=0;i<topb;i++)            {                if(flag3 == 0)                {                    printf("%d",tb[i].num);                    flag3 = 1;                }                else printf(" %d",tb[i].num);            }            printf("\n");    }    else    {        while((topa>posa&&topb>posb)&&(v-now>=2))        {            if(posb==topb-1)            {                while(v-now>=2&&ta[posa].ca>tb[posb].ca&&posa<topa)                {                    ans+=ta[posa].ca;                    posa++;                    now+=2;                }                if(v-now>=1)                {                    ans+=tb[posb].ca;                    posb++;                    now++;                }            }            else            {                if(tb[posb].ca+tb[posb+1].ca>=ta[posa].ca)                {                    ans+=tb[posb].ca;                    posb++;                    now++;                }                else                {                    ans+=ta[posa].ca;                    posa++;                    now+=2;                }            }        }        while(topa>posa&&v-now>=2)        {            ans+=ta[posa].ca;            posa++;            now+=2;        }        while(topb>posb&&v-now>=1)        {            ans+=tb[posb].ca;            posb++;            now++;        }        if(v-now==1)        {            if(posb<topb)            {                now++;                ans+=tb[posb].ca;                posb++;            }        }        //cout<<posa<<" "<<posb<<endl;        printf("%d\n",ans);        bool flag = 0;        for(int i=0;i<posa;i++)        {            //cout<<i<<"->";            if(flag == 0)            {                printf("%d",ta[i].num);                flag = 1;            }            else printf(" %d",ta[i].num);        }        for(int i=0;i<posb;i++)        {            //cout<<i<<"->";            if(flag == 0)            {                printf("%d",tb[i].num);                flag = 1;            }            else printf(" %d",tb[i].num);        }        printf("\n");    }    return 0;}
E.Commentator problem

題意:給出三個圓,要找一個點,要求這個點對於三個圓的視角大小是相同的。

思路:類比退火,現學的。很好理解,現在三個三角形“外接矩形”裡隨便找若干個點,dist定為矩形的長,然後不斷縮小dist,同時每次在每個點周圍再次找若干個點,同時找到一個參考值,用來表示這個點符合要求的程度,如果參考值比當前點參考值小,就用這個店取代當前點。參考了芳哥的思路,

但是有小問題,導致“隨機演算法小機率得到正確答案”,就改了一下參考值,改為:Σdis(aa,edge[i])/edge[i].r-dis(aa,edge[(i+1)%3])/edge[(i+1)%3].r),減少誤差,並且改了最後的判斷條件。

代碼:

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<map>#include<queue>#include<stack>#include<vector>#include<ctype.h>#include<algorithm>#include<string>#include <ctime>#define PI acos(-1.0)#define maxn 10005#define INF 0x7fffffff#define eps 1e-6typedef long long LL;using namespace std;struct point{    double x,y,r;    point(){}    point(double _x,double _y):x(_x),y(_y) {}    double val;};double dis(point a,point b){    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}point a[25];point edge[5];double get_val(point aa){    double s[5],sum = 0 ;    for(int i=0; i<3; i++)    {        s[i]=(dis(aa,edge[i])/edge[i].r-dis(aa,edge[(i+1)%3])/edge[(i+1)%3].r);//判斷符合值        s[i]=s[i]*s[i];        sum+=s[i];    }    return sum ;}point rand_point(point aa,point bb)//隨機取點{    double x = aa.x+(bb.x-aa.x)*((double)(rand()%10001)/10000.0);    double y = aa.y+(bb.y-aa.y)*((double)(rand()%10001)/10000.0);    point tmp=point(x,y);    tmp.val=get_val(tmp);    return tmp;}int main(){    srand(time(0));    point ld,ru;    ld=point(INF,INF);    ru=point(-INF,-INF);    for(int i=0; i<3; i++)    {        scanf("%lf%lf%lf",&edge[i].x,&edge[i].y,&edge[i].r);        ld.x=min(ld.x,edge[i].x);        ld.y=min(ld.y,edge[i].y);        ru.x=max(ru.x,edge[i].x);        ru.y=max(ru.y,edge[i].y);    }    double dist = max(ru.x-ld.x,ru.y-ld.y);    for(int i=0; i<20; i++)        a[i]=rand_point(ld,ru);    while(dist > 0.0001)//不斷縮小每個點的周圍點    {        for(int i=0; i<20; i++)        {            for(int j=0; j<20; j++)            {                point tmp;                point L,R;                L=point(max(ld.x,a[i].x-dist),max(ld.y,a[i].y-dist));                R=point(min(ru.x,a[i].x+dist),min(ru.y,a[i].y+dist));                tmp=rand_point(L,R);                if(tmp.val<a[i].val)                    a[i]=tmp;            }        }        dist *= 0.9;    }    point ans;    double ss=0;    ans.val = INF;    for(int i=0; i<20; i++)    {        double rec=edge[0].val/(dis(edge[0],a[i]));        if(a[i].val<=ans.val&&rec>=ss||(a[i].val<eps&&rec>=ss))        {            ans=a[i];            ss=rec;        }    }    if(ans.val<eps)        printf("%.5lf %.5lf\n",ans.x,ans.y);    return 0;}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.