国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

uva 11997 K Smallest Sums

2019-11-11 05:15:50
字體:
來源:轉載
供稿:網友

原題: You’re given k arrays, each array has k integers. There are k k ways to pick exactly one element in each array and calculate the sum of the integers. Your task is to find the k smallest sums among them. Input There will be several test cases. The first line of each case contains an integer k (2 ≤ k ≤ 750). Each of the following k lines contains k positive integers in each array. Each of these integers does not exceed 1,000,000. The input is terminated by end-of-file (EOF). Output For each test case, PRint the k smallest sums, in ascending order. Sample Input 3 1 8 5 9 2 5 10 7 6 2 1 1 1 2 Sample Output 9 10 12 2 2

中文: 給你k個數組,每個數組有k個數。現在問你每個數組中取一個數加一起求和,那么最小的前k個是多少?

#include <bits/stdc++.h>using namespace std;int k;vector<int> v[751],ans;struct item{ int s,b; item(int s,int b):s(s),b(b){} bool Operator < (const item &rhs) const { return s>rhs.s; }};void ini(){ for(int i=1;i<=k;i++) v[i].clear();}void Merge(vector<int> tmp){ priority_queue<item> pq; for(int i=0;i<ans.size();i++) pq.push(item(ans[i]+tmp[0],0)); for(int i=0;i<k;i++) { item t=pq.top(); pq.pop(); ans[i]=t.s; int b=t.b; if(b+1<k) pq.push(item(t.s-tmp[b]+tmp[b+1],b+1)); }}int main(){ ios::sync_with_stdio(false); while(cin>>k) { ini(); for(int i=1;i<=k;i++) { for(int j=1;j<=k;j++) { int res; cin>>res; v[i].push_back(res); } sort(v[i].begin(),v[i].end()); } ans=v[1]; for(int i=2;i<=k;i++) Merge(v[i]); for(int i=0;i<ans.size();i++) if(i!=ans.size()-1) cout<<ans[i]<<" "; else cout<<ans[i]<<endl; } return 0;}

思路: 訓練指南中的例題,想了一會沒思路,直接看的答案。想法很巧妙。 假設有兩個數組求前k個和最小保存在ans當中,現在有第三個數組,那么三個數組的和的最小值就是把當前保存的前k個最小值加上第三個數組當中的k個值即可。 用優先隊列維護前k個最小值,然后用遞推的思想每次算出加上第三個數組中第i個值的和即可。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 济宁市| 通州市| 穆棱市| 合江县| 准格尔旗| 招远市| 德庆县| 宁阳县| 泰顺县| 黔西县| 上杭县| 屯昌县| 南投市| 巴彦淖尔市| 若尔盖县| 枣强县| 灌云县| 运城市| 开鲁县| 青川县| 宝丰县| 射阳县| 集安市| 平乡县| 汉源县| 鹤峰县| 乐清市| 德令哈市| 福鼎市| 浦北县| 新营市| 巴青县| 陵水| 鄂托克前旗| 镇安县| 泾阳县| 进贤县| 武乡县| 乌什县| 苏州市| 长武县|