Codeforces Round #661-C Boats Competition
阿新 • • 發佈:2020-08-06
題目描述:
給出每個人的質量,要求兩人一組分組,每組兩人的重量和與其他組的重量和相等,求最多能分多少組。
輸入描述:
第1行:t(1 \(\leq\) t \(\leq\) 1000)。表示測試的個數。
第2行:n(1 \(\leq\) n \(\leq\) 50)。第一個測試中參賽者的個數。
第3行:n個數字組成的陣列w。表示n位參賽者的重量。(1 \(\leq\) \(w_i\) \(\leq\) n)
剩下的t-1個測試依此類推。
輸出描述:
對於每個測試用例,列印一個整數k。k表示在每個測試用例中分的最多組的個數。
題目連結:https://www.luogu.com.cn/problem/CF1399C
思路:由於題目的資料範圍比較小,可以使用暴力做法。不過直接通過遍歷陣列w很難下手,這時候需要考慮'執果索引'。即通過遍歷每組中兩個人的質量和的所有可能來求解。
(也就是我們通常說的正著想不出來思路可以考慮一下倒著想)。
程式碼如下:
#include<iostream> #include<algorithm> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int t,n,x; cin>>t; while(t--) { cin>>n; vector<int> cnt(n+1); for(int i=1;i<=n;i++) { cin>>x; ++cnt[x]; } int k=2*n,ans=0,res=0; for(int s=2;s<=k;s++) { for(int i=1;i<(s+1)/2;i++) { if(s-i>n) continue; ans+=min(cnt[i],cnt[s-i]); } if(s%2==0) ans+=cnt[s/2]/2; res=max(res,ans); ans=0; } cout<<res<<endl; } }