# 11044: 【原1044】二哥打游戏

### 题目描述

author: zyz 原OJ链接：https://acm.sjtu.edu.cn/OnlineJudge-old/problem/1044

## Sample Input

``````3
farmer 5 0 1
footman 16 0 2
tank 10 35 3
5
food 5
food 15
wood 40
food 20
wood 16
``````

## Sample Output

``````#1: a farmer was created.
#2: a footman was created.
#4: a tank was created.
food: 0, wood: 17
``````

## FineArtz's solution

``````/* 二哥打游戏 */
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

class Unit{
public:
char name[25];
int food = 0, wood = 0, priority = 0;

bool operator <(const Unit &u){
return priority > u.priority;
}
};

int main(){
Unit a[15];
int k, n;
cin >> k;
for (int i = 1; i <= k; ++i){
cin >> a[i].name >> a[i].food >> a[i].wood >> a[i].priority;
}
sort(a + 1, a + k + 1);
//    for (int i = 1; i <= k; ++i)
//        cout << a[i].name << ' ' << a[i].food << ' ' << a[i].wood << ' ' << a[i].priority << endl;
int wood = 0, food = 0, bonus = 0;
int wq[10005], fq[10005], wfront = 0, wrear = 0, ffront = 0, frear = 0;
cin >> n;
for (int t = 1; t <= n; ++t){
char source[5];
int amount;
cin >> source >> amount;
amount += bonus;
if (source[0] == 'f'){
fq[frear++] = amount;
food += amount;
}
else if (source[0] == 'w'){
wq[wrear++] = amount;
wood += amount;
}
for (int i = 1; i <= k; ++i){
if (food >= a[i].food && wood >= a[i].wood){
cout << "#" << t << ": a " << a[i].name << " was created." << '\n';
int f = 0, w = 0;
while (f < a[i].food){
f += fq[ffront++];
}
food -= f;
while (w < a[i].wood){
w += wq[wfront++];
}
wood -= w;
if (strcmp(a[i].name, "farmer") == 0)
++bonus;
}
}
}
cout << "food: " << food << ", wood: " << wood << '\n';
return 0;
}
``````

## yyong119's solution

``````#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int SIZE = 10010;

struct Unit {
char name[25];
int food, wood, level;
} units[11];
int unitNumber, timeNumber;
int food[SIZE], wood[SIZE], farmer = 0, resource, foodStart = 0, woodStart = 0, foodEnd = 0, woodEnd = 0, totalFood = 0, totalWood = 0;
char type[5];

bool cmp(const Unit &unit1, const Unit &unit2) {
return unit1.level < unit2.level;
}

int main() {
scanf("%d", &unitNumber);
for(int i = 0; i < unitNumber; ++i)
scanf("%s%d%d%d", &units[i].name, &units[i].food, &units[i].wood, &units[i].level);
sort(units, units + unitNumber, cmp);
scanf("%d", &timeNumber);
for(int i = 1; i <= timeNumber; ++i) {
scanf("%s%d", &type, &resource);
resource += farmer;
if(type[0] == 'f'){
totalFood += resource;
food[foodEnd++] = resource;
}
else if(type[0] == 'w'){
totalWood += resource;
wood[woodEnd++] = resource;
}
for(int j = unitNumber - 1; j >= 0; --j)
if(units[j].food <= totalFood && units[j].wood <= totalWood){
for(int remainFood = units[j].food; remainFood > 0; remainFood -= food[foodStart], totalFood -= food[foodStart++]);
for(int remainWood = units[j].wood; remainWood > 0; remainWood -= wood[woodStart], totalWood -= wood[woodStart++]);
if(!strcmp(units[j].name, "farmer"))
++farmer;
printf("#%d: a %s was created.\n", i, units[j].name);
break;
}
}
printf("food: %d, wood: %d\n", totalFood, totalWood);
return 0
}
``````