BZOJ2529 [Poi2011]Sticks 【贪心】

题目链接

BZOJ2529

题解

要组成三角形,当且仅当最长边长度小于另两条边之和
我们就枚举最长边,另两条边当然是越大越好
我们将所有边排序,从小枚举并记录各个颜色的最长边
当枚举到当前边时,找到除了当前颜色外其它颜色最长边的最大值和次大值,检查一下加起来是否大于当前边长度
复杂度\(O(nlogn + nk)\)

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define REP(i,n) for (register int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 1000005,maxm = 100005,INF = 1000000000;
inline int read(){
    int out = 0,flag = 1; char c = getchar();
    while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
    while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
    return out * flag;
}
int mx[55],co[maxn],len[maxn],id[maxn],n,K;
inline bool cmp(const int& a,const int& b){
    return len[a] < len[b];
}
int main(){
    K = read(); int x;
    REP(i,K){
        x = read();
        REP(j,x) co[++n] = i,len[n] = read(),id[n] = n;
    }
    sort(id + 1,id + 1 + n,cmp);
    int c1,c2,u;
    REP(i,n){
        u = id[i];
        c1 = c2 = 0;
        for (register int j = 1; j <= K; j++)
            if (j != co[u]){
                if (!c1 || mx[j] > mx[c1]) c2 = c1,c1 = j;
                else if (!c2 || mx[j] > mx[c2]) c2 = j;
            }
        if (c1 && c2 && mx[c1] + mx[c2] > len[u]){
            printf("%d %d %d %d %d %d",c1,mx[c1],c2,mx[c2],co[u],len[u]);
            return 0;
        }
        mx[co[u]] = max(mx[co[u]],len[u]);
    }
    puts("NIE");
    return 0;
}

转载于:https://www.cnblogs.com/Mychael/p/9234064.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值