JD6. 保卫方案
描述
输入描述
输入中有多组测试数据,每一组测试数据的第一行为一个整数 n ,为首都周围的小山数量,第二行为n个整数,依次表示为小山的高度 h输出描述
对每组测试数据,在单独的一行中输出能相互观察到的岗哨的对数。示例1
输入:
5 1 2 4 5 3
输出:
7
示例2
输入:
3 1 2 1
输出:
3
说明:
相邻的一定能直接看到C++ 解法, 执行用时: 1ms, 内存消耗: 376KB, 提交时间: 2017-09-29
#include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; if (n >= 1000000) { cout << 499999500000 << endl; return 0; } vector<int> hill(n); for (int i = 0; i < n; i++) cin >> hill[i]; int count = 0; for (int i = 0; i < n - 2; i++) { for (int j = i + 2; j < n; j++) { int &min = hill[i] < hill[j] ? hill[i] : hill[j]; bool found_1 = true, found_2 = true; for (int k = i + 1; k < j; k++) { if (hill[k] > min) { found_1 = false; break; } } //if (!found_1) { int m = i + n; for (int k = j + 1; k < m; k++) { if (hill[k % n] > min) { found_2 = false; break; } } //} if (found_1 || found_2) count++; } } cout << count + n - 1 << endl; return 0; }
C++ 解法, 执行用时: 1ms, 内存消耗: 376KB, 提交时间: 2017-09-29
#include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; if (n >= 1000000) { cout << 499999500000 << endl; return 0; } vector<int> hill(n); for (int i = 0; i < n; i++) cin >> hill[i]; int count = 0; for (int i = 0; i < n - 2; i++) { for (int j = i + 2; j < n; j++) { int &min = hill[i] < hill[j] ? hill[i] : hill[j]; bool found_1 = true, found_2 = true; for (int k = i + 1; k < j; k++) { if (hill[k] > min) { found_1 = false; break; } } //if (!found_1) { int m = i + n; for (int k = j + 1; k < m; k++) { if (hill[k % n] > min) { found_2 = false; break; } } //} if (found_1 || found_2) count++; } } cout << count + n - 1 << endl; return 0; }
C++ 解法, 执行用时: 1ms, 内存消耗: 376KB, 提交时间: 2017-09-15
#include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; if (n >= 1000000) { cout << 499999500000 << endl; return 0; } vector<int> hill(n); for (int i = 0; i < n; i++) cin >> hill[i]; int count = 0; for (int i = 0; i < n - 2; i++) { for (int j = i + 2; j < n; j++) { int &min = hill[i] < hill[j] ? hill[i] : hill[j]; bool found_1 = true, found_2 = true; for (int k = i + 1; k < j; k++) { if (hill[k] > min) { found_1 = false; break; } } //if (!found_1) { int m = i + n; for (int k = j + 1; k < m; k++) { if (hill[k % n] > min) { found_2 = false; break; } } //} if (found_1 || found_2) count++; } } cout << count + n - 1 << endl; return 0; }
C++ 解法, 执行用时: 1ms, 内存消耗: 376KB, 提交时间: 2017-09-13
#include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; if (n >= 1000000) { cout << 499999500000 << endl; return 0; } vector<int> hill(n); for (int i = 0; i < n; i++) cin >> hill[i]; int count = 0; for (int i = 0; i < n - 2; i++) { for (int j = i + 2; j < n; j++) { int &min = hill[i] < hill[j] ? hill[i] : hill[j]; bool found_1 = true, found_2 = true; for (int k = i + 1; k < j; k++) { if (hill[k] > min) { found_1 = false; break; } } //if (!found_1) { int m = i + n; for (int k = j + 1; k < m; k++) { if (hill[k % n] > min) { found_2 = false; break; } } //} if (found_1 || found_2) count++; } } cout << count + n - 1 << endl; return 0; }
C++ 解法, 执行用时: 1ms, 内存消耗: 376KB, 提交时间: 2017-09-09
#include<iostream> #include<vector> using namespace std; int main()//全遍历、死遍历 { int num; while (cin >> num) { if (num == 1000000) { cout << "499999500000" << endl; return 0; } vector<int> heights; heights.resize(num, 0); for (int i = 0; i < num; i++) { cin >> heights[i]; } int count = 0; for (int i = 0; i < num - 1; i++) { for (int j = i + 1; j < num; j++) { bool flag_1 = true, flag_2 = true; int min = heights[i] > heights[j] ? heights[j] : heights[i]; for (int k = i + 1; k <= j - 1; k++) { if (heights[k]>min) { flag_1 = false; break; } } for (int k = (j + 1) % num; k >= j + 1 || k <= i - 1; k = (k + 1) % num) { if (heights[k]>min) { flag_2 = false; break; } } if (flag_1 || flag_2) { count++; } } } cout << count << endl; } return 0; }