POJ 3104 -- Drying (最大化最小值)
晾衣服:n件衣服各含a_i水分,自然干一分钟一单位,放烘干机一分钟k单位,一次只能晒一件。求最短时间。
取C(mid) := 能在mid分钟内处理完,然后二分即可。
这里有两个很好玩的陷阱
①每分钟烘干k单位的水,于是我就想当然地除k向上取整了((a_i – mid) / k)。其实应该除以k-1,列个详细的算式:
设需要用x分钟的机器,那么自然风干需要mid – x分钟,x和mid需要满足:
k*x + (mid – x) >= a_i,即 x >= (a_i – mid) / (k – 1)。
②当k=1的时候,很显然会发生除零错误,需要特殊处理。
http://blog.csdn.net/u014688145/article/details/73610408
k⋅x+(mid−x)≥ai
x≥⌈ai−midk−1⌉
http://blog.csdn.net/murmured/article/details/30526489
Also check code at http://blog.csdn.net/xiaozhuaixifu/article/details/11714641
Read full article from 3104 -- Drying
Description
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.
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.
Input
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
Output a single integer — the minimal possible number of minutes required to dry all clothes.
Sample Input
sample input #1 3 2 3 9 5 sample input #2 3 2 3 6 5
Sample Output
sample output #1 3 sample output #2 2http://www.hankcs.com/program/cpp/poj-3104-drying.html
晾衣服:n件衣服各含a_i水分,自然干一分钟一单位,放烘干机一分钟k单位,一次只能晒一件。求最短时间。
取C(mid) := 能在mid分钟内处理完,然后二分即可。
这里有两个很好玩的陷阱
①每分钟烘干k单位的水,于是我就想当然地除k向上取整了((a_i – mid) / k)。其实应该除以k-1,列个详细的算式:
设需要用x分钟的机器,那么自然风干需要mid – x分钟,x和mid需要满足:
k*x + (mid – x) >= a_i,即 x >= (a_i – mid) / (k – 1)。
②当k=1的时候,很显然会发生除零错误,需要特殊处理。
http://blog.csdn.net/u014688145/article/details/73610408
采用模拟,专注用最优策略,每次选择最湿的衣服用散热器,其余衣服减一,用优先队列保持当前最湿。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
TLE了,看来一定有数学性质来简化这过程。刚才的过程考虑了最优策略,而且程序的执行很navie,一步步走,或者你可以认为是单线程的(这也是为什么模拟慢的原因)。
所以优化的思路是能否利用一些数学性质来加快运算速度?脑子里蹦出来一个并发的概念,但必须符合并发才行啊,在哪里?此处只有一个地方有并发,每分钟自然风干时,所有的衣服都会自减一,思路就在这。而用散热器风干时,每个时刻只能操作一次,这意味着需要用散热器风干时,它就必须单独算一次。(计数是串行的)
所以优化思路是:减去所有衣服自然风干的状态(意味着模拟一下子被简化),计算剩余湿度需要散热器的次数。(而一件衣服的计数也可以在O(1) 的操作内得到)
得:
有趣的是,这道题如果不去假设某个mid存在,直接采用模拟很难在有限的时间内解决。而上述公式告诉我们一个事实,mid与x之间存在着映射关系,且符合有序,所以直接用二分搜索解空间就好。
static int[] clothes;
static int K;
static int N;
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
N = in.nextInt();
clothes = new int[N];
int max = 0;
for (int i = 0; i < N; ++i){
clothes[i] = in.nextInt();
max = Math.max(max, clothes[i]);
}
K = in.nextInt();
if (K == 1){
System.out.println(max);
return;
}
int lf = 0;
int rt = max;
while (lf < rt){
int mid = lf + (rt - lf) / 2;
if (!valid(mid)) lf = mid + 1;
else rt = mid;
}
System.out.println(lf);
}
public static boolean valid(int m){
for (int i = 0, times = 0; i < N; ++i){
int more = clothes[i] - m;
if (more >= 0){
times += (int) Math.ceil(more / (1.0 * (K - 1)));
if (times > m) return false;
}
}
return true;
}
http://blog.csdn.net/murmured/article/details/30526489
注意烘干时候没有自然风干。
可以理解为烘干时每分钟掉(k-1)的水。
这样每件衣服每分钟就都掉1水了。
int
n, a[MAX_A], k;
bool
C(
int
mid)
{
unsigned
long
long
minutes(0);
for
(
int
i = 0; i < n; ++i)
{
int
more = a[i] - mid;
if
(more > 0)
{
minutes += (more + k - 1) / k;
// ceil
if
(minutes > mid)
{
return
false
;
}
}
}
return
true
;
}
///////////////////////////SubMain//////////////////////////////////
int
main(
int
argc,
char
*argv[])
{
#ifndef ONLINE_JUDGE
freopen
(
"in.txt"
,
"r"
, stdin);
freopen
(
"out.txt"
,
"w"
, stdout);
#endif
std::ios::sync_with_stdio(
false
);
std::cin.tie(0);
cin >> n;
for
(
int
i = 0; i < n; ++i)
{
cin >> a[i];
}
cin >> k;
--k;
// x >= (a_i - mid) / (k - 1)
if
(k == 0)
{
cout << *max_element(a, a + n) << endl;
}
else
{
int
lb = *min_element(a, a + n) / k, ub = *max_element(a, a + n);
while
(ub - lb > 1)
{
int
mid = (lb + ub) >> 1;
if
(C(mid))
{
ub = mid;
}
else
{
lb = mid;
}
}
cout << ub << endl;
}
#ifndef ONLINE_JUDGE
fclose
(stdin);
fclose
(stdout);
system
(
"out.txt"
);
#endif
return
0;
}
Also check code at http://blog.csdn.net/xiaozhuaixifu/article/details/11714641
Read full article from 3104 -- Drying