Codeforces#371

C. Hamburgers

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 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.

Output

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

Sample Input

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

1)二分查找:设定一个最大解,依次二分,每次二分时判断制作该数量的汉堡,所用价钱是否少于总的金钱数量。

# include<iostream>
# include<algorithm>
# include<cstdio>
# include<cstring>
using namespace std;
const long long  MAXN=1e13;
char ch[100+5];
long long  pr,l,r,mid;
int nb,ns,nc,pb,ps,pc,b=0,s=0,c=0;
bool judge(long long  mid)
{
    long long price=pb*max(0ll,mid*b-nb)+ps*max(0ll,mid*s-ns)+pc*max(0ll,mid*c-nc);
    return price<=pr;
}
int main()
{
    scanf("%s",&ch);
    scanf("%d%d%d%d%d%d%I64d",&nb,&ns,&nc,&pb,&ps,&pc,&pr);
    for(int i=0;i<strlen(ch);i++)
    {
        if(ch[i]=='B') b++;
        else if(ch[i]=='S') s++;
        else c++;
    }
    l=0;
    r=MAXN;
    while(l<=r)
    {
     mid=(l+r)/2;
     if(judge(mid)) l=mid+1;
     else r=mid-1;
    }
    printf("%I64d",l-1);
    return 0;
}


D. Vessels

There is a system of n vessels arranged one above the other as shown in the figure below. Assume that the vessels are numbered from 1 to n, in the order from the highest to the lowest, the volume of the i-th vessel is ai liters.

Initially, all the vessels are empty. In some vessels water is poured. All the water that overflows from the i-th vessel goes to the (i + 1)-th one. The liquid that overflows from the n-th vessel spills on the floor.

Your task is to simulate pouring water into the vessels. To do this, you will need to handle two types of queries:

Add xi liters of water to the pi-th vessel;
Print the number of liters of water in the ki-th vessel.
When you reply to the second request you can assume that all the water poured up to this point, has already overflown between the vessels.

Input

The first line contains integer n — the number of vessels (1 ≤ n ≤ 2·105). The second line contains n integers a1, a2, …, an — the vessels’ capacities (1 ≤ ai ≤ 109). The vessels’ capacities do not necessarily increase from the top vessels to the bottom ones (see the second sample). The third line contains integer m — the number of queries (1 ≤ m ≤ 2·105). Each of the next m lines contains the description of one query. The query of the first type is represented as “1 pi xi”, the query of the second type is represented as “2 ki” (1 ≤ pi ≤ n, 1 ≤ xi ≤ 109, 1 ≤ ki ≤ n).

Output

For each query, print on a single line the number of liters of water in the corresponding vessel.

Sample Input

Input
2
5 10
6
1 1 4
2 1
1 2 5
1 1 4
2 1
2 2
Output
4
5
8
Input
3
5 10 8
6
1 1 12
2 2
1 1 6
1 3 2
2 2
2 3
Output
7
10
5

分析

1)如果直接暴力求解,复杂度是O(n*n),会超时;

2)在暴力求解的基础上降低时间复杂度:如果一个Vessel已经满了,那么它对后续的操作就不再产生任何作用,于是一旦某个Vessel满了,就可以将它去除,只从未满的Vessel中搜搜,从而降低搜索的时间复杂度。

3)使用数据结构set,将已满的Vessel去除。

# include<iostream>
# include<algorithm>
# include<cstring>
# include<set>
using namespace std;
const int MAXN=2e5+5;
int a[MAXN],s[MAXN],full[MAXN];
set<int> st;
set<int>::iterator it;
int main()
{
    memset(s,0,sizeof(s));
    int n,m,op,p,x,k;
    cin>>n;
    for(int i=1; i<=n; i++)
    {
        cin>>a[i];
        st.insert(i);
    }
    cin>>m;
    while(m--)
    {
        cin>>op;
        int cnt=0;
        if(op==1)
        {
            cin>>p>>x;
            for(it=st.lower_bound(p); it!=st.end(); it++)
            {
                if(s[*it]+x<=a[*it])
                {
                    s[*it]+=x;
                    break;
                }
                else if(s[*it]<a[*it])
                {
                    x-=a[*it]-s[*it];
                    s[*it]=a[*it];
                    full[cnt++]=*it;
                }
            }
        }
        else
        {
            cin>>k;
            cout<<s[k]<<endl;
        }
        for(int i=0; i<cnt; i++) st.erase(full[i]);
    }
    return 0;
}

O(n*n)代码

# include<iostream>
# include<algorithm>
# include<cstring>
using namespace std;
const int MAXN=2e5+5;
int a[MAXN],s[MAXN];
int main()
{
    memset(s,0,sizeof(s));
    int n,m,op,p,x,k;
    cin>>n;
    for(int i=1; i<=n; i++)
        cin>>a[i];
    cin>>m;
    while(m--)
    {
        cin>>op;
        if(op==1)
        {
            cin>>p>>x;
            for(int j=p; j<=n; j++)
            {
                if(s[j]+x<=a[j])
                {
                    s[j]+=x;
                    break;
                }
                else if(s[j]<a[j])
                {
                    x-=a[j]-s[j];
                    s[j]=a[j];
                }
            }
        }
        else
        {
            cin>>k;
            cout<<s[k]<<endl;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值