2021.3.10--vj补题
B - Saving the City
cf--1443B
Bertown is a city with nn buildings in a straight line.
The city's security service discovered that some buildings were mined. A map was compiled, which is a string of length nn, where the ii-th character is "1" if there is a mine under the building number ii and "0" otherwise.
Bertown's best sapper knows how to activate mines so that the buildings above them are not damaged. When a mine under the building numbered xx is activated, it explodes and activates two adjacent mines under the buildings numbered x−1x−1 and x+1x+1 (if there were no mines under the building, then nothing happens). Thus, it is enough to activate any one mine on a continuous segment of mines to activate all the mines of this segment. For manual activation of one mine, the sapper takes aa coins. He can repeat this operation as many times as you want.
Also, a sapper can place a mine under a building if it wasn't there. For such an operation, he takes bb coins. He can also repeat this operation as many times as you want.
The sapper can carry out operations in any order.
You want to blow up all the mines in the city to make it safe. Find the minimum number of coins that the sapper will have to pay so that after his actions there are no mines left in the city.
Input
The first line contains one positive integer tt (1≤t≤1051≤t≤105) — the number of test cases. Then tt test cases follow.
Each test case begins with a line containing two integers aaand bb (1≤a,b≤10001≤a,b≤1000) — the cost of activating and placing one mine, respectively.
The next line contains a map of mines in the city — a string consisting of zeros and ones.
The sum of the string lengths for all test cases does not exceed 105105.
Output
For each test case, output one integer — the minimum number of coins that the sapper will have to pay.
Example
2 1 1 01000010 5 1 01101110
2 6
Note
In the second test case, if we place a mine under the fourth building and then activate it, then all mines on the field are activated. The cost of such operations is six, b=1b=1 coin for placing a mine and a=5a=5 coins for activating.
题意:给出长为n的字符串,由1和0组成,1代表有地雷,0代表无地雷,放置 1 即地雷需花费 b 个硬币,激活 1 需花费 a 个硬币,且可以把相邻的地雷激活不需花费硬币,求最小的花费数将字符串全变为0。
思路:至少要有一个引爆的费用,即第一段连续的地雷 1 需要 a硬币,sum=a,再看后面不相连的两段相连后的费用和不相连的费用的比较,取花费最小的,相连后即sum+=ct*b+a, 不相连的 sum+=a;
代码:
#include<bits/stdc++.h> using namespace std; int main() { int t; cin>>t; while(t--) { int a,b; cin>>a>>b; getchar(); string s; cin>>s; int x[100005]={0}; int m=s.size(),k=0; for(int i=0;i<m;i++) { if(s[i]=='1') { x[k++]=i; } } if(k==0)cout<<"0"<<endl; else{ int sum=a; for(int i=0;i<k-1;i++) { if(x[i+1]-x[i]<=1) continue; else sum+=min((x[i+1]-x[i]-1)*b,a); } cout<<sum<<endl; } } }
cf--1443C
Petya is preparing for his birthday. He decided that there would be n different dishes on the dinner table, numbered from 1 to n. Since Petya doesn't like to cook, he wants to order these dishes in restaurants.
Unfortunately, all dishes are prepared in different restaurants and therefore Petya needs to pick up his orders from n different places. To speed up this process, he wants to order courier delivery at some restaurants. Thus, for each dish, there are two options for Petya how he can get it:
- the dish will be delivered by a courier from the restaurant i, in this case the courier will arrive in aiminutes,
- Petya goes to the restaurant i on his own and picks up the dish, he will spend bi minutes on this.
Each restaurant has its own couriers and they start delivering the order at the moment Petya leaves the house. In other words, all couriers work in parallel. Petya must visit all restaurants in which he has not chosen delivery, he does this consistently.
For example, if Petya wants to order n=4 dishes and a=[3,7,4,5], and b=[2,1,2,4], then he can order delivery from the first and the fourth restaurant, and go to the second and third on your own. Then the courier of the first restaurant will bring the order in 3 minutes, the courier of the fourth restaurant will bring the order in 5minutes, and Petya will pick up the remaining dishes in 1+2=3 minutes. Thus, in 5 minutes all the dishes will be at Petya's house.
Find the minimum time after which all the dishes can be at Petya's home.
Input
The first line contains one positive integer t (1≤t≤2⋅105) — the number of test cases. Then t test cases follow.
Each test case begins with a line containing one integer n (1≤n≤2⋅105) — the number of dishes that Petya wants to order.
The second line of each test case contains n integers a1…an (1≤ai≤109) — the time of courier delivery of the dish with the number i.
The third line of each test case contains n integers b1…bn (1≤bi≤109) — the time during which Petya will pick up the dish with the number i.
The sum of n over all test cases does not exceed 2⋅105.
Output
For each test case output one integer — the minimum time after which all dishes can be at Petya's home.
Example
4 4 3 7 4 5 2 1 2 4 4 1 2 3 4 3 3 3 3 2 1 2 10 10 2 10 10 1 2
5 3 2 3
题意:
要点有n个菜,点外卖或者自己去拿,点外卖需时间ai,自己拿需 bi,求最短时间
思路:
二分法。 记下自己去拿菜的总时间,二分,如果 ai 时间<mid ,就点外卖,>mid,就自己去拿, 然后自己去拿的时间再和mid 比较
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=2e5+100; ll a[N],b[N]; ll n; bool show(ll x) { ll sum=0; for(int i=0;i<n;i++) { if(a[i]>x) { sum+=b[i]; } } return sum<=x; } int main() { ll t; cin>>t; while(t--) { cin>>n; for(int i=0;i<n;i++) { cin>>a[i]; } ll l=0,r=0; for(int j=0;j<n;j++) { cin>>b[j]; r+=b[j]; } while(l<r) { ll mid=(l+r)/2; if(show(mid)==1)r=mid; else l=mid+1; } cout<<r<<endl; } }
Petya is preparing for his birthday. He decided that there would be n different dishes on the dinner table, numbered from 1 to n. Since Petya doesn't like to cook, he wants to order these dishes in restaurants.
Unfortunately, all dishes are prepared in different restaurants and therefore Petya needs to pick up his orders from n different places. To speed up this process, he wants to order courier delivery at some restaurants. Thus, for each dish, there are two options for Petya how he can get it:
- the dish will be delivered by a courier from the restaurant i, in this case the courier will arrive in aiminutes,
- Petya goes to the restaurant i on his own and picks up the dish, he will spend bi minutes on this.
Each restaurant has its own couriers and they start delivering the order at the moment Petya leaves the house. In other words, all couriers work in parallel. Petya must visit all restaurants in which he has not chosen delivery, he does this consistently.
For example, if Petya wants to order n=4 dishes and a=[3,7,4,5], and b=[2,1,2,4], then he can order delivery from the first and the fourth restaurant, and go to the second and third on your own. Then the courier of the first restaurant will bring the order in 3 minutes, the courier of the fourth restaurant will bring the order in 5minutes, and Petya will pick up the remaining dishes in 1+2=3 minutes. Thus, in 5 minutes all the dishes will be at Petya's house.
Find the minimum time after which all the dishes can be at Petya's home.
Input
The first line contains one positive integer t (1≤t≤2⋅105) — the number of test cases. Then t test cases follow.
Each test case begins with a line containing one integer n (1≤n≤2⋅105) — the number of dishes that Petya wants to order.
The second line of each test case contains n integers a1…an (1≤ai≤109) — the time of courier delivery of the dish with the number i.
The third line of each test case contains n integers b1…bn (1≤bi≤109) — the time during which Petya will pick up the dish with the number i.
The sum of n over all test cases does not exceed 2⋅105.
Output
For each test case output one integer — the minimum time after which all dishes can be at Petya's home.
Example
4 4 3 7 4 5 2 1 2 4 4 1 2 3 4 3 3 3 3 2 1 2 10 10 2 10 10 1 2
5 3 2 3

浙公网安备 33010602011771号