题目描述很长,我这里就省略了,直接上代码:
#include <bits/stdc++.h>
using namespace std;
void output(vector< pair<int, int> > v){
cout << v[0].first << endl;
cout << v.size()-1 << " ";
for(int i = 1; i < v.size(); i++){
cout << v[i].first << " ";
}
cout << endl;
}
//炉石传说
int main(){
int n;
cin >> n;
vector< pair<int, int> > a;
vector< pair<int, int> > b;
a.push_back(pair<int, int>(30, 0));
b.push_back(pair<int, int>(30, 0));
int turn = 1, result;
for(int i = 0; i < n; i++){
string action;
cin >> action;
if(action == "summon"){
int p, att, h;
cin >> p >> att >> h;
if(turn == 1){
a.insert(a.begin()+p, pair<int, int>(h, att));
}
else if(turn == -1){
b.insert(b.begin()+p, pair<int, int>(h, att));
}
}
if(action == "attack"){
int att, def;
cin >> att >> def;
if(turn == 1){
a[att].first -= b[def].second;
b[def].first -= a[att].second;
if(a[att].first <= 0 && att){
a.erase(a.begin()+att);
}
if(b[def].first <= 0 && def){
b.erase(b.begin()+def);
}
}
else if(turn == -1){
b[att].first -= a[def].second;
a[def].first -= b[att].second;
if(b[att].first <= 0 && att){
b.erase(b.begin()+att);
}
if(a[def].first <= 0 && def){
a.erase(a.begin()+def);
}
}
if(a[0].first <= 0 || b[0].first <= 0) break;
}
if(action == "end"){
turn = 0 - turn;
}
}
if(a[0].first > 0 && b[0].first <= 0) result = 1;
else if(a[0].first <= 0 && b[0].first > 0) result = -1;
else result = 0;
cout << result << endl;
output(a);
output(b);
return 0;
}
个人心得:
这道题我修改了很多遍,用了vector也用过list,但是都只有80分,后来才发现是我result的取值出现了问题= =
if(a[0].first && b[0].first) result = 0;
else result = a[0].first > b[0].first? 1 : -1;
这是我之前的写法,因为我觉得平局的情况只有两个英雄都活着这种可能,不可能两个都死了,因为只有随从有攻击力。然后如果没有都活着那就是死了一个,一正一负肯定是大的那个赢了,现在我还是不懂为什么这种想法是错的orz......