NC252890. child
描述
输入描述
第一行输入一个正整数 t (t < 1000)代表接下来有 t 组询问。接下来 t 行,每行四个不超过一万的非负整数 c1, c2, c3, c4 (依次代表①到④号巧克力的数量)
输出描述
对每组询问输出一个非负整数,表示答案。
示例1
输入:
1 4 1 1 2
输出:
1
说明:
Python3 解法, 执行用时: 101ms, 内存消耗: 4744K, 提交时间: 2023-06-02 14:45:11
t=int(input()) for _ in range(t): [c1,c2, c3, c4]=list(map(int,input().split())) def f(c1,c2,c3,c4,n): c1-=4*n needc=n needh=n needd=n x=min(needh,c2) needh-=x c2-=x needc+=needh x=min(needc,c4) c4-=x needc-=x x=min(needc,c1,c3) c1-=x c3-=x needc-=x if c1<3*needc: return False c1-=3*needc x=min(c3,c2,needd) c3-=x c2-=x needd-=x x=min(c3,c4,needd) c3-=x c4-=x needd-=x x=min(c3//2,c1,needd) c3-=2*x c1-=x needd-=x x=min(c1//2,c2,needd) c1-=2*x c2-=x needd-=x x=min(c1//2,c4,needd) c1-=2*x c4-=x needd-=x x=min(c1//3,c3,needd) c1-=3*x c3-=x needd-=x if c1<5*needd: return False return True l=1 r=c1//4 while l<=r: mid=(l+r)>>1 if f(c1,c2,c3,c4,mid): l=mid+1 else: r=mid-1 print(r)
C++(g++ 7.5.0) 解法, 执行用时: 7ms, 内存消耗: 448K, 提交时间: 2023-06-02 15:14:56
#include <bits/stdc++.h> using namespace std; int a, b, c, d; bool check (int s) { int _a = a, _b = b, _c = c, _d = d; _a -= 3 * s; int p1 = s, p2 = s, p3 = s; if (_d <= s) p3 -= _d; else p3 = 0, _b += _d - s; if (_b <= s) p2 -= _b; else p2 -= s, p1 = max(0, p1 - _b + s); int O = (s - p1) + p1 * 2 + p2 + p3; int P = (s - p1) * 2 + p1 * 5 + (s - p2) * 1 + p2 * 4 + p3 * 3; O = min(O, _c); P -= O * 2; return _a - P >= 0; } int main() { int T; cin >> T; while (T --) { cin >> a >> b >> c >> d; int l = 0, r = 1e4 + 10, m; while (l < r - 1) { m = (l + r) >> 1; if (check(m)) l = m; else r = m; } printf("%d\n", l); } return 0; }
C++(clang++ 11.0.1) 解法, 执行用时: 7ms, 内存消耗: 508K, 提交时间: 2023-06-27 23:52:36
#include<iostream> using namespace std; int a,b,c,d; bool check(int x){ int _a = a,_b = b,_c = c,_d = d; _a -= 3 * x; _d -= x; if(_d < 0) { _c += _d; _a += _d; _d = 0; } if(_b + _d < 2 * x){ _c -= 2*x -(_b + _d); _a -= 2*x -(_b + _d); } _c -= x; if(_c < 0) _a+=_c*2; _a -= x; if(_a < 0) return false; else return true; } int main(){ int t; cin>>t; while(t--){ cin>>a>>b>>c>>d; int l = 0; int r = 10005; int mid; while(l < r){ mid = (l + r + 1)>>1; if(check(mid)) l = mid; else r = mid - 1; } cout<<l<<endl; } return 0; }