长城守卫 Beijing guards CERC 2004 LA3177 解题总结

来源:互联网 发布:视频剪辑合成配音软件 编辑:程序博客网 时间:2024/05/16 06:39

在最小值,最大值的问题 或者没有办法确定答案时候,  可以使用二分答案法.

#include <cstdio>#include <iostream>#include <fstream>using namespace std;int main(){fstream f("E:\\text.txt");int n;int gift[100];bool used[100];memset(used, 0, sizeof(used));int ans = 0;f >> n;for (int i = 0; i < n; i++){f >> gift[i];}ans = gift[0];for (int i = 0; i < gift[0]; i++) used[i] = true;for (int i = 1; i < n; i++){int g = gift[i];int j = 0;while (1){if (used[j]) used[j] = false;else {g--; used[j] = true;if (g == 0) break;}if (j+1>ans) ans = j+1;j++;}}int last=0;for (int i = gift[0]; i < ans; i++){if (!used[i]) last++;}for (int i = 0; i < gift[0]; i++){if (used[i]){if (last>0)last--;else ans++;}}cout << ans;}


0 0
原创粉丝点击