题目分析
没有题目分析……
寄存一下神奇反悔贪心
1 #include<bits/stdc++.h> 2 const int maxn = 100035; 3 4 struct node 5 { 6 int a,b; 7 node(int x=0, int y=0):a(x),b(y) {} 8 bool operator < (node x) const 9 { 10 return a < x.a; 11 } 12 }a[maxn],b[maxn]; 13 int T,n,m,mn,tot; 14 int mx,ans,cnt1,cnt2; 15 16 int read() 17 { 18 char ch = getchar(); 19 int num = 0; 20 bool fl = 0; 21 for (; !isdigit(ch); ch=getchar()) 22 if (ch==‘-‘) fl = 1; 23 for (; isdigit(ch); ch=getchar()) 24 num = (num<<1)+(num<<3)+ch-48; 25 if (fl) num = -num; 26 return num; 27 } 28 int main() 29 { 30 freopen("assassin.in","r",stdin); 31 freopen("assassin.out","w",stdout); 32 T = read(); 33 while (T--) 34 { 35 n = read(), m = read(); 36 cnt1 = cnt2 = mx = ans = tot = 0, mn = m+1; 37 for (int i=1; i<=n; i++) 38 { 39 int x = read(), y = read(); 40 if (y) a[++cnt1] = node(x, y), mn = std::min(mn, x), tot += y; 41 else b[++cnt2] = node(x, y); 42 } 43 std::sort(a+1, a+cnt1+1); 44 std::sort(b+1, b+cnt2+1); 45 if (m >= mn){ 46 mx = tot+1, ans = mn; 47 if (mx >= n){ 48 printf("%d %d\n",n,mn); 49 continue; 50 } 51 cnt2 -= mx-cnt1; 52 for (int i=2; i<=cnt1; i++) b[++cnt2] = a[i]; 53 std::sort(b+1, b+cnt2+1); 54 for (int i=1; i<=cnt2&&mx<n; i++) 55 if (ans+b[i].a <= m) mx++, ans += b[i].a; 56 else break; 57 printf("%d %d\n",mx,ans); 58 }else{ 59 mx = 0; 60 for (int i=1; i<=cnt2; i++) 61 if (b[i].a <= m) m -= b[i].a, mx++, ans += b[i].a; 62 printf("%d %d\n",mx,ans); 63 } 64 } 65 return 0; 66 }
END
原文地址:https://www.cnblogs.com/antiquality/p/9845230.html
时间: 2024-10-31 07:21:19