SRM 631 DIV1,srm631div1

來源:互聯網
上載者:User

SRM 631 DIV1,srm631div1
SRM 631 DIV1

A:最多肯定只需要兩步,中間的兩行,一行黑,一行白就可以了,這樣的話,只需要考慮一開始就滿足,和枚舉一行去染色滿足的情況就可以了,暴力即可

B:貪心,一個記錄當前有貓的位置和當前超過一隻貓的位置,然後位置排序從左往右找,如果當前能移動到之前超過兩隻的位置,就全部移動過去,不增加,如果不行,那麼考慮當前這個能不能鋪成一條,如果可以,相應更新位置,如果不行,就讓貓全部堆到右邊右邊去,然後堆數多1

代碼:

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;}};





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.