[POI 2011]Sticks(乱搞)

21 篇文章 0 订阅
2 篇文章 0 订阅

题目链接

http://main.edu.pl/en/archive/oi/18/pat

题目大意

给你k种颜色,每种颜色有一些木棍,给出每种木棍的长度和颜色,问是否能选出三根颜色互不相同的木棍,构成一个三角形

思路

我们可以把所有的木棍丢到一起,按照长度升序排序,然后枚举前 i <script type="math/tex" id="MathJax-Element-24">i</script>根木棍,维护其中的长度最大的三根颜色互不相同的木棍,然后判断这三根木棍是否能构成三角形。

实际上就是在枚举长度最长的那根木棍,显然另外两个比它短的木棍的长度之和应该大于它,因此这两根木棍的长度应该尽量长,所以上述做法是正确的

代码

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>

#define MAXN 1100000

using namespace std;

pair<int,int>pr[MAXN];
int tot=0,K;

int main()
{
    scanf("%d",&K);
    for(int i=1;i<=K;i++)
    {
        int cnt;
        scanf("%d",&cnt);
        for(int j=1;j<=cnt;j++)
        {
            tot++;
            scanf("%d",&pr[tot].first);
            pr[tot].second=i;
        }
    }
    sort(pr+1,pr+tot+1);
    pair<int,int>first=make_pair(-1,-1),second=make_pair(-1,-1),third=make_pair(-1,-1);
    for(int i=1;i<=tot;i++)
    {
        if(pr[i].second==first.second)
            first=pr[i];
        else if(pr[i].second==second.second)
        {
            second=first;
            first=pr[i];
        }
        else
        {
            third=second;
            second=first;
            first=pr[i];
        }
        if(first.first==-1||second.first==-1||third.first==-1) continue;
        if(second.first+third.first>first.first)
        {
            printf("%d %d %d %d %d %d\n",first.second,first.first,second.second,second.first,third.second,third.first);
            return 0;
        }
    }
    printf("NIE\n");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值