被#8hack了

P1208 [USACO1.3] 混合牛奶 Mixing Milk

@[Tooler_Yang](/user/377768) ``` #include<bits/stdc++.h> using namespace std; struct MILK{ int a,p; }mk[5001]; bool cmp(MILK a,MILK b){ return a.p<b.p; } int main(){ int n,m; cin>>n>>m; for(int i=1;i<=m;i++){ cin>>mk[i].p>>mk[i].a; } sort(mk+1,mk+m+1,cmp); int now=1; long long ans=0; while(1){ if(n<=mk[now].a){ ans+=mk[now].p*n; break; } if(n>mk[now].a){ ans+=mk[now].p*mk[now].a; n-=mk[now].a; } now++; } cout<<ans; return 0; } ``` 把两个 while 循环的 if 换一下位置(或者把 `naw++` 换到第一个 if 后,第二个 if 前),即可AC。 原因是当n走完第一个 if 后,如果 `n<=mk[now].a` 会执行第二个 if 但 now 却没有 加一。
by _Virgo_ @ 2022-08-31 19:51:19


@[ly_潘烨](/user/571589) 谢谢,已AC
by Tooler_Yang @ 2022-08-31 19:52:21


|