2019ICPC(沈阳) - Flowers(二分)
題目鏈接:點擊查看
題目大意:給出 nnn 種顏色不同的鮮花,每種花有 aia_iai? 個,現(xiàn)在規(guī)定 mmm 個不同的鮮花可以扎成一束,問最多可以扎多少束鮮花
題目分析:考慮問題的簡化版:有三種顏色不同的襪子,分別有 a,b,ca,b,ca,b,c 個,任意兩個顏色不同的襪子可以互相匹配,問最多可以匹配多少個,解法也很簡單,記 sum=a+b+c,mmax=max(a,b,c)sum=a+b+c,mmax=max(a,b,c)sum=a+b+c,mmax=max(a,b,c),則:
第二種情況比較顯然,用另外兩種襪子都來和最大值所表示的襪子匹配一定是最優(yōu)的,重點是需要想明白如果不等式滿足了第一種情況,那么一定是存在著一種分配方案,使得所有襪子要么全匹配,要么至多只剩一只無法匹配的情況
所以擴展到本題的情況,如果去二分答案,那么該如何去 checkcheckcheck 呢。觀察上面問題的簡化版,不難發(fā)現(xiàn)對于某種鮮花而言,大于答案的部分是沒有貢獻的。假設(shè)我們當(dāng)前二分的答案為 midmidmid,只需要將所有鮮花的 min(mid,ai)min(mid,a_i)min(mid,ai?) 求和記為 sumsumsum,就變成了上面提到的第一種情況,也就是此時可以組成 ?sum2?\lfloor \frac{sum}{2} \rfloor?2sum?? 束鮮花,和 midmidmid 判斷一下大小關(guān)系就好了
代碼:
// Problem: Flowers // Contest: NowCoder // URL: https://ac.nowcoder.com/acm/contest/7830/J // Memory Limit: 524288 MB // Time Limit: 2000 ms // // Powered by CP Editor (https://cpeditor.org)// #pragma GCC optimize(2) // #pragma GCC optimize("Ofast","inline","-ffast-math") // #pragma GCC target("avx,sse2,sse3,sse4,mmx") #include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<cassert> #include<bitset> #include<list> #include<unordered_map> #define lowbit(x) (x&-x) using namespace std; typedef long long LL; typedef unsigned long long ull; template<typename T> inline void read(T &x) {T f=1;x=0;char ch=getchar();while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();x*=f; } template<typename T> inline void write(T x) {if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0'); } const LL inf=0x3f3f3f3f3f3f3f3f; const int N=1e6+100; int n,m; LL a[N]; bool check(LL mid) {LL sum=0;for(int i=1;i<=n;i++) {sum+=min(a[i],mid);}return sum/m>=mid; } int main() { #ifndef ONLINE_JUDGE // freopen("data.in.txt","r",stdin); // freopen("data.out.txt","w",stdout); #endif // ios::sync_with_stdio(false);int w;cin>>w;while(w--) {read(n),read(m);for(int i=1;i<=n;i++) {read(a[i]);}LL l=0,r=inf,ans=-1;while(l<=r) {LL mid=(l+r)>>1;if(check(mid)) {ans=mid;l=mid+1;} else {r=mid-1;}}cout<<ans<<endl;}return 0; }總結(jié)
以上是生活随笔為你收集整理的2019ICPC(沈阳) - Flowers(二分)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2021牛客多校3 - Kuriyama
- 下一篇: 2021HDU多校6 - 7029 Me