Codeforces Round #516(Div 2)

比賽連接:傳送門node

A. Make a triangle!(簡單思惟)ios

題目大意:c++

給你三條邊,問你最多加多少長度能使這三條邊能構成三角形。ide

思路:spa

最大邊小於答案加另外兩條邊的和。code

#include <bits/stdc++.h>

using namespace std; int main() { int a, b, c; cin >> a >> b >> c; int _max = max(a, b); _max = max(_max, c); int sum = a+b+c - _max; int ans = max(0, _max-sum+1); cout << ans << endl; return 0; }
View Code

 

B. Equations of Mathematical Magic(位運算)blog

題目大意:排序

給定a,求方程:a = x + (a^x)的非負整數解的數量。(符號"^"表示異或)隊列

思路:ci

只有一個二進制位時:(表達式各位置的對應未知數與上面方程對齊)

① a = 0:0 = 0 + (0^0)

② a = 1:1 = 0 + (1^0) = 1 + (1^1)

因此對於題目a的每一個二進制位:

① 若是爲0,則x的對應二進制位也只能爲0

② 若是爲1,則x的對應二進制位能夠是1或0

組合後答案爲2k,(其中k爲a變成二進制後1的個數)

#include <bits/stdc++.h>

using namespace std; int main() { int T; cin >> T; while (T--) { int N; cin >> N; int cnt = 0; while (N) { if (N&1) cnt++; N/=2; } cout << (1 << cnt) << endl; } return 0; } /* 3 0 2 1073741823 */
View Code

 

C. Oh Those Palindromes(字符串)

題目大意:

求給定字符串重排序後,最多的迴文子串數量。

思路:

把同樣的字母放在一塊兒貌似就能夠了。(直接sort)

#include <bits/stdc++.h>

using namespace std; int cnt[30]; int main() { int N; string s; cin >> N >> s; memset(cnt, 0, sizeof cnt); for (int i = 0; i < N; i++) { int ind = s[i] - 'a'; cnt[ind]++; } for (int i = 0; i < 26; i++) { while (cnt[i]--) { printf("%c", 'a'+i); } } printf("\n"); return 0; }
View Code

 

D. Labyrinth(0-1bfs)

題目大意:

給定bfs圖,起始點,最多的左移次數X、右移次數Y,求能訪問的最多的格子數。

思路:

用bfs乍一搜能夠pretest passed,可是能夠被這組樣例hack。

/* 20 7 3 6 5 2 ......* .****.* .****.* ....*.* **.**.* **.**.* **.**.* **.**.* **.**.* **.**.* **.**.* **.**.* **.**.* **.**.* **.**.* **.**.* **.**.* **.**.* **....* ******* */
View Code

加一個對當前點X、Y的最大值的維護便可。

另外能夠按列縮點跑最短路,不過我沒試過。

#include <bits/stdc++.h>

using namespace std; const int MAX_N = 2e3 + 5; const int actx[4] = {0, 0, 1, -1}; const int acty[4] = {1, -1, 0, 0}; struct Node{ int x, y; int resl, resr; Node(int xx = 0, int yy = 0, int ll = 0, int rr = 0) : x(xx), y(yy), resl(ll), resr(rr) {} }nodes[MAX_N][MAX_N]; int N, M, R, C, X, Y; char mat[MAX_N][MAX_N]; bool vis[MAX_N][MAX_N]; inline bool check(int x, int y) { if (x < 1 || x > N || y < 1 || y > M) return false; if (mat[x][y] == '*') return false; return true; } int main() { cin >> N >> M >> R >> C >> X >> Y; memset(vis, false, sizeof vis); for (int i = 1; i <= N; i++) scanf("%s", mat[i]+1); queue <Node> Q; vis[R][C] = true; Q.push(Node(R, C, X, Y)); while (!Q.empty()) { Node cur = Q.front(); Q.pop(); for (int i = 0; i < 4; i++) { int x = cur.x + actx[i]; int y = cur.y + acty[i]; if (!check(x, y)) continue; int l = cur.resl; int r = cur.resr; if (i == 0) r--; if (i == 1) l--; if (l < 0 || r < 0) continue; if (x == 4 && y == 4) int aaa = 1; if (vis[x][y]) { if (l > nodes[x][y].resl || r > nodes[x][y].resr) { nodes[x][y].resl = max(l, nodes[x][y].resl); nodes[x][y].resr = max(r, nodes[x][y].resr); } else
                    continue; } else if (!vis[x][y]) { nodes[x][y] = Node(x, y, l, r); vis[x][y] = true; } Q.push(Node(x, y, l, r)); } } int ans = 0; for (int i = 1; i <= N; i++) { for (int j = 1; j <= M; j++) { if (vis[i][j]) ans++; } } cout << ans << endl; return 0; }
View Code

更新:

正解應該是0-1bfs(說白了就是用雙向隊列deque維護的bfs)

縱向搜到的點入隊首,橫向搜到的點入隊尾。

#include <bits/stdc++.h>

using namespace std; const int MAX_N = 2000 + 5; const int actx[4] = {1, -1, 0, 0}; const int acty[4] = {0, 0, 1, -1}; #define nx cur.x+actx[i]
#define ny cur.y+acty[i]

struct Node{ int x, y; int l, r; Node (int xx = 0, int yy = 0, int ll = 0, int rr = 0) : x(xx), y(yy), l(ll), r(rr) {} }; int N, M, R, C, X, Y; bool vis[MAX_N][MAX_N]; char mat[MAX_N][MAX_N]; bool check(int x, int y) { if (x < 1 || x > N || y < 1 || y > M) return false; if (mat[x][y] == '*' || vis[x][y]) return false; return true; } int bfs0_1() { int ans = 1; memset(vis, false, sizeof vis); deque <Node> Q; Q.push_back(Node(R, C, X, Y)); vis[R][C] = true; while (!Q.empty()) { Node cur = Q.front(); Q.pop_front(); for (int i = 0; i < 4; i++) { if (!check(nx, ny)) continue; int l = cur.l; int r = cur.r; if (i < 2) { Q.push_front(Node(nx, ny, l, r)); } else { if (i == 2) r--; if (i == 3) l--; if (l < 0 || r < 0) continue; Q.push_back(Node(nx, ny, l, r)); } vis[nx][ny] = true; ans++; } } return ans; } int main() { cin >> N >> M >> R >> C >> X >> Y; for (int i = 1; i <= N; i++) scanf("%s", mat[i]+1); cout << bfs0_1() << endl; return 0; }
View Code

 

E. Dwarves, Hats and Extrasensory Abilities(二分)

題目大意:

交互式的題目,給定N,而後你輸出N個點的座標,你每輸出一個點,他給你這個點的顏色(隨機黑或白),而後讓你給出一條直線,使得黑白的點在線的兩邊。

思路:

二分座標系。注意題目中座標系的上限直接拿來用的話,不夠N的最大值。

因此有下圖的二分選擇:

注意交互式的題目輸出時要加一個cout << flush;

#include <iostream>

using namespace std; int main() { int N; cin >> N; int l = 0, r = 1 << 29; string sl, scur; cout << l << ' ' << l << endl << flush; cin >> sl; if (N == 1) { cout << 1 << ' ' << 0 << endl << flush; cout << 1 << ' ' << 1 << endl << flush; return 0; } cout << r << ' ' << 0 << endl << flush; cin >> scur; N -= 2; if (sl == scur) {//在(2^29, 0)到(2^29, 2^29)內二分
        int mid = (l + r) >> 1; int x = 1 << 29; while (N--) { cout << x << ' ' << mid << endl << flush; cin >> scur; if (scur == sl) l = mid; else r = mid; mid = (l + r) >> 1; } cout << x << ' ' << mid << endl << flush; cout << x-1 << ' ' << mid << endl << flush; } else { int mid = (l+r) >> 1; int y = 0; while (N--) { cout << mid << ' ' << y << endl << flush; cin >> scur; if (sl == scur) { l = mid; } else { r = mid; } mid = (l+r) >> 1; } cout << mid << ' ' << y << endl << flush; cout << mid << ' ' << y+1 << endl << flush; } return 0; }
View Code

更新:

在x軸上二分也能夠。

#include <iostream>

using namespace std; void print(int x, int y) { cout << x << ' ' << y << endl << flush; } int main() { int N; string col, pre; cin >> N; int l = 0, mid = 0, r = 1<<29; if (N--) { print(l, 1); cin >> pre; mid = (l+r) >> 1; } while (N--) { print(mid, 1); cin >> col; if (col == pre) l = mid; else r = mid; mid = (l+r) >> 1; } print(l, 0); print(r, 2); return 0; }
View Code

 

F. Candies for Children(數學+分類暴力)

題目大意:

有n我的圍成一圈分k個糖果,從 l 開始分到 r ,能夠跑不少圈。一我的拿一顆,裏面有p我的喜歡甜食會拿兩顆。

問你p的最大值。數據矛盾則輸出-1。

思路:

題目中的數據範圍是1e11,這個數據很微妙(1e11 = 1e5 * 1e6),因此分兩種狀況討論:

① n < 2e6 :

   這時候能夠直接跑答案。

  對於給定的p,y有不少取值,可是能夠直接取模算出y的知足p、d約束最大值。O(n)複雜度。

② n > 2e6 :

   這時候跑圈數i,每一個對應的圈數能夠求出最大的喜歡甜食的人數:

  一、(p + n) * i + y + d = k

  二、p - y <= n - d

  聯立這個就能夠求p的最大值。(其中d表示l跑到r的人數,y表示d中的喜甜人數)而後處理一下細節就能夠O(k/n)。

  而後暴力跑就行了。注意一個點就是最後一我的能夠喜歡甜食可是隻拿一個。

#include <bits/stdc++.h>

using namespace std; typedef long long ll; const ll X = 2e6 + 5; int main() { ll n, l, r, k; cin >> n >> l >> r >> k; ll d = (r - l + n) % n + 1; ll ans = -1; if (n < X) { for (ll p = n; p >= 0; p--) { ll y = (k - 1)%(n + p) + 1 - d; if (y < 0 || y > p || y > d || d-(y+1) > n-p) continue; ll y2 = y+1; if (y2 <= p && y2 <= d) { ans = max(ans , p); } if (d-y <= n-p) { ans = max(ans, p); } } } else { for (ll i = 0; i <= k/n; i++) { ll p = (k - 2*d - (i-1)*n + 1) / (i+1); ll y = k - i*(n+p) - d; if (y < 0) { if (i == 0) continue; ll dis = (-y - 1) / i + 1; y += i*dis; p -= dis; } if (p > n) { y += (p-n) * i; p == n; } if (y < 0 || y > p || y > d || d-(y+1) > n-p) continue; ll y2 = y+1; if (y2 <= p && y2 <= d) { ans = max(ans , p); } if (d-y <= n-p) { ans = max(ans, p); } } } cout << ans << endl; return 0; } /* 10 5 5 1 10000000 5 5 1 */
View Code
相關文章
相關標籤/搜索