ICPC区域赛热身赛第二场补题

昨天是第二场ICPC热身赛,总体难度没有第一场大,但结果并没有预期好,团队只A了6题,其实有一题小模拟我可以做出来的,但是因为公式计算错误没有成功,很愧疚


第一题

牛客 I
For the UCF High School Programming Tournament, the judges were located in the Engineering building, and most of the teams were in the Classroom building, which is on the other side of Pegasus Circle.

Chris was walking to the Classroom building for the first time, and was joined by Jeremy, who had made the hike a couple of times already.
“Jeremy, is it faster to stay on the circle, or to cut through the middle using the boardwalks that go to the Student Union?” asked Chris.
“I don’t know.” Jeremy answered. “I think it’s about the same, but it might be slightly faster to use the walkways.”
“Well, if it’s about the same, let’s stick to the circle. I don’t want to be attacked by squirrels.”

The Problem:

Given two points on a circle, and two paths to get from one to the other—one following the perimeter of the circle, and the other by a sequence of connected straight line segments through the interior of the circle—determine the shorter of the two paths.
输入描述:
The input will contain multiple test cases, each consisting of two lines. The first line of each testcase contains six floating-point numbers:xc,yc,xs,ys,xf, andyf, where (xc,yc) is the center point of the circle, (xs,ys) is the start point for both paths (e.g., the Engineering building), and (xf,yf) is the finish point for both paths (e.g., the Classroom building).The circle will always have a radius greater than 1, and the start and finish points are both guaranteed to be at distinct pointson its perimeter, with an accuracy of at least 3 placesafter the decimal.The path along the perimeter is always in the directioncounter-clockwise around the circle.

The second line of each test case will start with an integer,n(1≤n≤ 10), followed by n pairs of floating-point numbers,x1,y1,x2,y2, …xn, and yn, where each pair (xi,yi) is a point inside the circle. The interior path traveled will be from point (xs,ys) to point (x1,y1), then from (x1,y1) to (x2,y2), then from (x2,y2) to (x3,y3), …, then from (xn,yn) to (xf,yf).

The last test case will be followed by a line containing six zeros. All numbers on an input line will beseparated from each other by one space, with no extra spaces at the beginning or end of lines. Assumethat all the input floating point numbers will be less than 1000.0 and greater than
-1000.0, with at most 6 places after the decimal.
输出描述:
For each test case in the input, output a line in either the format
Case #n:Stick to the Circle.
if the perimeter path is shorter,or
Case #n:Watch out for squirrels!
if the interior pathis shorter, where n is the num berof the input test case, starting at 1.
Assume that the two paths will not be equal, i.e., it is guaranteed that the two distances will not be equal. In particular, assume that the two paths will differ in length by 0.001 or more.

Leave a blank line after the output for each test case.
示例1
输入

5.0 5.0 10.0 5.0 5.0 10.0
6 8.5 4.7 6.9 5.0 3.6 6.5 4.2 7.1 4.2 8.3 4.7 8.8
2.0 8.0 0.5 16.87412 7.5 0.8761
2 3.25 9.25 7.0 7.0
0 0 0 0 0 0

复制

Case #1: Stick to the Circle.

Case #2: Watch out for squirrels!

思路解释

题目意思其实很简单,就是先给你三个点的坐标,其中一个圆心坐标,两个圆边上的坐标,然后给你若干圆内的点迹坐标,求起点到终点的距离是圆上走近还是园内近。这道题就是简单的解析几何模拟,运用高中数学知识可以轻松求解,但是比赛时可能是公式推错的原因没有AC。


AC代码

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define pre(i,a,b) for(int i=a;i>=b;--i)
#define m(x) memset(x,0,sizeof x)
const double PI = acos(-1);
typedef long long ll;
typedef struct trop{
    
    
    double x,y;
}CreateTrop;
double CountDistance(double xa,double ya,double xb,double yb){
    
    
    return sqrt((xa-xb)*(xa-xb)*(1.0)+(1.0)*(ya-yb)*(ya-yb));
}
CreateTrop dot;
double xc,yc,xs,ys,xf,yf;
double R,ans1,ans2,x;
double temp;
int n,cnt;
int main()
{
    
    
    cnt = 0;
    while(~scanf("%lf %lf %lf %lf %lf %lf",&xc,&yc,&xs,&ys,&xf,&yf))
    {
    
    
        if(xc==0&&yc==0&&xs==0&&ys==0&&xf==0&&yf==0)break;
        R = CountDistance(xc+0.0, yc+0.0, xs+0.0, ys+0.0);
        //cout << R << endl;
        x = CountDistance(xs, ys, xf, yf);
        //运用数学定理求解起点终点和圆心连线的角度
        double jiao = ((xs-xc)*(xf-xc)+(ys-yc)*(yf-yc))/(sqrt((xs-xc)*(xs-xc)+(ys-yc)*(ys-yc))*sqrt((xf-xc)*(xf-xc)+(yf-yc)*(yf-yc)));
        jiao = acos(jiao);
        ans1 = jiao * sqrt((xs-xc)*(xs-xc)+(ys-yc)*(ys-yc));
        
        scanf("%d",&n);
        ans2 = 0.0;
        CreateTrop start;
        start.x = xs;start.y = ys;
        
        while(n--){
    
    
            scanf("%lf %lf",&dot.x,&dot.y);
            temp = CountDistance(dot.x, dot.y,start.x , start.y);
            ans2 += temp;
            start.x = dot.x;
            start.y = dot.y;

        }
        
        temp = CountDistance(start.x, start.y, xf, yf);
        ans2 += temp;
        printf("Case #%d: ",++cnt);
        if(ans1-ans2>0.001)printf("Watch out for squirrels!");
        else printf("Stick to the Circle.");
        cout << endl;
        cout << endl;
    }
    return 0;
}


第二题

牛客 F
After two years of sharing a bedroom with you in a college dorm, Jeff finally has his own room. Excited about inviting girls over to his room, he ponders over what decorations the fairer sex will enjoy.1 He decides upon setting up a “fake” planetarium with a black ceiling and glow in the dark stars that form constellations. Unfortunately, in his haste, he has made several “errors” in setting up his constellations. See, everyone knows that constellations don’t have cycles in them. Instead, whenever we visually connect the stars together with lines, a tree is always formed. (A cycle is formed when you can start at a star and, using connections, go to one or more other stars and then end up at the original star.)

Since you were Jeff’s roommate for two years, you figure you’ll help him fix his constellations. Your job will be twofold: to count the number of constellations Jeff has, and to report how many of them have cycles and need to be fixed. A constellation consists of multiple stars that are all connected to one another (directly or indirectly). A constellation that needs fixing is simply one that has a cycle.
The Problem:
Given several configurations of stars and connections between stars, determine how many constellations are defined in each configuration and how many need fixing.
输入描述:
The first input line contains a positive integer, n (n ≤ 100), indicating the number of night skies to consider. The first line of each night sky contains two positive integers, s (s ≤ 1000), representing the number of stars for this night sky, and c (c ≤ 10000), representing the total number of connections between pairs of stars for this night sky. Each of the following c input lines contains two distinct positive integers representing a single connection between two stars. The stars in each test case will be numbered 1 through s, inclusive. A connection is considered bidirectional, thus, if a is connected to b, b is connected to a. Assume that all connections in a data set are distinct, i.e., no duplicates. Also assume that there will never be a connection from a star to itself.
1 This is based on a true story. The person who is the inspiration for this story did, in fact, major in Planetary Science and make his room’s ceiling a relatively accurate depiction of the night sky, as seen in Boston circa 1995.
输出描述:
For each test case, just output a line with the following format:
Night sky #k: X constellations, of which Y need to be fixed.
where k is the number of the night sky, starting at 1, X is the total number of constellations described in that night sky, and Y is how many of those constellations contain a cycle.
Leave a blank line after the output for each test case.

示例1
输入

3
5 4
1 2
1 3
2 3
4 5
8 5
1 2
3 4
6 7
6 8
8 7
3 2
1 2
1 3

输出

Night sky #1: 2 constellations, of which 1 need to be fixed. 

Night sky #2: 3 constellations, of which 1 need to be fixed. 

Night sky #3: 1 constellations, of which 0 need to be fixed.

说明

Note:In the second example, star number 5 is not connected to any other stars. This staron its own is NOT counted as a constellation, leaving only {1,2}, {3,4}and {6,7,8} as constellations, of which only the last one needs tobe fixed.


思路解释

对于每个图首先应该求出有多少个连通图就会有多少个星座,但是因为对于存在环的图中,是一个错误的需要修正的星座,因此我们首先可以求出中间有多少连通图,及存在多少星座,然后对于每个连通图里判断是否存在环.


AC代码

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define pre(i,a,b) for(int i=a;i>=b;--i)
#define m(x) memset(x,0,sizeof x)
const double PI = acos(-1);
typedef long long ll;
vector<int> vec[1100];
bool vis[1100],viss[1100][1100];
bool ok = false,okk=false;

void dfs(int x,int step){
    
    
    vis[x] = true;
    //如果能到达别的恒星则将ok标记为true
    if(step>=1){
    
    
        ok=true;
    }
    //从vec[x]的长度进行遍历所有星球
    for(int i=0;i<vec[x].size();i++){
    
    
        int tx = vec[x][i];
        //成环要满足以下条件:首先,tx这个节点曾经被标记过,也就是曾经作为节点出发过,而且从x可以到达tx(viss==0就是说以前没有走过)
        if(vis[tx]&&!viss[x][tx])okk = true;
        //如果tx这个节点没有被走过,执行以下操作
        if(!vis[tx]){
    
    
            //标记双向导通,可以从x走到tx,也可以从tx走到x
            viss[x][tx]=1;
            viss[tx][x]=1;
            dfs(tx,step+1);
        }
    }
    
}
int main()
{
    
    
        int t;
       scanf("%d",&t);
       for(int jj=1;jj<=t;jj++){
    
    
           if(jj!=1){
    
    
               printf("\n");
           }
           memset(viss,0,sizeof(viss));
           memset(vis,0,sizeof(vis));
           int n,m;
           scanf("%d%d",&n,&m);
           for(int i=0;i<=n;i++){
    
    
               vec[i].clear();
           }
           
           while(m--){
    
    
               int u,v;
               scanf("%d%d",&u,&v);
               vec[u].push_back(v);
               vec[v].push_back(u);
           }
           
           int ans = 0,ans1=0;
           for(int i=1;i<=n;i++){
    
    
               //这个点没访问过则去访问
               if(!vis[i]){
    
    
                   //ok记录是否有两个及以上的恒星相连
                   //okk记录是否有成环
                   ok = false;
                   okk = false;
                   //运行dfs深度优先搜索
                   dfs(i,0);
                   if(ok){
    
    
                       ans++;
                   }
                   if(ok && okk){
    
    
                       ans1++;
                   }
               }
           }
           
           printf("Night sky #%d: %d constellations, of which %d need to be fixed. \n",jj,ans,ans1);
       }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/DAVID3A/article/details/114848684
今日推荐