codeforces 883C Downloading B++ 枚举,贪心
2017-11-05 11:07
363 查看
Downloading B++
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Only T milliseconds left before the start of well-known online programming contest Codehorses Round 2017.
Polycarp needs to download B++ compiler to take part in the contest. The size of the file is f bytes.
Polycarp's internet tariff allows to download data at the rate of one byte per t0 milliseconds.
This tariff is already prepaid, and its use does not incur any expense for Polycarp. In addition, the Internet service provider offers two additional packages:
download a1 bytes
at the rate of one byte per t1 milliseconds,
paying p1 burles
for the package;
download a2 bytes
at the rate of one byte per t2 milliseconds,
paying p2 burles
for the package.
Polycarp can buy any package many times. When buying a package, its price (p1 or p2)
is prepaid before usage. Once a package is bought it replaces the regular tariff until package data limit is completely used. After a package is consumed Polycarp can immediately buy a new package or switch to the regular tariff without loosing any time. While
a package is in use Polycarp can't buy another package or switch back to the regular internet tariff.
Find the minimum amount of money Polycarp has to spend to download an f bytes file no more than in T milliseconds.
Note that because of technical reasons Polycarp can download only integer number of bytes using regular tariff and both packages. I.e. in each of three downloading modes the number of downloaded bytes will be integer. It means that Polycarp can't download a
byte partially using the regular tariff or/and both packages.
Input
The first line contains three integer numbers f, T and t0 (1 ≤ f, T, t0 ≤ 107)
— size of the file to download (in bytes), maximal time to download the file (in milliseconds) and number of milliseconds to download one byte using the regular internet tariff.
The second line contains a description of the first additional package. The line contains three integer numbers a1, t1 and p1(1 ≤ a1, t1, p1 ≤ 107),
where a1 is
maximal sizes of downloaded data (in bytes), t1 is
time to download one byte (in milliseconds), p1 is
price of the package (in burles).
The third line contains a description of the second additional package. The line contains three integer numbers a2, t2 and p2(1 ≤ a2, t2, p2 ≤ 107),
where a2 is
maximal sizes of downloaded data (in bytes), t2 is
time to download one byte (in milliseconds), p2 is
price of the package (in burles).
Polycarp can buy any package many times. Once package is bought it replaces the regular tariff until package data limit is completely used. While a package is in use Polycarp can't buy another package or switch back to the regular internet tariff.
Output
Print the minimum amount of money that Polycarp needs to pay to download B++ compiler no more than in T milliseconds. If there is no
solution, print the only integer -1.
题解:
枚举时间最短的套餐使用个数,得到剩余F(剩余数据包)以及剩余T(剩余时间)
然后使用最少的第二个套餐的个数,使得下载F量的数据包满足时间不超过剩余T。(相当于一元不等式,直接解就ok了,有一些细节自己考虑一下)
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL inf = 1e18;
LL f,T,t0,a1,t1,p1,a2,t2,p2,resF,resT,ans = inf;
int main(){
cin>>f>>T>>t0>>a1>>t1>>p1>>a2>>t2>>p2;
if(t1>t2) swap(a1,a2),swap(p1,p2),swap(t1,t2);
for(int i = 0;(i-1)*a1 <= f;++i){
resF = f-min(i*a1,f),resT = T-min(i*a1,f)*t1;
if(resF == 0) {
if(resT < 0)break;
ans = min(ans,i*p1);break;
}
if(t0*resF>resT && t2*resF>resT) continue;
if(t0*resF <= resT) ans = min(ans,i*p1);
else{
LL x = (resT-t0*resF)/(t2-t0) + ((resT-t0*resF)%(t2-t0)==0?0:1);
//x = max(0ll,x);
LL pk = (x/a2)+(x%a2==0?0:1);
ans = min(ans,pk*p2+i*p1);
}
}
ans = max(ans,(LL)0);
printf("%lld\n",ans < inf?ans:-1);
return 0;
}
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Only T milliseconds left before the start of well-known online programming contest Codehorses Round 2017.
Polycarp needs to download B++ compiler to take part in the contest. The size of the file is f bytes.
Polycarp's internet tariff allows to download data at the rate of one byte per t0 milliseconds.
This tariff is already prepaid, and its use does not incur any expense for Polycarp. In addition, the Internet service provider offers two additional packages:
download a1 bytes
at the rate of one byte per t1 milliseconds,
paying p1 burles
for the package;
download a2 bytes
at the rate of one byte per t2 milliseconds,
paying p2 burles
for the package.
Polycarp can buy any package many times. When buying a package, its price (p1 or p2)
is prepaid before usage. Once a package is bought it replaces the regular tariff until package data limit is completely used. After a package is consumed Polycarp can immediately buy a new package or switch to the regular tariff without loosing any time. While
a package is in use Polycarp can't buy another package or switch back to the regular internet tariff.
Find the minimum amount of money Polycarp has to spend to download an f bytes file no more than in T milliseconds.
Note that because of technical reasons Polycarp can download only integer number of bytes using regular tariff and both packages. I.e. in each of three downloading modes the number of downloaded bytes will be integer. It means that Polycarp can't download a
byte partially using the regular tariff or/and both packages.
Input
The first line contains three integer numbers f, T and t0 (1 ≤ f, T, t0 ≤ 107)
— size of the file to download (in bytes), maximal time to download the file (in milliseconds) and number of milliseconds to download one byte using the regular internet tariff.
The second line contains a description of the first additional package. The line contains three integer numbers a1, t1 and p1(1 ≤ a1, t1, p1 ≤ 107),
where a1 is
maximal sizes of downloaded data (in bytes), t1 is
time to download one byte (in milliseconds), p1 is
price of the package (in burles).
The third line contains a description of the second additional package. The line contains three integer numbers a2, t2 and p2(1 ≤ a2, t2, p2 ≤ 107),
where a2 is
maximal sizes of downloaded data (in bytes), t2 is
time to download one byte (in milliseconds), p2 is
price of the package (in burles).
Polycarp can buy any package many times. Once package is bought it replaces the regular tariff until package data limit is completely used. While a package is in use Polycarp can't buy another package or switch back to the regular internet tariff.
Output
Print the minimum amount of money that Polycarp needs to pay to download B++ compiler no more than in T milliseconds. If there is no
solution, print the only integer -1.
题解:
枚举时间最短的套餐使用个数,得到剩余F(剩余数据包)以及剩余T(剩余时间)
然后使用最少的第二个套餐的个数,使得下载F量的数据包满足时间不超过剩余T。(相当于一元不等式,直接解就ok了,有一些细节自己考虑一下)
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL inf = 1e18;
LL f,T,t0,a1,t1,p1,a2,t2,p2,resF,resT,ans = inf;
int main(){
cin>>f>>T>>t0>>a1>>t1>>p1>>a2>>t2>>p2;
if(t1>t2) swap(a1,a2),swap(p1,p2),swap(t1,t2);
for(int i = 0;(i-1)*a1 <= f;++i){
resF = f-min(i*a1,f),resT = T-min(i*a1,f)*t1;
if(resF == 0) {
if(resT < 0)break;
ans = min(ans,i*p1);break;
}
if(t0*resF>resT && t2*resF>resT) continue;
if(t0*resF <= resT) ans = min(ans,i*p1);
else{
LL x = (resT-t0*resF)/(t2-t0) + ((resT-t0*resF)%(t2-t0)==0?0:1);
//x = max(0ll,x);
LL pk = (x/a2)+(x%a2==0?0:1);
ans = min(ans,pk*p2+i*p1);
}
}
ans = max(ans,(LL)0);
printf("%lld\n",ans < inf?ans:-1);
return 0;
}
相关文章推荐
- !codeforces 399C Cards-数论&贪心-(暴力枚举)
- codeforces 613B B. Skills(枚举+二分+贪心)
- 【codeforces 761C】Dasha and Password(贪心+枚举做法)
- codeforces 672C Recycling Bottles (枚举+贪心)
- Codeforces 672C Recycling Bottles【极限思维+贪心枚举】
- Codeforces 3 B. Lorry 暴力 排序 枚举 贪心
- Codeforces 913 A 模拟 B模拟 C 贪心+位运算 D 优先队列+枚举 (补题)
- codeforces 324# E. Anton and Ira (暴力枚举+贪心)
- Codeforces 509C. Sums of Digits 贪心枚举
- codeforces 509C C. Sums of Digits(数位dp+贪心+枚举)
- codeforces 600C. Make Palindrome(贪心)
- CodeForces - 797C Minimal string (贪心)
- Codeforces 373C Counting Kangaroos is Fun【贪心】
- Codeforces 749C Voting 贪心+模拟
- codeforces - 870problem - C. Maximum splitting【贪心】
- Codeforces 626D Jerry's Protest(暴力枚举+概率)
- 【CodeForces - 864】 A B C【模拟】 D【贪心+优先队列】
- POJ 2718 Smallest Difference(贪心 or next_permutation()暴力枚举)
- Codeforces 500B New Year Permutation Floyd算法+贪心
- Codeforces 265D Good Sequences 贪心+DP