麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

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

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個值的和即可。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 精品国产91久久久久久久 | 国产高清成人久久 | 日本黄色一级电影 | 夜夜夜影院 | 色淫视频 | 中文字幕22页 | 久久国产夫妻视频 | 免费嗨片首页中文字幕 | 91久久久久久久久久久久久 | 在线播放黄色网址 | 黄色免费入口 | 极品xxxx欧美一区二区 | 久久久久北条麻妃免费看 | 免费a级黄色片 | 国产亚洲精品久久午夜玫瑰园 | 九九精品视频观看 | 国产成人精品一区二区三区电影 | av免费入口 | 国产毛片网站 | 久久精品免费网站 | 亚洲精品 在线播放 | 大学生一级毛片在线视频 | 欧美a∨一区二区三区久久黄 | 欧美特黄a | 毛片视频免费播放 | 成人免费观看49www在线观看 | 免费一级毛片在线播放视频 | 97视频 | 日韩毛片在线看 | 依依成人综合 | 日韩av在线影院 | 国产成人强伦免费视频网站 | 成年人视频在线免费观看 | 麻豆传传媒久久久爱 | 久草在线观看福利视频 | 亚洲欧美成aⅴ人在线观看 免费看欧美黑人毛片 | 青久草视频 | 亚洲精华液久久含羞草 | 黄色毛片视频在线观看 | 亚洲国产精品500在线观看 | 中文日产幕无线码6区免费版 |