BZOJ2529 [Poi2011]Sticks 【贪心】
题目链接
题解
要组成三角形,当且仅当最长边长度小于另两条边之和
我们就枚举最长边,另两条边当然是越大越好
我们将所有边排序,从小枚举并记录各个颜色的最长边
当枚举到当前边时,找到除了当前颜色外其它颜色最长边的最大值和次大值,检查一下加起来是否大于当前边长度
复杂度\(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;
}