hdu--1280--与前类似

这题和我前天还是昨天做的一题很相似

都是先2层for预处理 然后通过hash标记数组来做

这题 我也尝试了下用 优先队列

第一遍做的时候 忘记在2层for里面去维护队列长度了 导致MLE。。。

第2遍 就过了 但是速度上还是比hash慢点 但多种做题的思路就够了~

      touch  me

 1 #include <iostream>
 2 #include <cstring>
 3 #include <queue>
 4 using namespace std;
 5
 6 struct cmp
 7 {
 8     bool operator()( int& a , int& b )
 9     {
10         return a>b;
11     }
12 };
13 priority_queue<int,vector<int>,cmp>q;
14 int arr[3010];
15 int main()
16 {
17     cin.sync_with_stdio(false);
18     int n , k , cnt;
19     bool flag;
20     while( cin >> n >> k )
21     {
22         while( !q.empty() )
23             q.pop();
24         cnt = 0;
25         for( int i = 0 ; i<n ; i++ )
26         {
27             cin >> arr[i];
28             for( int j = 0 ; j<i ; j++ )
29             {
30                 cnt++;
31                 q.push(arr[i]+arr[j]);
32                 if(cnt>k)
33                 {
34                     q.pop();
35                 }
36             }
37         }
38         cnt = 0;
39         while( !q.empty() )
40         {
41             arr[cnt++] = q.top();
42             q.pop();
43         }
44         for( int i = cnt-1; i>=1 ; i-- )
45         {
46             cout << arr[i] << " ";
47         }
48         cout << arr[0] << endl;
49     }
50     return 0;
51 }

 1 #include <iostream>
 2 #include <cstring>
 3 using namespace std;
 4
 5 int hash[10010];
 6 int arr[3010];
 7 int main()
 8 {
 9     cin.sync_with_stdio(false);
10     int n , k , cnt;
11     bool flag;
12     while( cin >> n >> k )
13     {
14         flag = false;
15         memset( hash , 0 , sizeof(hash) );
16         cnt = 0;
17         for( int i = 0 ; i<n ; i++ )
18         {
19             cin >> arr[i];
20             for( int j = 0 ; j<i ; j++ )
21             {
22                 hash[ arr[i] + arr[j] ]++;
23             }
24         }
25         for( int i = 10000; i>=0 ; i-- )
26         {
27             while( hash[i]-- )
28             {
29                 cnt++;
30                 if( cnt == k )
31                 {
32                     cout << i << endl;
33                     flag = true;
34                     break;
35                 }
36                 else
37                 {
38                     cout << i << " ";
39                 }
40             }
41             if( flag )
42                 break;
43         }
44     }
45     return 0;
46 }

今天把 窃听风云的3部看完了  个人感觉 还是比 无间道差了点。。 窃听风云第一部 感觉质量最高点

hdu--1280--与前类似

时间: 2024-08-10 23:16:12

hdu--1280--与前类似的相关文章

hdu 1280 前m大的数 哈希

前m大的数 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10574    Accepted Submission(s): 3686 Problem Description 还记得Gardon给小希布置的那个作业么?(上次比赛的1005)其实小希已经找回了原来的那张数表,现在她想确认一下她的答案是否正确,但是整个的答案是很庞大的表,小

hash--1264--与前类似

hdu的为你推荐相关题目 还真TM相关啊... 这题 又是用hash数组标记来做的 水题啊 明天 有时间的话 随便换换来做 touch  me 1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 5 const int size = 105; 6 int hash[size+5][size+5]; 7 8 int main() 9 { 10 cin.sync_with_stdio(false); 1

HDU 1280 前m大的数

挺水的是吧? 是啊,挺水的. 挺水的比赛怎么没过? 宝宝心里苦啊,当时光顾着看n的范围了,没看清数的范围也不过5000,T T,好崩溃啊T T #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int res[100010],a[3010],End[1010]; int main() { int n,m; while(~

HDU 1280 前m大的数【哈希入门】

题意:中文的题目= =将各种组合可能得到的和作为下标,然后因为不同组合得到的和可能是一样的, 所以再用一个数组num[]数组,就可以将相同的和都记录下来 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<algorithm> 6 using namespace std; 7 8 typedef long long LL

hdu 1280 前m大的数(排序)

题意:排序 思路:排序 #include<iostream> #include<stdio.h> #include<algorithm> using namespace std; int a[4500005]; bool cmp(int a,int b){ return a>b;//降序 } int main(){ int b[3005]; int n,m,i,j,k; while(~scanf("%d%d",&n,&m)){

hdu 4421 和poj3678类似二级制操作(2-sat问题)

/* 题意:还是二进制异或,和poj3678类似 建边和poj3678一样 */ #include<stdio.h> #include<string.h> #include<math.h> #define N 2100 struct node{ int v,next; }bian[N*N]; int head[N],dfn[N],low[N],vis[N],stac[N],belong[N],yong,ans,index,top; void init() { yong=

杭电1280(前m大的数)

点击打开杭电1280 Problem Description 还记得Gardon给小希布置的那个作业么?(上次比赛的1005)其实小希已经找回了原来的那张数表,现在她想确认一下她的答案是否正确,但是整个的答案是很庞大的表,小希只想让你把答案中最大的M个数告诉她就可以了. 给定一个包含N(N<=3000)个正整数的序列,每个数不超过5000,对它们两两相加得到的N*(N-1)/2个和,求出其中前M大的数(M<=1000)并按从大到小的顺序排列. Input 输入可能包含多组数据,其中每组数据包括

hdu 4707 dfs+前向星

#include<stdio.h> #include<string.h> #include<iostream> using namespace std; struct node { int to,next; }A[100010]; int list[100010]; int tot,n,d; int deal(int a,int b) { A[++tot].to=b; A[tot].next=list[a]; list[a]=tot; return 0; } int d

HDU 1003 Max Sum (最大连续子序和)

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 156637    Accepted Submission(s): 36628 Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max su