湖南大学第十六届程序设计竞赛(重现赛)

湖南大学第十六届程序设计竞赛(重现赛)

A:Triangles

题意:给你三个点,判断这3个点是否能组成三角形,如果能组成三角形,则输出是锐角,钝角,直角三角形中的哪一个
思路:先判断三个点是否共线或者重复,共线或重复不可能组成三角形;
如果是三角形,再用余弦定理判断是哪种三角形即可
ac代码:

#include <iostream>
#include <algorithm>
#include <stack>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#define ll long long
#define int long long
#define orz main
using namespace std;
int n,m,a[2005],c[200005],mod=1e9+7,k;
int check(double a,double b,double c,double d,double e,double f) 
{
	if (c != a && e != c && e != a)
		{
			double k1 = 1.0 * (d - b) / (c - a);
			double k2 = 1.0 * (f - d) / (e - c);
			double k3 = 1.0 * (f - b) / (e - a);
			if (k1 == k2 && k2 == k3)
			{
				return 0;
			}
			else
			{
				return 1;
			}

		}
		else if (a == c && c == e)
		{
				return 0;
		}
		else
		{
			if (b == d && d == f)
				return 0;
			else
				return 1;
		}
}
signed orz()
{
    int t;
    cin>>t;
    while(t--){
              double  a,b,c,x1,y1,x2,y2,x3,y3;
              cin>>x1>>y1>>x2>>y2>>x3>>y3;
              a=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
              b=sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
              c=sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
              if(check(x1,y1,x2,y2,x3,y3)==0)cout<<"invalid"<<endl;
                else if(x1==x2&&y1==y2||x1==x3&&y1==y3||x2==x3&&y2==y3)cout<<"invalid"<<endl;
          else if (a+b>c&&a+c>b&&b+c>a)
	      {
	     //     cout<<a<<" "<<b<<" "<<c<<endl;
	    if(abs(a*a+b*b-c*c)<=0.000001||abs(a*a+c*c-b*b)<=0.000001||abs(b*b+c*c-a*a)<=0.000001)
		    cout<<"right"<<endl;
	    else if(a*a+b*b-c*c>0&&a*a+c*c-b*b>0&&b*b+c*c-a*a>0)
	      cout<<"acute"<<endl;
	    else
	     cout<<"obtuse"<<endl;
         }
	else
	   cout<<"invalid"<<endl;
    }
    return 0;
}

时间复杂度o(t);

B:Yuki with emofunc and playf

题意:给你k=1,再给你一个n和x每一步有2个操作:
k=k*10;
k=k-1+x;
最小次数找出k=n的倍数(k%n==0);
思路:用bfs寻找最小次数,用一个数组标记已经走过的状态;因为找的是n的倍数;每次k=k%n就行。
ac代码:

#include <iostream>
#include <algorithm>
#include <stack>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#define ll long long
#define int long long
#define orz main
using namespace std;
int n,m,a[1000005],c[200005],mod=1e9+7,k;
struct node
{
    int now,step;
};

signed orz()
{
    cin>>n>>m;
    queue<node>q;
    q.push({1,0});
    a[1]=1;
    if(n==1)
    {
        cout<<0<<endl;
        return 0;
    }
    while(q.size())
    {
        node p=q.front();
        q.pop();
        int temp=p.now*10;
        if(temp%n==0)
        {
            cout<<p.step+1<<endl;
            return 0;
        }
        if(a[temp%n]==0)
        {
           // cout<<temp%n<<" "<<666<<endl;
            q.push({temp%n,p.step+1});
            a[temp%n]=1;
        }
   //     cout<<temp<<endl;
        temp=p.now-1+m;
        if(temp%n==0)
        {
           cout<<p.step+1<<endl;
            return 0;
        }
        if(a[temp%n]==0)
        {
            q.push({temp%n,p.step+1});
            a[temp%n]=1;
        }
     //   cout<<temp<<endl;

    }
    cout<<-1<<endl;

    return 0;
}

因为是%n的所有时间复杂度o(n);

D:Queueing

题意:
有n个队列,队列会变化遵循以下规则:
1.每个人以1/n的概率独立冲向第i个(1≤i≤n)窗口;
2、如果一开始A在B前面,并且A和B冲到同一个窗口,那么A还在B前面。

Playf现在在队伍中排名m,这意味着Playf前面还有m-1个人。 Playf 想知道在自助餐厅开放后他在新队列中的排名期望。
在这里插入图片描述
ac代码:

#include <iostream>
#include <algorithm>
#include <stack>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#define ll long long
#define int long long
#define orz main
using namespace std;
int n,m,s[200005],c[200005],mod=1e9+7,k;
signed orz()
{
   cin>>n>>m;
   printf("%.8lf",(double)(m-1)/(double)n+1.0);

    return 0;
}

F:Team

题意;求a+b+c
思路:pyhon yyds
ac代码:

a=input()
b=input()
c=input()
a=int(a)
b=int(b)
c=int(c)
print(a+b+c)

G:Binbin’s string

题意:给你2个字符串s和t。有2种操作:
1.从任意位置删除任意个字符
2.从任意位置添加任意个字符
找到最小次数使s变为t;
思路:很明显,s为任意串,2次操作后都能变为t(第一次删除s所有字符,第二次添加t字符串),所以我们要找2次操作以下的;当s为t的左右子串(从左右开始的子串)或者s删去一部分能变为t或者s增加一部分能变成t都是操作数为1当s==t时输出0;
ac代码:

#include <iostream>
#include <algorithm>
#include <stack>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#define ll long long
#define int long long
#define orz main
using namespace std;
int n,m,a[200005],mod=1e9+7,k;
signed orz()
{
   string s1,s2;
   cin>>s1>>s2;
   vector<int>q;
   int len1=s1.size();
    int len2=s2.size();
    if(s1==s2)
    {
        cout<<0<<endl;
    }
    else
    {
       if(len1==len2)
       {
           cout<<2<<endl;
           return 0;
       }
       else
       {
           if(len1<len2)
           {
               int l=0;
               while(l<len1&&s1[l]==s2[l])
               {
                   l++;
               }
               if(l>=len1)
               {
                   cout<<1<<endl;
                   return 0;
               }
               l=len1-1;
               int r=len2-1;
               while(l>=0&&r>=0&&s1[l]==s2[r])
               {
                   l--;
                   r--;
               }
               if(l<0)
               {
                   cout<<1<<endl;
                   return 0;
               }


           }
           else
           {
               int l=0;
               while(l<len2&&s1[l]==s2[l])
               {
                   l++;
               }
               if(l>=len2)
               {
                   cout<<1<<endl;
                   return 0;
               }
                l=len1-1;
               int r=len2-1;
               while(l>=0&&r>=0&&s1[l]==s2[r])
               {
                   l--;
                   r--;
               }
               if(r<0)
               {
                   cout<<1<<endl;
                   return 0;
               }
           }
           int l1=0,r1=len1-1;
        int l2=0,r2=len2-1;
        while(l1<len1&&l2<len2&&s1[l1]==s2[l2])
        {
            l1++;
            l2++;
        }
        while(r1>0&&r2>0&&s1[r1]==s2[r2])
        {
            r1--;
            r2--;
        }
        l1--;
        l2--;
        r1++;
        r2++;
      // cout<<l1<<" "<<r1<<" "<<l2<<" "<<r2<<endl;
        if(r1-l1==1||r2-l2==1)cout<<1<<endl;
        else cout<<2<<endl;
       }
    }

    return 0;
}

I:Binbin and Balls

Binbin gets a special material ball. As everyone knows, Binbin is a curious girl, so she decides to study the hardness of the ball. She finds a building with n storeys. Every time she chooses a floor f arbitrarily and throws a ball down from the f floor. The ball may be intact, but it may also be broken. If the ball is broken, it cannot be used again. Unfortunately, Binbin only has two balls, she wants to find the largest x that if she throws the ball down from the x floor, the ball will not be broken. Also, she wants to know in the worst case, how many times she needs to throw at least to find the value of x.
Note: It is certain that if a ball will be broken after dropped from x-th floor, it will also be broken after dropped from y-th floor where y>x.
题意:有 一个n 层的建筑。 每次她随意选择f层,从f层往下扔一个球。 球可能完好无损,但也可能损坏。 如果球被打破,则不能再次使用。只有两个球,找到最大的x,如果把球从x 层扔下去,球不会被打破。在最坏的情况下,需要抛出多少次才能找到 x 的值。
注意:可以肯定的是,如果一个球从 x 层落下后会被打破,那么它也会在 y>x 的 y 层落下后被打破。
思路:二分答案
在这里插入图片描述

#include <iostream>
#include <algorithm>
#include <stack>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#define ll long long
#define int long long
#define orz main
using namespace std;
int n,m;int mod=1000000007;

signed main()
{
       int t;
       cin>>t;
       while(t--)
       {
           cin>>n;
           int l=1,r=2e9;
           while(l<r)
           {
               int mid=l+r>>1;
               if(mid*(mid+1)/2<n)l=mid+1;
               else r=mid;
           }
           cout<<l<<endl;
       }

    return 0;
}

L:Cracked Pipes

在这里插入图片描述
题意:有6种水管,从(1,0)是否能到达(n,m+1);
思路:从(1,1)开始遍历,用并查集维护集合,遍历到(n,m)时看(1,1)和(n,m)是否在一个集合里,然后再看(1,1)是否有向左的接口,(n,m)是否有向右的接口,比赛的时候不知道为什么遍历一遍不能ac,赛后遍历2次就能ac
ac代码:

#include <iostream>
#include <algorithm>
#include <stack>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#define ll long long
#define int long long
#define orz main
using namespace std;
int n,m,a[200005],c[200005],mod=1e9+7,k;
int ask(int x)
{
    if(a[x]==x)return x;
    return  a[x]=ask(a[x]);
}
void add(int x,int y)
{
    a[ask(x)]=ask(y);
}
signed orz()
{
    cin>>n>>m;
    int b[n+3][m+3],map[n+3][m+3];
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cin>>b[i][j];
        }
    }
        int k=1;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                map[i][j]=k;
                k++;
            }
        }
    for(int i=1;i<=n*m;i++)
    {
        a[i]=i;
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(b[i][j]==1)
            {
                if(b[i-1][j]==1||b[i-1][j]==3||b[i-1][j]==6)
                {
                    add(map[i-1][j],map[i][j]);           //上
                }
                if(b[i+1][j]==1||b[i+1][j]==4||b[i+1][j]==5)
                {
                    add(map[i+1][j],map[i][j]);            //下
                }
            }
            if(b[i][j]==2)
            {
                if(b[i][j-1]==2||b[i][j-1]==3||b[i][j-1]==5||b[i][j-1]==6)
                {
                    add(map[i][j-1],map[i][j]);    //左
                }
                if(b[i][j+1]==2||b[i][j+1]==4||b[i][j+1]==5||b[i][j+1]==6)
                {
                    add(map[i][j+1],map[i][j]);    //右
                }
            }
            if(b[i][j]==3)
            {
                 if(b[i+1][j]==1||b[i+1][j]==4||b[i+1][j]==5)
                {
                    add(map[i+1][j],map[i][j]);            //下
                }
                 if(b[i][j+1]==2||b[i][j+1]==4||b[i][j+1]==5||b[i][j+1]==6)
                {
                    add(map[i][j+1],map[i][j]);    //右
                }
            }
            if(b[i][j]==4)
            {
                if(b[i-1][j]==1||b[i-1][j]==3||b[i-1][j]==6)
                {
                    add(map[i-1][j],map[i][j]);           //上
                }
                if(b[i][j-1]==2||b[i][j-1]==3||b[i][j-1]==5||b[i][j-1]==6)
                {
                    add(map[i][j-1],map[i][j]);    //左
                }
            }
            if(b[i][j]==5)
            {
               if(b[i-1][j]==1||b[i-1][j]==3||b[i-1][j]==6)
                {
                    add(map[i-1][j],map[i][j]);           //上
                }
                if(b[i][j-1]==2||b[i][j-1]==3||b[i][j-1]==5||b[i][j-1]==6)
                {
                    add(map[i][j-1],map[i][j]);    //左
                }
                 if(b[i][j+1]==2||b[i][j+1]==4||b[i][j+1]==5||b[i][j+1]==6)
                {
                    add(map[i][j+1],map[i][j]);    //右
                }
            }
            if(b[i][j]==6)
            {
                 if(b[i+1][j]==1||b[i+1][j]==4||b[i+1][j]==5)
                {
                    add(map[i+1][j],map[i][j]);            //下
                }
                if(b[i][j-1]==2||b[i][j-1]==3||b[i][j-1]==5||b[i][j-1]==6)
                {
                    add(map[i][j-1],map[i][j]);    //左
                }
                if(b[i][j+1]==2||b[i][j+1]==4||b[i][j+1]==5||b[i][j+1]==6)
                {
                    add(map[i][j+1],map[i][j]);    //右
                }
            }

        }
    }
        for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(b[i][j]==1)
            {
                if(b[i-1][j]==1||b[i-1][j]==3||b[i-1][j]==6)
                {
                    add(map[i-1][j],map[i][j]);           //上
                }
                if(b[i+1][j]==1||b[i+1][j]==4||b[i+1][j]==5)
                {
                    add(map[i+1][j],map[i][j]);            //下
                }
            }
            if(b[i][j]==2)
            {
                if(b[i][j-1]==2||b[i][j-1]==3||b[i][j-1]==5||b[i][j-1]==6)
                {
                    add(map[i][j-1],map[i][j]);    //左
                }
                if(b[i][j+1]==2||b[i][j+1]==4||b[i][j+1]==5||b[i][j+1]==6)
                {
                    add(map[i][j+1],map[i][j]);    //右
                }
            }
            if(b[i][j]==3)
            {
                 if(b[i+1][j]==1||b[i+1][j]==4||b[i+1][j]==5)
                {
                    add(map[i+1][j],map[i][j]);            //下
                }
                 if(b[i][j+1]==2||b[i][j+1]==4||b[i][j+1]==5||b[i][j+1]==6)
                {
                    add(map[i][j+1],map[i][j]);    //右
                }
            }
            if(b[i][j]==4)
            {
                if(b[i-1][j]==1||b[i-1][j]==3||b[i-1][j]==6)
                {
                    add(map[i-1][j],map[i][j]);           //上
                }
                if(b[i][j-1]==2||b[i][j-1]==3||b[i][j-1]==5||b[i][j-1]==6)
                {
                    add(map[i][j-1],map[i][j]);    //左
                }
            }
            if(b[i][j]==5)
            {
               if(b[i-1][j]==1||b[i-1][j]==3||b[i-1][j]==6)
                {
                    add(map[i-1][j],map[i][j]);           //上
                }
                if(b[i][j-1]==2||b[i][j-1]==3||b[i][j-1]==5||b[i][j-1]==6)
                {
                    add(map[i][j-1],map[i][j]);    //左
                }
                 if(b[i][j+1]==2||b[i][j+1]==4||b[i][j+1]==5||b[i][j+1]==6)
                {
                    add(map[i][j+1],map[i][j]);    //右
                }
            }
            if(b[i][j]==6)
            {
                 if(b[i+1][j]==1||b[i+1][j]==4||b[i+1][j]==5)
                {
                    add(map[i+1][j],map[i][j]);            //下
                }
                if(b[i][j-1]==2||b[i][j-1]==3||b[i][j-1]==5||b[i][j-1]==6)
                {
                    add(map[i][j-1],map[i][j]);    //左
                }
                if(b[i][j+1]==2||b[i][j+1]==4||b[i][j+1]==5||b[i][j+1]==6)
                {
                    add(map[i][j+1],map[i][j]);    //右
                }
            }

        }
    }


    if((b[1][1]==2||b[1][1]==4||b[1][1]==5||b[1][1]==6)&&(b[n][m]==3||b[n][m]==2||b[n][m]==5||b[n][m]==6))
    {
      if(a[map[1][1]]==a[map[n][m]])  cout<<"YES"<<endl;
      else cout<<"NO"<<endl;
    }
    else cout<<"NO"<<endl;


    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值