中级农民
- 积分
- 115
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2015-5-28
- 最后登录
- 1970-1-1
|
- #include <iostream>
- #include <map>
- #include <math.h>
- #include <set>
- #include <sstream>
- #include <stack>
- #include <stdio.h>
- #include <unordered_map>
- #include <unordered_set>
- #include <vector>
- using namespace std;
- unordered_map<string, int> convert = {
- {"HEALTHY", 0}, {"RECOVERING", 1}, {"SICK", 2}};
- unordered_map<int, string> number_to_health = {
- {0, "HEALTHY"}, {1, "RECOVERING"}, {2, "SICK"}};
- vector<vector<string>> input;
- unordered_map<string, int> state_person;
- unordered_map<string, string> city_person;
- unordered_map<string, int> state_city;
- unordered_map<string, vector<string>> cities;
- unordered_map<string, int> idx;
- vector<vector<int>> ret;
- void solve() {
- for (int i = 0; i < 365; i++) {
- // initialize state_city for each day
- for (auto city : state_city) {
- state_city[city.first] = 0;
- }
- vector<int> tmp;
- for (auto ele : state_person) {
- string city = city_person[ele.first];
- // calculate the state_city for each day
- state_city[city] = max(state_city[city], ele.second);
- tmp.push_back(ele.second);
- }
- ret.push_back(tmp);
- // check if all healthy
- bool all_healthy = true;
- for (auto person : state_person) {
- if (state_person[person.first] != 0) {
- all_healthy = false;
- break;
- }
- }
- if (all_healthy)
- break;
- // update the state_city for each day
- for (auto city : state_city) {
- state_city[city.first] = (city.second == 1 ? 2 : city.second);
- }
- // update state_person
- for (auto person : state_person) {
- string city = city_person[person.first];
- if (state_city[city] == 2) {
- if (!state_person[person.first])
- state_person[person.first] = 2;
- else
- state_person[person.first]--;
- } else {
- state_person[person.first]--;
- if (state_person[person.first] < 0)
- state_person[person.first] = 0;
- }
- }
- // update city_person
- for (auto person : city_person) {
- int cur_idx = (++idx[person.first]) % cities[person.first].size();
- city_person[person.first] = cities[person.first][cur_idx];
- }
- }
- for (auto person : state_person) {
- cout << person.first << " ";
- }
- cout << endl;
- for (auto ele : ret) {
- for (auto e : ele) {
- cout << number_to_health[e] << " ";
- }
- cout << endl;
- }
- cout << ret.size();
- }
- int main() {
- input = {{"Wilson", "SICK", "PaloAlto", "DC", "London", "PaloAlto"},
- {"Yun", "HEALTHY", "PaloAlto"},
- {"Ali", "RECOVERING", "DC", "DC", "DC", "London"},
- {"Jasmine", "HEALTHY", "London"}};
- for (int i = 0; i < input.size(); i++) {
- string name = input[i][0];
- int health = convert[input[i][1]];
- state_person[name] = health;
- city_person[name] = input[i][2];
- vector<string> tmp;
- for (int j = 2; j < input[i].size(); j++) {
- tmp.push_back(input[i][j]);
- state_city[input[i][j]] = 0;
- }
- cities[input[i][0]] = tmp;
- idx[input[i][0]] = 0;
- }
- solve();
- return 0;
- }
复制代码
补充内容 (2019-3-29 08:00):
sample 能过,不知有没有 bug。 |
|