码迷,mamicode.com
首页 > 其他好文 > 详细

codeforces Gym 100971 A、B、C、F、G、K、L

时间:2018-01-22 22:55:04      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:view   scanf   eee   比较   bsp   color   图片   style   ide   

A题  直接把问号全部变为陆地如果所有陆地连通    那么再逐个把刚才变成陆地的问号变为水如果依旧连通有多种解

为什么我的代码跑不过去,和网上的题解思路一模一样!!??

技术分享图片
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#define MAX_N 55
using namespace std;
char map[MAX_N][MAX_N];
int n,m;
int cnt_s;
int cnt_p;
int cnt;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
bool vis[MAX_N][MAX_N];
void dfs(int sx,int sy)
{
    vis[sx][sy]=true;
    for(int i=0;i<4;i++)
    {
        int tx=sx+dx[i];
        int ty=sy+dy[i];
        if(tx>=0&&tx<n&&ty>=0&&ty<m&&map[tx][ty]!=#&&!vis[tx][ty])
        {
            cnt++;
            if(map[tx][ty]==?)
            map[tx][ty]=*;//标记走过的?
            vis[tx][ty]=true;
            dfs(tx,ty);
        }
    }
    return ;
}
int main(void)
{
    while(scanf("%d%d",&n,&m)==2)
    {
        cnt_p=0;
        int sx,sy;
        for(int i=0;i<n;i++)
        scanf("%s",map[i]);
        memset(vis,false,sizeof(vis));
        int k=0;//k块陆地
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(map[i][j]==.&&!vis[i][j])
                {
                    k++;
                    cnt=1;
                    dfs(i,j);
                    sx=i;
                    sy=j;
                }
            }
        }
        if(k>1)
        {
            printf("Impossible\n");
            continue;
        }
        cnt_s=cnt;
        bool ok=false;
        for(int i=0;i<n&&!ok;i++)
        {
            for(int j=0;j<m&&!ok;j++)
            {
                cnt=1;
                if(map[i][j]==*)
                {
                    memset(vis,false,sizeof(vis));
                    map[i][j]=#;
                    dfs(sx,sy);
                    if(cnt==cnt_s-1)
                    ok=true;
                    map[i][j]=.;
                }
            }
        }
        if(ok)
        printf("Ambiguous\n");
        else
        {
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++)
                {
                    if(map[i][j]==.)
                    printf(".");
                    else
                    printf("#");
                }
                printf("\n");
            }
        }
    }
    return 0;
}
View Code

B题  判断有几个点在原位置上  偶数个直接两两互换,奇数个前三个互换 后面两两互换,特判只有一个的时候

技术分享图片
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <vector>
using namespace std;
const int maxn= 200005;
const int maxm= 1e4+10;
const int inf = 0x3f3f3f3f;
typedef long long ll;
int a[maxn],b[maxn];
int n;
int main()
{
    while(cin>>n)
    {
        int cnt=0;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
            if(a[i]==i)
                b[cnt++]=a[i];
        }
        if(cnt==0)
            cout<<0<<endl;
        else if(cnt==1)
        {
            cout<<cnt<<endl;
            if(b[0]-1==0)
                cout<<b[0]<<" "<<b[0]+1<<endl;
            else
                cout<<b[0]<<" "<<b[0]-1<<endl;
        }
        else
        if(cnt%2==0)
        {
            cout<<cnt/2<<endl;
            for(int i=1;i<cnt;i=i+2)
                cout<<b[i]<<" "<<b[i-1]<<endl;
        }
        else
        {
            cout<<(cnt-3)/2+2<<endl;
            for(int i=1;i<=2;i++)
                cout<<b[i]<<" "<<b[0]<<endl;
            for(int i=3;i<cnt;i=i+2)
            {
                cout<<b[i]<<" "<<b[i+1]<<endl;
            }
        }
    }
}
View Code

C题 大于最大差小于最小和

技术分享图片
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <vector>
using namespace std;
const int maxn= 200005;
const int maxm= 1e4+10;
const int inf = 0x3f3f3f3f;
typedef long long ll;
int a[maxn],b[maxn];
int n;
int main()
{
    while(cin>>n)
    {
        for(int i=1;i<=n;i++)
            cin>>a[i];
        sort(a+1,a+n+1);
        int x=a[1]+a[2];
        int y=a[n]-a[1];
        if(x-y>=2)
            cout<<"YES"<<endl<<y+1<<endl;
        else
            cout<<"NO"<<endl;
    }
}
View Code

F题 直接列出方程来  解二元一次方程  精度问题真的头疼。。为什么我的代码就不对 贴一个大佬二分写的

技术分享图片
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <vector>
using namespace std;
const int maxn = 2e5+10;
const int maxm = 1e4+10;
const int inf = 0x3f3f3f3f;
const double epx = 1e-10;
typedef long long ll;
int n;
int xx1,yy1,xx2,yy2;
int vxx1,vyy1,vxx2,vyy2;
double solve(double t)
{
    int A=xx1-xx2;
    int A1=vxx1-vxx2;
    int B=yy1-yy2;
    int B1=vyy1-vyy2;
    return (A+A1*t)*(A+A1*t)+(B+B1*t)*(B+B1*t);
}
int main()
{
    while(scanf("%d %d %d %d",&xx1,&yy1,&xx2,&yy2)!=EOF)
    {
        scanf("%d %d %d %d",&vxx1,&vyy1,&vxx2,&vyy2);
        double ans=0;
        double l=0,r=1e6;
        while(r-l>epx)
        {
            double mid=(l+r)/2;
            double midmid=(mid+r)/2;
            if(solve(mid)<=solve(midmid))
                r=midmid;
            else
                l=mid;
        }
        ans=sqrt(solve((l+r)/2));
        printf("%.10f\n",ans);
    }
}
View Code

G题 八种情况直接暴力模拟

技术分享图片
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <vector>
using namespace std;
const int maxn= 200005;
const int maxm= 1e4+10;
const int inf = 0x3f3f3f3f;
typedef long long ll;
int a[maxn],b[maxn];
int n,m;
int main()
{
    while(cin>>n>>m)
    {
       int x,y;
       int x1,y1;
       cin>>x>>y>>x1>>y1;
       int flag=0;
       if((x>n&&x>m)||(y>n&&y>m)||(x1>n&&x1>m)||(y1>n&&y1>m))
            flag=0;
       else if((x+x1<=n&&max(y,y1)<=m)||(x+y1<=n&&max(x1,y)<=m)||(y+x1<=n&&max(x,y1)<=m)||(y+y1<=n&&max(x,x1)<=m)||(x+x1<=m&&max(y,y1)<=n)||(x+y1<=m&&max(x1,y)<=n)||(y+x1<=m&&max(x,y1)<=n)||(y+y1<=m&&max(x,x1)<=n))
            flag=1;
        if(flag)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
}
View Code

K题 回文串 两种情况两边开始比较第一次遇到不同的  删右边或者删左边  删完之后若还有不同行不通   一边成立就YES 两边都不成立NO

我想的有点多写麻烦了 贴的队友的代码 

技术分享图片
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=1e6+10;
char a[N];
int main()
{
    while(~scanf("%s",a))
    {
        int len=strlen(a);
        int flag1=0,flag2=0;
        int num=0,num1=0,num2=0;
        for(int i=0; i<len/2; i++)
        {
            if(a[i]==a[len-1-i])num++;
            else
            {
                flag1=i;
                flag2=len-1-i;
                break;
            }
        }
        //cout<<flag1<<" "<<flag2<<endl;
        for(int i=flag1+1; i<=len/2; i++)
        {
            if(a[i]!=a[len-1-i+1])
            {
                num1++;
                break;
            }
        }
        for(int i=flag1; i<len/2; i++)
        {
            if(a[i]!=a[len-1-i-1])
            {
                num2++;
                break;
            }
        }
        //cout<<num<<" "<<num1<<" "<<num2<<endl;
        if(num==len/2)
        {
            printf("YES\n%d\n",len/2+1);
        }
        else if(num1==0)
        {
            printf("YES\n");
            printf("%d\n",flag1+1);
        }
        else if(num2==0)
        {
            printf("YES\n");
            printf("%d\n",flag2+1);
        }
        else printf("NO\n");
    }
    return 0;
}
View Code

L题   分别算出两支队在比赛中最多赢得场次 从而判断能不能获胜(大于n/2)

技术分享图片
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <vector>
using namespace std;
const int maxn= 2e5+10;
const int maxm= 1e4+10;
const int inf = 0x3f3f3f3f;
typedef long long ll;
int n;
int a[maxn],b[maxn];
int main()
{
    while(cin>>n)
    {
        for(int i=1;i<=n;i++)
            cin>>a[i];
        for(int i=1;i<=n;i++)
            cin>>b[i];
        sort(a+1,a+n+1);
        sort(b+1,b+n+1);
        int maxa=0,maxb=0,mina,minb,p=0;
        for(int i=1,j=1;i<=n;)
        {
            if(a[i]>b[j])
            {
                maxa++;
                i++;
                j++;
            }
            else
            {
                i++;
            }
        }
        minb=n-maxa;
        for(int i=1,j=1;i<=n;)
        {
            if(b[i]>a[j])
            {
                maxb++;
                i++;
                j++;
            }
            else
            {
                i++;
            }
        }
        mina=n-maxb;
//        cout<<maxa<<" "<<mina<<endl;
//        cout<<maxb<<" "<<minb<<endl;
        if(maxa>n/2)
        {
            if(maxb>n/2)
                printf("Both\n");
            else
                 printf("First\n");
        }
        else
        {
            if(maxb>n/2)
                printf("Second\n");
            else
                 printf("None\n");
        }
    }
}
View Code

 

codeforces Gym 100971 A、B、C、F、G、K、L

标签:view   scanf   eee   比较   bsp   color   图片   style   ide   

原文地址:https://www.cnblogs.com/stranger-/p/8331266.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!