Loading

Codeforces Gym101097I:Sticks (思维)

http://codeforces.com/gym/101097/attachments

题意:现在有k种颜色的木棍,每种颜色有ni根木棍,每根木棍有一个长度,问是否有三根木棍可以组成三角形,并且这三根木棍的颜色都不相同。

思路:忘了并不能直接枚举第i根,然后找i-1和i-2根,因为还有很多情况没考虑到。

可以用三个变量,分别存储当前最大的三种颜色的最大值,然后根据新进来的颜色进行判定,进行更新。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 #define N 5000010
 5 #define INF 0x3f3f3f3f
 6 struct node {
 7     int id; LL val;
 8     bool operator < (const node &rhs) const {
 9         if(val != rhs.val) return val < rhs.val;
10         return id < rhs.id;
11     }
12 } ;
13 vector<LL> vec;
14 vector<node> p;
15 node a, b, c;
16 
17 int main() {
18     freopen("sticks.in", "r", stdin);
19     freopen("sticks.out", "w", stdout);
20     int k; scanf("%d", &k);
21     int cnt = 0;
22     for(int i = 1; i <= k; i++) {
23         int n; scanf("%d", &n);
24         vec.clear();
25         for(int j = 1; j <= n; j++) {
26             LL x; scanf("%lld", &x);
27             vec.push_back(x);
28         }
29         sort(vec.begin(), vec.end());
30         vec.erase(unique(vec.begin(), vec.end()), vec.end());
31         for(int j = 0; j < vec.size(); j++) p.push_back( (node) { i, vec[j] } );
32     }
33     sort(p.begin(), p.end());
34     bool flag = 0;
35 //    a = p[1]; b = p[0]; c.id = 0, c.val = 0;
36     for(int i = 0; i < p.size() && !flag; i++) {
37         if(a.id == p[i].id) {
38             if(p[i].val < b.val + c.val) printf("%d %lld %d %lld %d %lld\n", p[i].id, p[i].val, b.id, b.val, c.id, c.val), flag = 1;
39             a = p[i];
40         } else if(b.id == p[i].id) {
41             if(p[i].val < a.val + c.val) printf("%d %lld %d %lld %d %lld\n", p[i].id, p[i].val, a.id, a.val, c.id, c.val), flag = 1;
42             b = a; a = p[i];
43         } else {
44             if(p[i].val < a.val + b.val) printf("%d %lld %d %lld %d %lld\n", p[i].id, p[i].val, a.id, a.val, b.id, b.val), flag = 1;
45             c = b; b = a; a = p[i];
46         }
47     }
48     if(!flag) puts("NIE");
49     return 0;
50 }

 

posted @ 2017-04-28 00:22  Shadowdsp  阅读(224)  评论(0编辑  收藏  举报