Master Di plans to take his girlfriend for a travel by bike. Their journey, which can be seen as a line segment of length L, is a road of swamps and flats. In the swamp, it takes A point strengths per meter for Master Di to ride; In the flats, Master Di will regain B point strengths per meter when riding. Master Di wonders:In the beginning, he needs to prepare how much minimum strengths. (Except riding all the time,Master Di has no other choice)
Input
In the first line there is an integer t (1≤t≤501≤t≤50), indicating the number of test cases.
For each test case:
The first line contains four integers, n, A, B, L.
Next n lines, each line contains two integers: Li,RiLi,Ri, which represents the interval [Li,Ri][Li,Ri] is swamp.
1≤n≤100,1≤L≤105,1≤A≤10,1≤B≤10,1≤Li<Ri≤L1≤n≤100,1≤L≤105,1≤A≤10,1≤B≤10,1≤Li<Ri≤L.
Make sure intervals are not overlapped which means Ri<Li+1Ri<Li+1 for each i (1≤i<n1≤i<n).
Others are all flats except the swamps.
Output
For each text case:
Please output “Case #k: answer”(without quotes) one line, where k means the case number counting from 1, and the answer is his minimum strengths in the beginning.
Sample Input
1
2 2 2 5
1 2
3 4
Sample Output
Case #1: 0
题意:走一段有沼泽有平路的路,走沼泽地时每米消耗a点体力,走平路时每秒恢复b点体力,求最开始需要最少的体力。
可以直接模拟这个状态就可以做出来,也有的大佬用二分做,我当时是直接进行的模拟。
简单模拟:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
using namespace std;
typedef long long ll;
struct ss{
int l;
int r;
}s[200];
int main()
{
int n,a,b,l,t;
cin>>t;
int k=0;
while(t--){
k++;
cin>>n>>a>>b>>l;
for(int i=0;i<n;i++){
scanf("%d %d",&s[i].l,&s[i].r);
}
int ans=0;
int temp=0;
int c=0;
for(int i=0;i<n;i++){
c+=b*(s[i].l-temp);
if(c<(s[i].r-s[i].l)*a){
ans+=((s[i].r-s[i].l)*a-c);
c=0;
}
else{
c-=(s[i].r-s[i].l)*a;
}
temp=s[i].r;
}
printf("Case #%d: %d\n",k,ans);
}
return 0;
}
二分:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
const int maxn=110;
struct Node{
int l,r;
}A[maxn];
int a,b;
bool judge(int mid,int n){
int ans=mid,l=0;
for(int i=0;i<n;++i){
ans+=b*(A[i].l-l);
ans-=a*(A[i].r-A[i].l);
l=A[i].r;
if(ans<0)return false;
}
return true;
}
int main()
{
int t,i,j,k=1,l,n;
scanf("%d",&t);
while(t--){
scanf("%d%d%d%d",&n,&a,&b,&l);
int l=0,r=0;
for(i=0;i<n;++i){
scanf("%d%d",&A[i].l,&A[i].r);
r+=(A[i].r-A[i].l)*a;
}
printf("Case #%d: ",k++);
int ans=r;
while(l<=r){
int mid=(l+r)>>1;
if(judge(mid,n)){
ans=mid;
r=mid-1;
}
else {
l=mid+1;
}
}
printf("%d\n",ans);
}
return 0;
}