SRM 631 div1
A: A maximum of two steps are required. The two rows in the middle are black and one line is white. In this case, you only need to consider the first step, and enumerate a row to dye the satisfied situation, just brute force.
B: greedy. A record records the current location of a cat and the current location of more than one cat, and then sorts the positions from left to right. If the current location can be moved to more than two positions, move all the data in the past without increasing the number. If not, consider whether the current data can be paved. If yes, update the location accordingly. If not, heap all the cats to the right, then the number of heaps is 1
Code:
A:
#include <iostream>#include <string>#include <vector>#include <set>#include <map>using namespace std;class TaroJiroGrid {public:bool judge(vector<string> grid) {for (int i = 0; i < grid.size(); i++) {int cnt = 1;for (int j = 1; j < grid.size(); j++) {if (grid[j][i] == grid[j - 1][i]) {cnt++;} else {if (cnt > grid.size() / 2) return false;cnt = 1;}}if (cnt > grid.size() / 2) return false;}return true;}bool solve(vector<string> grid, int cnt) {if (cnt == 0)if (judge(grid)) return true;else if (cnt == 1) {for (int i = 0; i < grid.size(); i++) {vector<string> tmp = grid;for (int j = 0; j < grid[i].length(); j++)tmp[i][j] = 'B';if (judge(tmp)) return true;tmp = grid;for (int j = 0; j < grid[i].length(); j++)tmp[i][j] = 'W';if (judge(tmp)) return true;}}return false;}int getNumber(vector<string> grid) {for (int i = 0; i < 2; i++) {if (solve(grid, i))return i;}return 2;}};
B:
#include <vector>#include <algorithm>using namespace std;typedef pair<int, int> pii;#define MP(a,b) make_pair(a,b)const int INF = 0x3f3f3f3f;class CatsOnTheLineDiv1 {vector<pii> g;public:int getNumber(vector<int> position, vector<int> count, int time) {int n = position.size();for (int i = 0; i < n; i++)g.push_back(MP(position[i] - time, count[i]));sort(g.begin(), g.end());int le = -INF, sink = -INF, ans = 0;for (int i = 0; i < n; i++) {int l = g[i].first;int r = l + 2 * time;if (l <= sink) continue;le = max(le, l);if (r - l + 1 < count[i]) {ans++;sink = r;} else {le += count[i];}}return ans;}};
SRM 631 div1