湖南大学第十四届ACM程序设计大赛 D Dandan's lunch

链接:https://ac.nowcoder.com/acm/contest/338/D
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 65536K,其他语言131072K
64bit IO Format: %lld

题目描述

    As everyone knows, there are now n people participating in the competition. It was finally lunch time after 3 hours of the competition. Everyone brought a triangular bread. When they were going to eat bread, some people found that they solved more problems than others, but their bread was smaller than others. They thought it was very unfair. In this case, they will forcibly exchange bread with the other party (may be exchanged many times, someone can still exchange with others after being exchanged if the above conditions are satisfied, the other party can not refuse).
    The description of the bread is given by the coordinates of the three vertices of the triangle. The size of the bread is twice the size of the triangle area, ensuring that there are no two breads of the same size, and the number of problems each person makes is different.
    Dandan is also one of the contestants. Now he knows the number of problems solved by each person and the description of the bread they bring. Now he wants to know that after all the exchanges are over (That is, there can be no more exchanges between any two people), The size of the bread he can get.   

输入描述:

The first line gives an integer n, which indicates the number of people who participated in the competition.
Lines 2~n+1, each line gives 7 integers separated by spaces such as:
num x1 y1 x2 y2 x3 y3
num represents the number of the ith personal problem solving. (x1, y1) (x2, y2) (x3, y3) represents the coordinates of the three points of the bread of the triangle with the i-th person. ensure that three points are not in the same line.
Notice that the second line (the first person) represents Dandan's information.
Data guarantee: 0<n<=1e5,0<=num<1e9, -1e8<x1, x2, x3, y1, y2, y3<1e8.

输出描述:

Outputs an integer representing the size of the bread that DanDan eventually gets.

示例1

输入

复制

1
100000000 0 0 10000 0 0 1000

输出

复制

10000000

说明

There's only Dandan alone.

示例2

输入

复制

4
3 0 0 1 0 0 1
1 0 0 2 0 0 2
2 0 0 3 0 0 3
4 0 0 4 0 0 4

输出

复制

9

说明

Dandan solved three problems, ranking second. Ranking first can get the biggest bread, so he can get the second largest bread.

备注:

1e5=100000
1e8=100000000
1e9=1000000000

题目大意:

众所周知,现在有N个人参加了比赛。比赛进行了3个小时后,终于到了吃午饭的时间。每个人都带了一个三角面包。当他们准备吃面包时,一些人发现他们解决的问题比其他人多,但他们的面包比其他人小。他们认为这很不公平。在这种情况下,他们会强行与对方交换面包(可以多次交换,如果满足上述条件,对方不能拒绝,交换后仍可以与他人交换)。
面包的描述由三角形三个顶点的坐标给出。面包的大小是三角形区域的两倍,确保没有两个相同大小的面包,每个人制造的问题数量也不一样。
丹丹也是参赛者之一。现在他知道每个人解决的问题的数量和他们带来的面包的描述。现在他想知道,在所有的交换都结束后(也就是说,任何两个人之间都不能再交换),他能得到的面包的大小。
第一行给出一个整数n,表示参加比赛的人数。
第2行~n+1,每行给出7个整数,用空格分隔,例如:
数字x1 y1 x2 y2 x3 y3
num表示第i次个人问题解决的次数。(x1,y1)(x2,y2)(x3,y3)表示三角形面包的三个点与第i个人的坐标。确保三个点不在同一条线上。
注意,第二行(第一个人)表示丹丹的信息。
数据保证:0<n<1e5,0<num<1e9,-1e8<x1,x2,x3,y1,y2,y3<1e8。
输出一个整数,表示丹丹最终得到的面包大小。

分析:题目说的很详细,那么,做题最多的人一定会得到最大的面包,做题第二多的人一定会得到第二大的面包。。。。做题最少的人一定会得到最小的面包,将每个人的做题数和面包大小进行排序,输出对应的面包大小即可。关键在于如何求面包的大小,题目提供了面包的三个点坐标。还说了面包的大小是三角形区域的两倍,我们就可以联想到用三点求三角形面积后再扩大2倍就是面包的大小。

设A(x1,y1),B(x2,y2),C(x3,y3)
由A-->B-->C-->A 按逆时针方向转。(行列式书写要求)
设三角形的面积为S
则S=(1/2)*(下面行列式)
|x1 y1 1|
|x2 y2 1|
|x3 y3 1|
S=(1/2)*(x1y2*1+x2y3*1+x3y1*1-x1y3*1-x2y1*1-x3y2*1)
即用三角形的三个顶点坐标求其面积的公式为:
S=(1/2)*(x1y2+x2y3+x3y1-x1y3-x2y1-x3y2)

则2S=(x1y2+x2y3+x3y1-x1y3-x2y1-x3y2)

注意求面积的结果是绝对值的形式。

#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
ll area(ll x1,ll y1,ll x2,ll y2,ll x3,ll y3)//已知三点求三角形面积
{
    return (labs(x1*y2+x2*y3+x3*y1-x1*y3-x2*y1-x3*y2));//注意结果要取绝对值
}
 
int main()
{
    long long x,n[100005],x1,y1,x2,y2,x3,y3,s[100005],k;
    cin>>x;
     
    for(int i=1;i<=x;i++)
    {
        cin>>n[i]>>x1>>y1>>x2>>y2>>x3>>y3;//依次输入每点做题数及三点坐标
        s[i]=area(x1,y1,x2,y2,x3,y3);
    }
    k=n[1];
    sort(n+1,n+x+1);//对做题数进行排序
    sort(s+1,s+x+1);//对面包大小进行排序
    for(int i=1;i<=x;i++)//对于大数据容易超时,建议使用二分查找。
    {
        if(n[i]==k)//如果查找数据做题数相对应
        {
            cout<<s[i]<<endl;//输出对应面包大小
            break;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/basketball616/article/details/86062936
今日推荐