[POI 2011]Sticks(乱搞)

题目链接

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

题目大意

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

思路

我们可以把所有的木棍丢到一起,按照长度升序排序,然后枚举前i 根木棍,维护其中的长度最大的三根颜色互不相同的木棍,然后判断这三根木棍是否能构成三角形。

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

代码

#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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章