BZOJ2529: [Poi2011]Sticks
Posted 嘒彼小星
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了BZOJ2529: [Poi2011]Sticks相关的知识,希望对你有一定的参考价值。
2529: [Poi2011]Sticks
Time Limit: 10 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 257 Solved: 133
[Submit][Status][Discuss]
Description
Little
Johnny was given a birthday present by his grandparents. This present
is a box of sticks of various lengths and colours. Johnny wonders if
there are three sticks in the set he has been given that would form a
triangle with different-coloured sides. Let us note that Johnny is
interested in non-degenerate triangles only, i.e., those with positive
area.
给出若干木棍,每根木棍有特定的颜色和长度。问能否找到三条颜色不同的木棍构成一个三角形。
(注意这里所说的三角形面积要严格大于0)
第一行给出一个整数k(3<=k<=50),表示颜色的种数。这k种颜色被标号为1至k。
接下来k行,第i+1描述颜色为i的木棍的信息。
首先一个整数Ni(1<=Ni<=10^6)表示颜色为i的木棍的数量。
接下来Ni个整数,表示这Ni根木棍各自的长度。
所有木棍的长度<=10^9。总木棍数量<=10^6。
你的程序应该仅输出一行
如果有解,输出6个整数,分别表示第一条边的颜色,第一条边的长度,第二条边的颜色,第二条边的长度,第三条边的颜色,第三条边的长度,这六个整数以空格分割。
如果有多组解,随便输出一组即可。
如果无解,输出 NIE
Input
In
the first line of the standard input an integer k(3<=k<=50)is
given, which is the number of different colours of sticks. The colours
themselves are numbered from 1 to k.
The
following klines contain descriptions of the sticks of particular
colours. The line no. i+1holds integers that describe the sticks of
colour , separated by single spaces. The first of these numbers,
Ni(1<=Ni<=10^6) denotes the number of sticks of colour . It is
followed, in the same line, by Niintegers denoting the lengths of the
sticks of colour . All lengths are positive and do not exceed10^9.
Furthermore, the total number of all sticks does not exceed 10^6.0020
In tests worth at least 30% of the points the following holds in addition: the total number of the sticks does not exceed 250.
Output
Your program should print (on the first and only line of the standard output) either:
· six
integers, separated by single spaces, that describe the construction of
a triangle with different-coloured sides as follows: the colour and the
length of the first stick, the colour and the length of the second
stick, and the colour and the length of the third stick,
· or the word NIE (Polish for no) if no such triple of sticks exists.
If
there are multiple triples of different-coloured sticks that give rise
to a triangle, your program may pick one such triple arbitrarily.
Sample Input
4
1 42
2 6 9
3 8 4 8
1 12
1 42
2 6 9
3 8 4 8
1 12
Sample Output
3 8 4 12 2 9
HINT
Source
【题解】
先按照长度排序
从前到后扫描。维护最长的三个颜色不同的木棍即可
从前到后扫描。维护最长的三个颜色不同的木棍即可
证明也比较显然
对于a,b,c
我们证一下如果a,b,c不合题意,那么以c为最大边的其他可能也不会满足题意
分各种情况讨论
发现是对的(唔)
很麻烦,不写了
1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 #include <algorithm> 6 #define max(a, b) ((a) > (b) ? (a) : (b)) 7 #define min(a, b) ((a) < (b) ? (a) : (b)) 8 9 inline void read(int &x) 10 { 11 x = 0;char ch = getchar(), c = ch; 12 while(ch < ‘0‘ || ch > ‘9‘)c = ch, ch = getchar(); 13 while(ch <= ‘9‘ && ch >= ‘0‘)x = x * 10 + ch - ‘0‘, ch = getchar(); 14 if(c == ‘-‘)x = -x; 15 } 16 17 const int MAXN = 1000000 + 10; 18 19 int k, n; 20 21 struct Node 22 { 23 int num,color; 24 }node[MAXN]; 25 26 bool cmp(Node a, Node b) 27 { 28 return a.num < b.num; 29 } 30 31 int main() 32 { 33 read(k); 34 if(k < 3) 35 { 36 printf("NIE"); 37 return 0; 38 } 39 for(register int i = 1;i <= k;++ i) 40 { 41 int tmp;read(tmp); 42 for(register int j = 1;j <= tmp;++ j) 43 { 44 node[++n].color = i; 45 read(node[n].num); 46 } 47 } 48 std::sort(node + 1, node + 1 + n, cmp); 49 int p1 = n - 1; 50 while(node[p1].color == node[n].color)-- p1; 51 int p2 = p1 - 1; 52 while(node[p2].color == node[p1].color || node[p2].color == node[n].color)-- p2; 53 if(node[p1].num + node[p2].num > node[n].num) 54 { 55 printf("%d %d %d %d %d %d", node[n].color,node[n].num,node[p1].color,node[p1].num,node[p2].color,node[p2].num); 56 return 0; 57 } 58 for(register int i = n - 1;i >= 1;-- i) 59 { 60 while(node[p1].color == node[i].color)-- p1; 61 p2 = min(p1 - 1, p2); 62 while(node[p2].color == node[p1].color || node[p2].color == node[i].color)-- p2; 63 if(p2 < 0 || p1 < 0)break; 64 if(node[p1].num + node[p2].num > node[i].num) 65 { 66 printf("%d %d %d %d %d %d", node[i].color,node[i].num,node[p1].color,node[p1].num,node[p2].color,node[p2].num); 67 return 0; 68 } 69 } 70 printf("NIE"); 71 return 0; 72 }
以上是关于BZOJ2529: [Poi2011]Sticks的主要内容,如果未能解决你的问题,请参考以下文章
BZOJ2213[Poi2011]Difference DP