牛客暑假1 【持续更新】

本文介绍了两个编程挑战:一个涉及新手程序员以字典序排序整数的问题,另一个是关于电力系统连接优化的路径长度计算。在字典序排序问题中,程序需要找出111到nnn之间的最大字典序数,而在电力系统问题中,目标是确定最小电线总长度以连接所有节点。这两个问题都需要对排序和几何知识的巧妙应用。
摘要由CSDN通过智能技术生成

链接:登录—专业IT笔试面试备考平台_牛客网
来源:牛客网


G Lexicographical Maximum 【签到题题一道】

题目描述

Eibwen is a newbie in Python.

You might know that when you input a number in the command line, your Python program will receive a string containing that number instead of a number that can be used to calculate. This is an interesting feature that a newbie might not know.

Eibwen wants to find the maximum of some given numbers, so he writes a program to sort the list of the numbers and print the last element in the sorted list. However, as a newbie, Eibwen doesn't know the feature. He actually sorts the list of number strings in lexicographical order and prints the last string in the list.

Now Eibwen runs his program, inputs all the integers from 111 to nnn, and finds his program really slow. Could you help him find out the expected output of his program?

输入描述:

The only line contains an integer nnn (1≤n≤1010000001\leq n\leq 10^{1000000}1≤n≤101000000) — the size of Eibwen's input.

输出描述:

Print the expected output of Eibwen's program in a single line, which actually is the lexicographically maximum from 111 to nnn.

示例1

输入

复制616

616

输出

复制99

99
#include <bits/stdc++.h>
using namespace std;
int main()
{
    string n;
    cin>>n;
    
    bool flag=false;
    string res;
    for(int i=0;i<n.size()-1;i++)
    {
        if(n[i]!='9')
        {
            flag=true;
        }
        res+='9';
    }
    
    
    if(!flag)
    {
        cout<<n<<endl;
    }
    else
    {
        cout<<res<<endl;
    }
    
    return 0;
}

 链接:登录—专业IT笔试面试备考平台_牛客网
来源:牛客网
A Villages: Landlines  【存边界,对一个圆来说存左右边界】【排序】

题目描述

The game development team NIO Tech releases its debut game, Villages: Landlines. In this game, players develop their villages and enjoy their peaceful country life.

The game Villages: Landlines contains a one-dimensional power system. Initially, there are several power stations and buildings. The goal of the game is to make every power stations and buildings connected together in the power system, forming a connected graph. Players can build some power towers and wires in the game, which are the tools to connect power stations and buildings.

Power stations or buildings can connect to power towers without wires, while power towers must connect to each other by wires. What's more, power stations and buildings can't connect to each other directly, they must connect through power towers.

Assuming that the coordinate of a building is xix_ixi​ and its receiving radius is rir_iri​, all the power towers whose distance from the building is no greater than rir_iri​ are directly connected to it without wires. That is to say, for the power tower located at xxx, it is directly connected to the building at xix_ixi​ without wires if and only if ∣x−xi∣≤ri|x-x_i|\leq r_i∣x−xi​∣≤ri​. Similarly, assuming that the power supply radius of a power station is rsr_srs​, all the power towers whose distance from the power station is no more than rsr_srs​ are directly connected to it without wires. Supposing that the coordinates of two power towers are xAx_AxA​ and xBx_BxB​, players can connect them with the wire, and the length of wire required is ∣xA−xB∣|x_A-x_B|∣xA​−xB​∣. A power tower can be connected to any number (possibly zero) of power towers, buildings and power stations.

In order to make the game more friendly to the rookies, Nostalgia, a member of NIO Tech, decides to develop the power distribution recommendation function. In the case of using any number of power towers, players can choose exactly one power station and several buildings to get an optimal power supply scheme, which uses the shortest total length of wires to complete the power system. However, Nostalgia is not sure whether her scheme is correct, so she needs your help to calculate the total length of wires used in the optimal scheme to determine the correctness of her scheme.

Note that the players can build a new power tower at coordinate xxx even if there exists a power station or a building at xxx. There might be more than one power station or building at the same coordinate.

输入描述:

The first line contains a single integer nnn (1≤n≤2×1051\leq n\leq 2\times 10^51≤n≤2×105).

The second line contains two integers xs,rsx_s, r_sxs​,rs​ (−109≤xs≤109,1≤rs≤109-10^9 \leq x_s \leq 10^9, 1\leq r_s\leq 10^9−109≤xs​≤109,1≤rs​≤109) — the coordinate of the power station and its power supply radius.


The iii-th line of the next n−1n-1n−1 lines contains two integers xi,rix_i, r_ixi​,ri​ (−109≤xi≤109,1≤ri≤109-10^9 \leq x_i \leq 10^9, 1\leq r_i\leq 10^9−109≤xi​≤109,1≤ri​≤109) — the coordinate of iii-th building and its receiving radius.

输出描述:

Print an integer in a single line, denoting the total length of wires used in the optimal scheme.

示例1

输入

复制5 0 1 0 3 5 1 6 1 9 2

5
0 1
0 3
5 1
6 1
9 2

输出

复制1

1

示例2

输入

复制2 -1000000000 1000000000 1000000000 1000000000

2
-1000000000 1000000000
1000000000 1000000000

输出

复制0

0

备注:

For the first sample, one possible optimal scheme is building power towers at 1,3,4,6,71,3,4,6,71,3,4,6,7, and connect the power towers at 333 and 444 with the wire of length 111.

这道题出现了左边界维护了,右边界就不好维护

                      右边界维护了,左边界就不好维护

所以按照左边界排序,

右边界用maxv来维护。

#include <bits/stdc++.h>
using namespace std;
const int N=2e5+10;
typedef long long LL;
struct Node
{
    int l,r;
}q[N];

bool cmp(struct Node a,struct Node b)
{
    return a.l<b.l;
}

int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        int x,r;
        cin>>x>>r;   //位置和半径
        q[i].l=x-r;  //存左边界
        q[i].r=x+r;  //存右边界
    }
    
    sort(q+1,q+1+n,cmp);
    /*
    for(int i=1;i<=n;i++)
    {
        cout<<q[i].l<<" "<<q[i].r<<endl;
    }
    */
    LL res=0;
    int maxv=-999999999;
    for(int i=1;i<=n;i++)
    {
        maxv=max(maxv,q[i].r);
        if(maxv<q[i+1].l)
        {
            res+=q[i+1].l-maxv;
        }
    }
    
    cout<<res<<endl;
    
    return 0;
}

 链接:登录—专业IT笔试面试备考平台_牛客网
来源:牛客网
D Mocha and Railgun 【几何】

题目描述

There is a candy store near Mocha's school. It's said that the storekeeper, Dagashiya, can cast the railgun spell. To be the most powerful Mahou Shoujo, Mocha asks Dagashiya to teach her the railgun spell.

To test her mastery of this spell, Mocha creates a circular wall CCC whose center is (0,0)(0,0)(0,0). The railgun spell has a base segment ABABAB with length 2d2d2d. When it is cast, a point PPP on the circular wall will be destroyed if and only if it meets the following conditions:
 

  1. The projection of point PPP on line ABABAB lies on the segment ABABAB (inclusive).
  2. A,B,PA,B,PA,B,P make a counterclockwise turn, that is to say AB→×AP→>0\overrightarrow{AB} \times \overrightarrow{AP} > 0AB×AP>0.


Mocha chooses a point QQQ which strictly lies in the circle and sets QQQ as the center of the base segment. Then Mocha will choose an angle α\alphaα arbitrarily and rotate the base segment around QQQ by α\alphaα. Because Mocha is a rookie of the spell, the endpoints of the base segment will always lie in CCC strictly, which means the distance from QQQ to the wall CCC is greater than ddd.

Mocha wants to know the maximum length of the wall that can be destroyed in one cast.

You can see the note for better understanding.

输入描述:

The first line is an integer TTT (1≤T≤1051\leq T\leq 10^51≤T≤105) — the number of test cases.

In each test case, the first line is an integer rrr (1≤r≤1091\leq r\leq 10^91≤r≤109) — the radius of the circular wall CCC.

The next line contains three integers xQ,yQ,dx_Q,y_Q,dxQ​,yQ​,d — the coordinates of the point QQQ and a half of the length of the base segment.

输出描述:

For each test case, print a real number in a single line, denoting the maximum length of the wall that Mocha can destroy in one cast. Your answer will be considered equal to the correct answer when their absolute or relative error doesn't exceed 10−610^{-6}10−6.

示例1

输入

复制1 2 0 0 1

1
2
0 0 1

输出

复制2.094395102393

2.094395102393

备注:

 

 

In the picture above, the blue lines are the boundaries of the railgun spell. The red arc is the wall being destroyed.

 比赛的时候差点就出了,非常可惜,吸取这次的教训。

由于还保留着高中的做题思维,对acos()直接出角度结果还不太熟练

以至于我用 cos(外角-内角)=cos(外角)cos(外角)+sin(外角)sin(内角) (这种做法精度丢失)

来求 a=外角-内角

其实不需要 ,

有了外角,直接 外角=acos(外角),就已经把外角的角度求出来了

                          内角=acos(内角),。。。。。

a=外角-内角,就求出来了

 

错误代码

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        double x,y,d,r;
        cin>>r;
        cin>>x>>y>>d;
        double x1;
        double x2;
        double ds=sqrt(x*x+y*y);
         
        x1=ds+d; 
        x2=ds-d;
         
         
 
        double cos1=(double)x1/(double)r;
        
        double sin1=sqrt(1-cos1*cos1);
         
        double cos2=(double)x2/(double)r;
        
        double sin2=sqrt(1-cos2*cos2);
         
        
        double b=cos2*cos1+sin2*sin1;
        //cout<<b<<endl;
        printf("%.12lf\n",abs(acos(b)*r));
         
    }
     
    return 0;
}

 正确代码

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        double x,y,d,r;
        cin>>r;
        cin>>x>>y>>d;
        double x1;
        double x2;
        double ds=sqrt(x*x+y*y);
         
        x1=ds+d; 
        x2=ds-d;
         
         
 
        double cos1=(double)x1/(double)r;
        
        double a1=acos(cos1);
         
        double cos2=(double)x2/(double)r;
         
        double a2=acos(cos2);
        
        
    
        //cout<<b<<endl;
        printf("%.12lf\n",(a2-a1)*r);
         
    }
     
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值