转载请注明出处:http://blog.csdn.net/jiangshibiao/article/details/21536359
【序言】向codeforces进发了!这里的题解我会很简略。这都是思维题。
【371C】
Polycarpus loves hamburgers very much. He especially adores the hamburgers he makes with his own hands. Polycarpus thinks that there are only three decent ingredients to make hamburgers from: a bread, sausage and cheese. He writes down the recipe of his favorite "Le Hamburger de Polycarpus" as a string of letters 'B' (bread), 'S' (sausage) и 'C' (cheese). The ingredients in the recipe go from bottom to top, for example, recipe "ВSCBS" represents the hamburger where the ingredients go from bottom to top as bread, sausage, cheese, bread and sausage again.
Polycarpus has nb pieces of bread, ns pieces of sausage and nc pieces of cheese in the kitchen. Besides, the shop nearby has all three ingredients, the prices are pb rubles for a piece of bread, ps for a piece of sausage and pc for a piece of cheese.
Polycarpus has r rubles and he is ready to shop on them. What maximum number of hamburgers can he cook? You can assume that Polycarpus cannot break or slice any of the pieces of bread, sausage or cheese. Besides, the shop has an unlimited number of pieces of each ingredient.
The first line of the input contains a non-empty string that describes the recipe of "Le Hamburger de Polycarpus". The length of the string doesn't exceed 100, the string contains only letters 'B' (uppercase English B), 'S' (uppercase English S) and 'C' (uppercase English C).
The second line contains three integers nb, ns, nc (1 ≤ nb, ns, nc ≤ 100) — the number of the pieces of bread, sausage and cheese on Polycarpus' kitchen. The third line contains three integers pb, ps, pc (1 ≤ pb, ps, pc ≤ 100) — the price of one piece of bread, sausage and cheese in the shop. Finally, the fourth line contains integer r (1 ≤ r ≤ 1012) — the number of rubles Polycarpus has.
Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the%I64d specifier.
Print the maximum number of hamburgers Polycarpus can make. If he can't make any hamburger, print 0.
BBBSSC 6 4 1 1 2 3 4
2
BBC 1 10 1 1 10 1 21
7
BSC 1 1 1 1 1 3 1000000000000
200000000001
【代码】
#include<cstdio>
#include<string>
using namespace std;
typedef long long big;
big have[3],cost[3],num[3],money,len,Max,ans;int i;
char s[101];
big min(big a,big b){return a<b?a:b;}
bool check(big k)
{
big now=money;
for (int i=0;i<3;i++)
if (have[i]<k*num[i])
{
now-=(k*num[i]-have[i])*cost[i];
if (now<0) return false;
}
return true;
}
big erfen(big l,big r)
{
if (l==r) return l;
big mid=(l+r)/2+1;
if (check(mid))
return erfen(mid,r);
return erfen(l,mid-1);
}
int main()
{
scanf("%s",s);len=strlen(s);
for (i=0;i<len;i++)
switch (s[i])
{
case 'B':{num[0]++;break;}
case 'S':{num[1]++;break;}
case 'C':{num[2]++;break;}
}
scanf("%I64d%I64d%I64d",&have[0],&have[1],&have[2]);
scanf("%I64d%I64d%I64d",&cost[0],&cost[1],&cost[2]);
scanf("%I64d",&money);
Max=-1;
for (i=0;i<3;i++)
if (num[i]>0)
{
if (Max==-1) Max=(have[i]+money/cost[i])/num[i];
else Max=min(Max,(have[i]+money/cost[i])/num[i]);
}
if (Max==-1) Max=0;
ans=erfen(0,Max);
printf("%I64d",ans);
return 0;
}
【343C】
Mad scientist Mike does not use slow hard disks. His modification of a hard drive has not one, but n different heads that can read data in parallel.
When viewed from the side, Mike's hard drive is an endless array of tracks. The tracks of the array are numbered from left to right with integers, starting with 1. In the initial state the i-th reading head is above the track number hi. For each of the reading heads, the hard drive's firmware can move the head exactly one track to the right or to the left, or leave it on the current track. During the operation each head's movement does not affect the movement of the other heads: the heads can change their relative order; there can be multiple reading heads above any of the tracks. A track is considered read if at least one head has visited this track. In particular, all of the tracks numbered h1, h2, ..., hn have been read at the beginning of the operation.
Mike needs to read the data on m distinct tracks with numbers p1, p2, ..., pm. Determine the minimum time the hard drive firmware needs to move the heads and read all the given tracks. Note that an arbitrary number of other tracks can also be read.
The first line of the input contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of disk heads and the number of tracks to read, accordingly. The second line contains n distinct integers hi in ascending order (1 ≤ hi ≤ 1010, hi < hi + 1) — the initial positions of the heads. The third line contains m distinct integers pi in ascending order (1 ≤ pi ≤ 1010, pi < pi + 1) - the numbers of tracks to read.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is recommended to use the cin, cout streams or the%I64d specifier.
Print a single number — the minimum time required, in seconds, to read all the needed tracks.
3 4 2 5 6 1 3 6 8
2
3 3 1 2 3 1 2 3
0
1 2 165 142 200
81
【分析】同样二分。在贪心的时候注意一下,那个max的意思是:先向左走再尽力向右;在向左的保证下先尽量向右。
【代码】
#include<cstdio>
using namespace std;
typedef long long big;
const int maxn=100001;
big ans,INF,a[maxn],b[maxn],now;
big max(big a,big b){return a>b?a:b;}
big abs(big a){return a>0?a:-a;}
int n,m,i;
bool check(big t)
{
int now=1;big went;
for (int i=1;i<=n;i++)
{
if (abs(a[i]-b[now])>t) continue;
if (b[now]<a[i]) went=max(a[i]+t-(a[i]-b[now])*2,a[i]+(t-(a[i]-b[now]))/2);
else went=a[i]+t;
while (went>=b[now]&&now<=m) now++;
if (now>m) return true;
}
if (now<=m) return false;
}
big erfen(big l,big r)
{
if (l==r) return l;
big mid=(l+r)/2;
if (check(mid))
return erfen(l,mid);
return erfen(mid+1,r);
}
int main()
{
scanf("%d%d",&n,&m);
for (i=1;i<=n;i++) scanf("%I64d",&a[i]);
now=a[1];
for (i=1;i<=m;i++)
{
scanf("%I64d",&b[i]);
INF+=abs(now-b[i]);
now=b[i];
}
ans=erfen(0,INF);
printf("%I64d",ans);
return 0;
}