#include<bits/stdc++.h> #define ll long long #define N 100005 usingnamespace std; ll e, m, h; boolcheck(int x) { if ((e >= x * 5 && m >= x * 3))return1; elsereturn0; } intbsearch_2(int l, int r) { while (l < r) { int mid = l + r + 1 >> 1; if (check(mid)) l = mid; else r = mid - 1; } return l; } intmain() { // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); ll t; cin >> t; while (t--) { cin >> e >> m >> h; int ans = bsearch_2(0, h); cout << ans << endl;
// cin >> e >> m >> h; // while (1) // { // if ((e >= h * 5 && m >= h * 3)&&(e >= (h+1) * 5 && m >= (h+1) * 3)) // { // cout << h << endl; // break; // } // h/2; // while((e >= h * 5 && m >= h * 3))h=h+h/2; // } } }
voidcopy(int a[], int b[], int pos) { for (int i = 1; i <= len(a); i++) b[i + pos - 1] = a[i]; len(b) = len(a) + pos - 1; }
intcompare(int x[], int y[]) { if (len(x) > len(y)) return1; if (len(x) < len(y)) return-1; for (int i = len(x); i >= 1; i--) { if (x[i] > y[i]) return1; if (x[i] < y[i]) return-1; } return0; }
voidadd(int a[], int b[], int c[]) { len(c) = max(len(a), len(b)); for (int i = 1; i <= len(c); i++) { c[i] += a[i] + b[i]; c[i + 1] = c[i] / 10; c[i] %= 10; } while (c[len(c) + 1]) len(c)++; }
voidminu(int a[], int b[], int c[]) { len(c) = max(len(a), len(b)); for (int i = 1; i <= len(c); i++) { c[i] += a[i] - b[i]; if (c[i] < 0) { c[i + 1]--; c[i] += 10; } } while (c[len(c)] == 0 && len(c) > 1) len(c)--; }
voidselfminu(int a[], int b[]) { for (int i = 1; i <= len(a); i++) { if (a[i] < b[i]) { a[i + 1]--; a[i] += 10; } a[i] -= b[i]; } while (len(a) > 0 && a[len(a)] == 0) len(a)--; }
voidmult(int a[], int b[], int c[]) { len(c) = len(a) + len(b); for (int i = 1; i <= len(a); i++) for (int j = 1; j <= len(b); j++) c[i + j - 1] += a[i] * b[j]; for (int i = 1; i <= len(c); i++) if (c[i] > 9) { c[i + 1] += c[i] / 10; c[i] %= 10; } while (c[len(c)] == 0 && len(c) > 1) len(c)--; }
voiddivi(int a[], int b[], int c[], int d[]) { len(c) = len(a) - len(b) + 1; copy(a, d, 1); for (int i = len(c); i > 0; i--) { init(tmp); copy(b, tmp, i); while (compare(d, tmp) >= 0) { c[i]++; selfminu(d, tmp); } } while (len(c) > 0 && c[len(c)] == 0) len(c)--; }
voidselfDiviByLow(int a[], int b,int* res) { int c[MAXN]={0}; len(c)=len(a); while (a[len(c)] < b) { a[len(c) - 1] += a[len(c)] * 10; a[len(c)] = 0; len(c)--; } for (int i = len(c); i >= 1; i--) { if (a[i] >= b) { c[i] = a[i] / b; a[i] %= b; } a[i - 1] += a[i] * 10; } *res=a[1]; copy(c,a,1); }
开始有点难度了,这道题首先有个坑点,就是 long long ,我后面折腾了很久才发现有个 int 忘记改成 long long 了,导致我一直过不了
然后这道题的难度自然是 check() 函数,其实还是不难理解的:首先 k 的搜索范围自然是 H + mOrH ,然后就要看 mOrH 中,到底分了多少给 H ,确定之后把剩下的分给 M ,接着如果 M 还是够不着 k*3 的话要把 eOrM 中的部分分给 M ,剩下的分给 E ,如果够分的话那就 return 1 ,不够分自然就 return 0
#include<bits/stdc++.h> #define ll long long #define N 100005 usingnamespace std; ll E, M, H, eOrM, mOrH;
inlineboolcheck(ll x) { ll e=E,m=M,h=H; if ((e >= x * 5 && m >= x * 3)) return1; if (e + eOrM < x * 5) return0; if (m + mOrH + eOrM < x * 3) return0;
ll mOrHGiveToH, eOrMGiveToM; if (x > h) { mOrHGiveToH = x - h; m += mOrH - mOrHGiveToH; } else { mOrHGiveToH = 0; m += mOrH; } if (m + eOrM < x * 3) return0; if (m < x * 3) { eOrMGiveToM = x * 3 - m; m += eOrMGiveToM; e += eOrM - eOrMGiveToM; } else { eOrMGiveToM = 0; e += eOrM; } if ((e >= x * 5 && m >= x * 3)) return1;
return0; } inline ll bsearch_2(ll l, ll r) { while (l < r) { ll mid = l + r + 1 >> 1; //cout<<mid<<endl; if (check(mid)) l = mid; else r = mid - 1; } return l; } inline ll read() { ll s = 0; char ch = getchar(); while (ch < '0' || ch > '9') { ch = getchar(); } while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar(); return s; } intmain() { //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); ll t; cin >> t; while (t--) { // cin >> e >> m >> h >> eOrM >> mOrH; E = read(); eOrM = read(); M = read(); mOrH = read(); H = read();
ll ans = bsearch_2(0, H + mOrH); cout << ans << endl;
// cin >> e >> m >> h; // while (1) // { // if ((e >= h * 5 && m >= h * 3)&&(e >= (h+1) * 5 && m >= (h+1) * 3)) // { // cout << h << endl; // break; // } // h/2; // while((e >= h * 5 && m >= h * 3))h=h+h/2; // } } }
T7
很遗憾,这道题我最后没 A 掉,看上去可能是用 DP 做的,但是我 DP 学的本来就差,但是我后面一想也不能用DP做呀,就只能照着题目的意思乱写,最后死活 TLE,没办法,人菜还得好好学习
#include<bits/stdc++.h> #define ll long long #define N 1001 #define INF 0x3f3f usingnamespace std; short Move[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; short a[N][N], b[N][N], X, Y, Ax, Ay, Bx, By; bool Map[N][N]; inlineshortmin(short x, short y){ return x < y ? x : y; }
voidstaus(short x, short y, short hack, short step) { if (x == Bx && y == By) { a[x][y] = min(hack, a[x][y]); return; } if (hack >= a[x][y]) return; else a[x][y] = hack; if (step >= b[x][y]) return; else b[x][y] = step; for (short i = 0; i < 4; i++) { short nx = x + Move[i][0], ny = y + Move[i][1]; if (nx < 0 || nx >= X || ny < 0 || ny >= Y) continue; if (Map[nx][ny]) continue; staus(nx, ny, hack, step + 1); } for (short dx = -2; dx <= 2; dx++) { for (short dy = -2; dy <= 2; dy++) { if ((dx == 0 && dy == 0) || (dx == 1 && dy == 0) || (dx == -1 && dy == 0) || (dx == 0 && dy == 1) || (dx == 0 && dy == -1)) continue; short nx = x + dx, ny = y + dy; if (nx < 0 || nx >= X || ny < 0 || ny >= Y) continue; if (Map[nx][ny]) continue; staus(nx, ny, hack + 1, step); } } } intmain() { std::ios::sync_with_stdio(false); // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); short n; cin >> n; while (n--) {
cin >> X >> Y >> Ax >> Ay >> Bx >> By; Ax--; Ay--; Bx--; By--; for (short i = 0; i < X; i++) { for (short j = 0; j < Y; j++) { a[i][j] = INF; b[i][j] = INF; char tmp; cin >> tmp; if (tmp == '.') Map[i][j] = 0; else Map[i][j] = 1; } }