二分专题 codeforces 「343 C」 &「371 C」 题解

转载请注明出处:http://blog.csdn.net/jiangshibiao/article/details/21536359

【序言】向codeforces进发了!这里的题解我会很简略。这都是思维题。

【371C】

C. Hamburgers
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

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 nbnsnc (1 ≤ nb, ns, nc ≤ 100) — the number of the pieces of bread, sausage and cheese on Polycarpus' kitchen. The third line contains three integers pbpspc (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 cincout streams or the%I64d specifier.

Output

Print the maximum number of hamburgers Polycarpus can make. If he can't make any hamburger, print 0.

Sample test(s)
input
BBBSSC
6 4 1
1 2 3
4
output
2
input
BBC
1 10 1
1 10 1
21
output
7
input
BSC
1 1 1
1 1 3
1000000000000
output
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】

C. Read Time
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 h1h2...hn have been read at the beginning of the operation.

Mike needs to read the data on m distinct tracks with numbers p1p2...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.

Input

The first line of the input contains two space-separated integers nm (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 ≤ 1010hi < hi + 1) — the initial positions of the heads. The third line contains m distinct integers pi in ascending order (1 ≤ pi ≤ 1010pi < 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 cincout streams or the%I64d specifier.

Output

Print a single number — the minimum time required, in seconds, to read all the needed tracks.

Sample test(s)
input
3 4
2 5 6
1 3 6 8
output
2
input
3 3
1 2 3
1 2 3
output
0
input
1 2
165
142 200
output
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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值