POJ 3104 Drying
来源:互联网 发布:碳微球 知乎 编辑:程序博客网 时间:2024/06/10 00:01
It is very hard to wash and especially to dry clothes in winter. But Jane is a very smart girl. She is not afraid of this boring process. Jane has decided to use a radiator to make drying faster. But the radiator is small, so it can hold only one thing at a time.
Jane wants to perform drying in the minimal possible time. She asked you to write a program that will calculate the minimal time for a given set of clothes.
There are n clothes Jane has just washed. Each of them took ai water during washing. Every minute the amount of water contained in each thing decreases by one (of course, only if the thing is not completely dry yet). When amount of water contained becomes zero the cloth becomes dry and is ready to be packed.
Every minute Jane can select one thing to dry on the radiator. The radiator is very hot, so the amount of water in this thing decreases by k this minute (but not less than zero — if the thing contains less than k water, the resulting amount of water will be zero).
The task is to minimize the total time of drying by means of using the radiator effectively. The drying process ends when all the clothes are dry.
The first line contains a single integer n (1 ≤ n ≤ 100 000). The second line contains ai separated by spaces (1 ≤ ai ≤ 109). The third line contains k (1 ≤ k ≤ 109).
Output a single integer — the minimal possible number of minutes required to dry all clothes.
sample input #132 3 95sample input #232 3 65
sample output #13sample output #22
做了一星期二分,套路大概是摸清了。
这题需要推导一下计算公式:cnt >= (w[i] - x) / (k - 1),还有一个主意点——cnt要用long long!不然会爆炸。
#include <cmath>#include <cstdio>#include <iostream>#include <algorithm>#define eps 1e-7#define ll long longusing namespace std;ll n, k;ll w[100005];bool check(ll x){ll cnt = 0; //用int会原地旋转爆炸!!!for (int i = 0; i < n; i++){if (w[i] - x > 0){cnt += (w[i] - x) / (k - 1);if ((w[i] - x) % (k - 1))cnt++;}}if (cnt > x) return false;else return true;}int main(){cin >> n;for (int i = 0; i < n; i++)scanf_s("%lld", &w[i]);cin >> k;if (k == 1){ll max = w[0];for (int i = 1; i < n; i++)if (w[i] > max) max = w[i];cout << max << endl;}else{ll l = 0, r = 1e9, mid, ans;while (r >= l){mid = l + (r - l) / 2;if (check(mid)){r = mid - 1;ans = mid;}else l = mid + 1;}cout << ans << endl;}return 0;}
"There is but one rule: Hunt or be hunted."
- POJ 3104 Drying 二分
- 二分法 POJ-3104 Drying
- POJ--3104--Drying【二分】
- POJ 3104 Drying
- POJ-3104 Drying
- POJ 3104 Drying 二分
- poj 3104 Drying (二分)
- POJ-3104-Drying
- POJ 3104 Drying
- POJ 3104 Drying (二分)
- POJ 3104 Drying (二分)
- Poj 3104 Drying
- POJ 3104 Drying
- POJ 3104 Drying Clothes
- POJ 3104- Drying-二分
- POJ-3104Drying
- POJ 3104 Drying(二分)
- POJ 3104 Drying
- hdu 5583
- 每周Apache commons-SystemUtils
- 微信抢红包的架构设计
- 三、JVM之对象的创建
- 数据库事务(二)
- POJ 3104 Drying
- hdu 5572
- SPFA的两种优化SLF和LLL
- 煤球问题
- 数据库事务(三)
- hdu 5528
- vue-lazyload基础实例(基于vue2.0和vue-router2.0)
- hdu 5492
- 把base64图片数据转为本地图片的函数