[bzoj2529][Poi2011]Sticks_贪心
Sticks bzoj-2529 Poi-2011
题目大意:给你n根木棒,每种木棒有长度和颜色,颜色共有k种,求满足条件的3根木棒使得这3根木棒颜色互不相同且可以围成三角形。
注释:$1\le n \le 10^6$,$1\le k\le 50$。
想法:我们这么想:假设当前木棍是满足题意的三根木棍中的最大者,那么剩下两根木棍一定是越大越好。所以,将所有木棍按长度排序,每次记录一下连续的三个长度不同的三根木棍,然后比较。知道有答案位置。
最后,附上丑陋的代码... ...
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAXN 1000010
#define MAXM 1010
struct Node
{
int l;
int c;
friend bool operator <(const Node &x,const Node &y)
{
return x.l<y.l;
}
};
int k;
int n,m;
Node a[MAXN];
int x,y,z,lx,ly,lz;
Node ans[10];
int main()
{
scanf("%d",&k);
for(int i=1;i<=k;i++)
{
scanf("%d",&m);
for(int j=1;j<=m;j++)
{
a[++n].c=i;
scanf("%d",&a[n].l);
}
}
sort(a+1,a+n+1);
for(int j=1;j<=3;j++)
{
ans[j].l=ans[j].c=0;
}
for(int i=1;i<=n;i++)
{
bool flag=0;
for(int j=1;j<=3;j++)
{
if(ans[j].c==a[i].c)
{
ans[j].l=a[i].l;
flag=1;
}
}
if(!flag)
{
ans[1]=a[i];
}
sort(ans+1,ans+4);
if(ans[1].l+ans[2].l>ans[3].l&&ans[1].l!=0)
{
for(int j=1;j<=3;j++)
{
printf("%d %d ",ans[j].c,ans[j].l);
}
printf("\n");
return 0;
}
}
printf("NIE\n");
return 0;
}
小结:贪心思想有时是容易的,但是能不能想到贪心就看造化了... ...
| 欢迎来原网站坐坐! >原文链接<

浙公网安备 33010602011771号