POJ 3258:River Hopscotch & POJ 3273:Monthly Expense (二分)
来源:互联网 发布:北京网络推广培训 编辑:程序博客网 时间:2024/06/05 04:23
传送门:
POJ 3258
POJ 3273
两道题思路差不了多少,找到要二分什么,怎么check,问题基本就解决了。
River Hopscotch:
#include <cmath>#include <cstdio>#include <iostream>#include <algorithm>#define eps 1e-7using namespace std;int l, n, m;int pos[50005];bool check(int x){int pre = 0, curr, cnt = 0;for (int i = 1; i < n; i++){if (pos[i] - pos[pre] < x)cnt++;else pre = i;if (cnt > m)return false;}return true;}int main(){cin >> l >> n >> m;pos[0] = 0;n++;for (int i = 1; i < n; i++)scanf_s("%d", &pos[i]);pos[n] = l;n++;sort(pos, pos + n);int lb = 0, rb = l + 1, mid;while (rb - lb > 1){mid = lb + (rb - lb) / 2;if (check(mid))lb = mid;else rb = mid;}cout << lb << endl;return 0;}
Monthly Expense:
#include <cmath>#include <cstdio>#include <iostream>#include <algorithm>#define eps 1e-7using namespace std;int n, m;int money[100005];bool check(int x){int cnt = 0, beg = 0, sum = money[0];for (int i = 0; i < n; i++){if(beg != i)sum += money[i];if (sum > x){cnt++;beg = i;if (money[beg] > x)return false;else sum = money[i];}}cnt++;if (cnt > m)return false;else return true;}int main(){cin >> n >> m;for (int i = 0; i < n; i++)scanf_s("%d", &money[i]);int l = 0, r = 1e9 + 1, mid;while (r - l > 1){mid = l + (r - l) / 2;if (check(mid))r = mid;else l = mid;}cout << r << endl;return 0;}
"So be it."
0 0
- POJ 3258:River Hopscotch & POJ 3273:Monthly Expense (二分)
- POJ 3273 Monthly Expense(二分枚举)
- POJ 3273 - Monthly Expense(二分)
- POJ 3273 Monthly Expense(二分查找)
- poj 3273 Monthly Expense(二分+贪心)
- POJ 3273 Monthly Expense (二分搜索)
- POJ 3273 Monthly Expense(二分)
- poj-3273 Monthly Expense (二分)
- POJ 3273 Monthly Expense (二分搜索)
- POJ 3273-Monthly Expense(二分+贪心)
- POJ 3273 Monthly Expense (二分)
- 【POJ】3273 - Monthly Expense(二分)
- poj 3273 Monthly Expense(二分)
- Poj 3273 Monthly Expense(二分)
- poj 3273 二分 Monthly Expense
- poj 3273 二分 monthly expense
- poj-3273-Monthly Expense-二分
- POJ 3273 Monthly Expense(二分)
- 关于springMVC重定向问题
- 塔防大战unity5.5版本系列(part10)
- 深入理解javascript原型和闭包(7)——原型的灵活性
- Java多线程系列--“基础篇”05之 线程等待与唤醒
- 什么是堡垒机
- POJ 3258:River Hopscotch & POJ 3273:Monthly Expense (二分)
- Linearlayout线性布局中layout_weight权重分配不起作用?
- codevs1380 没有上司的舞会 树上dp
- 理解ThreadLocal
- 数据库回顾
- 第43天(就业班) jQuery-AJAX、mysql的优化
- JavaScript简介
- Java设计模式之单例模式
- 吹蜡烛问题