目录
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.
- 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.
- 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.
- 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++代码实现了一个程序,主要用于模拟一系列操作。以下是对代码功能的大致介绍:
readint()
函数用于读取输入中的整数,包括对负数的处理。定义了一个结构体
node
,其中包含两个成员变量id
和x
。结构体实现了一个比较操作符,用于在排序时按照x
的值进行排序,如果x
相同,则按照id
的值进行排序。声明了一些全局变量,包括整数
n
和m
,以及长整型数组a
和c
,还有结构体数组k
。在
main
函数中,首先读取输入的n
和m
。使用一个循环读取数组
a
和给结构体数组k
赋值。对结构体数组
k
进行排序,排序的依据是x
的值。接下来的循环处理了一系列操作,每次操作包括读取两个整数
x
和y
,然后进行一些计算,最后输出结果。在操作中,首先根据
y
和数组a
中的值进行一些计算,然后根据排序后的结构体数组k
进行进一步的操作。输出结果,如果
y
不为零则输出 0,否则输出计算得到的结果。总体而言,这段代码的作用是模拟了一系列对数组和结构体数组的操作,其中涉及了输入输出、排序和条件判断等基本编程概念。