挑战程序设计竞赛(1.6-p26)
2017-09-04 08:38
190 查看
挑战程序设计竞赛
标签 : acm参考
P26
1.6 有n根棍子,棍子i的长度为ai,想要从中选出3根棍子组成周长尽可能长的三角形,请输出最大的周长,若无法组成三角形则输出0。
思路:直接三重循环遍历能得到结果,时间复杂度为$O(n^3)$,如果给定的棍子是排好序的,那事情就好办了,若$n1<n2<n3<n4<...<nk$,选取邻近的三个数即可。显然如果选择为ni,n(i+1),n(i+2)
#define ONLINE_JUDGE #include <iostream> #include <vector> #include <iterator> #include <algorithm> #include <functional> using namespace std; int main(int argc, char *argv[]) { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif int n; cin >> n; vector<int> coll; copy(istream_iterator<int>(cin), istream_iterator<int>(), back_inserter(coll)); sort(coll.begin(), coll.end(), greater<int>()); //copy(coll.begin(), coll.end(), ostream_iterator<int>(cout, " ")); bool found = false; for (int i = 0; i < n - 2; ++i) { if (coll[i] < coll[i + 1] + coll[i + 2]) { cout << coll[i] + coll[i + 1] + coll[i + 2] << endl; found = true; break; } } if (!found) { cout << 0 << endl; } #ifndef ONLINE_JUDGE fclose(stdin); fclose(stdout); system("out.txt"); #endif return 0; }
相关文章推荐
- poj3320 尺取法 <挑战程序设计竞赛>
- 挑战编程 程序设计竞赛训练手册-1.6.6 解释器(Interpreter)
- ACM Property Distribution(挑战程序设计竞赛)
- ACM Backward Digit Sums(挑战程序设计竞赛)
- ACM Cleaning Shifts(挑战程序设计竞赛)
- ACM Yogurt factory(挑战程序设计竞赛)
- 挑战程序设计竞赛 部分和问题
- [挑战程序设计竞赛] AOJ 0033 - Ball
- [挑战程序设计竞赛] POJ 3009 - Curling 2.0
- 堆的push,pop数组实现(挑战程序设计竞赛)
- 最小生成树---Kruskal算法---挑战程序设计竞赛读书笔记
- 挑战程序设计竞赛:三角形
- 挑战程序设计竞赛 2.1 最基础的“穷竭搜索”
- 挑战程序设计竞赛 3.6 与平面和空间打交道的计算几何
- 挑战程序设计竞赛选录:1
- 挑战程序设计竞赛: 硬币问题, 一直贪心
- 挑战程序设计竞赛 二叉堆(优先队列)的实现
- poj2376 区间贪心 <挑战程序设计竞赛>
- poj3276 反转 挑战程序设计竞赛
- ACM Osenbei(挑战程序设计竞赛)