B. Lunar New Year and Food Ordering

目录

1.Problem

2.Input

3.Output

4.Examples

4.1input

4.2output

5.Code

6.Conclusion


1.Problem

Lunar New Year is approaching, and Bob is planning to go for a famous restaurant — "Alice's".

The restaurant "Alice's" serves nn kinds of food. The cost for the ii-th kind is always cici. Initially, the restaurant has enough ingredients for serving exactly aiai dishes of the ii-th kind. In the New Year's Eve, mm customers will visit Alice's one after another and the jj-th customer will order djdj dishes of the tjtj-th kind of food. The (i+1)(i+1)-st customer will only come after the ii-th customer is completely served.

Suppose there are riri dishes of the ii-th kind remaining (initially ri=airi=ai). When a customer orders 11 dish of the ii-th kind, the following principles will be processed.

  1. If ri>0ri>0, the customer will be served exactly 11 dish of the ii-th kind. The cost for the dish is cici. Meanwhile, riri will be reduced by 11.
  2. Otherwise, the customer will be served 11 dish of the cheapest available kind of food if there are any. If there are multiple cheapest kinds of food, the one with the smallest index among the cheapest will be served. The cost will be the cost for the dish served and the remain for the corresponding dish will be reduced by 11.
  3. If there are no more dishes at all, the customer will leave angrily. Therefore, no matter how many dishes are served previously, the cost for the customer is 00.

If the customer doesn't leave after the djdj dishes are served, the cost for the customer will be the sum of the cost for these djdj dishes.

Please determine the total cost for each of the mm customers.

2.Input

The first line contains two integers nn and mm (1≤n,m≤1051≤n,m≤105), representing the number of different kinds of food and the number of customers, respectively.

The second line contains nn positive integers a1,a2,…,ana1,a2,…,an (1≤ai≤1071≤ai≤107), where aiai denotes the initial remain of the ii-th kind of dishes.

The third line contains nn positive integers c1,c2,…,cnc1,c2,…,cn (1≤ci≤1061≤ci≤106), where cici denotes the cost of one dish of the ii-th kind.

The following mm lines describe the orders of the mm customers respectively. The jj-th line contains two positive integers tjtj and djdj (1≤tj≤n1≤tj≤n, 1≤dj≤1071≤dj≤107), representing the kind of food and the number of dishes the jj-th customer orders, respectively.

3.Output

Print mm lines. In the jj-th line print the cost for the jj-th customer.

4.Examples

4.1input

8 5
8 6 2 1 4 5 7 5
6 3 3 2 6 2 3 2
2 8
1 4
4 7
3 4
6 10

4.2output

22
24
14
10
39

5.Code

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

int readint(){
    int x=0,f=1; 
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-') f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}

struct node{
    ll id,x;
    bool operator<(const node c)const{
        if(x==c.x) return id<c.id;
        return x<c.x;
    }
} k[100005];

int n, m;
ll ans;
ll a[100005], c[100005];

int main(){
    n = readint(); 
    m = readint();
    
    for(int i=1; i<=n; i++) {
        a[i] = readint();
        k[i].id = i;
    }
    
    for(int i=1; i<=n; i++) {
        k[i].x = c[i] = readint();
    }
    
    sort(k+1, k+n+1);
    
    int now = 1; 
    ll x, y;
    
    for(int i=1; i<=m; i++){
        x = readint(); 
        y = readint(); 
        ans = 0;
        
        if(y <= a[x]) {
            a[x] -= y;
            ans = y * c[x];
            y = 0;
        }
        else {
            ans = a[x] * c[x];
            y -= a[x];
            a[x] = 0;
        }
        
        if(y > a[x]){
            while(y && now <= n){
                if(y <= a[k[now].id]) {
                    a[k[now].id] -= y;
                    ans += y * c[k[now].id];
                    y = 0;
                }
                else {
                    ans += a[k[now].id] * c[k[now].id];
                    y -= a[k[now].id];
                    a[k[now].id] = 0;
                }
                
                while(!a[k[now].id] && now <= n) now++;
            }
        }
        
        while(!a[k[now].id] && now <= n) now++;
        
        if(y) 
            printf("0\n");
        else 
            printf("%lld\n", ans);
    }
    
    return 0;
}

6.Conclusion

这段C++代码实现了一个程序,主要用于模拟一系列操作。以下是对代码功能的大致介绍:

  1. readint() 函数用于读取输入中的整数,包括对负数的处理。

  2. 定义了一个结构体 node,其中包含两个成员变量 idx。结构体实现了一个比较操作符,用于在排序时按照 x 的值进行排序,如果 x 相同,则按照 id 的值进行排序。

  3. 声明了一些全局变量,包括整数 nm,以及长整型数组 ac,还有结构体数组 k

  4. main 函数中,首先读取输入的 nm

  5. 使用一个循环读取数组 a 和给结构体数组 k 赋值。

  6. 对结构体数组 k 进行排序,排序的依据是 x 的值。

  7. 接下来的循环处理了一系列操作,每次操作包括读取两个整数 xy,然后进行一些计算,最后输出结果。

  8. 在操作中,首先根据 y 和数组 a 中的值进行一些计算,然后根据排序后的结构体数组 k 进行进一步的操作。

  9. 输出结果,如果 y 不为零则输出 0,否则输出计算得到的结果。

总体而言,这段代码的作用是模拟了一系列对数组和结构体数组的操作,其中涉及了输入输出、排序和条件判断等基本编程概念。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

向阳而生__

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值