Skip to main content
  1. Posts/

hdu 1280 前m大的数 (计数排序)

·1 min
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

hdu1280

题意:给出n(3000)个数,两两求和,输出最大的m(5000)个和。

思路:由于数据有限,想到计数排序。。。以及,m个可能刚好某个数据没有全部输出,要在while里判断一下。。

。。。好好的人。。怎么开始刷水了。。。。。

其实是因为最近在看.suffix array…然后里面涉及到了radix sort..算法课的时候到是写过。。。不过快忘了orz。。所以打算找几道题目。。。? 然而这是计数排序不是基数排序啊摔/!

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年07月30日 星期六 17时58分04秒
 4File Name :code/hdu/1280.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=3005;
34const int M=1E4+7;
35int n;
36int a[N];
37int cnt[M];
38int m;
39int main()
40{
41	#ifndef  ONLINE_JUDGE
42	freopen("code/in.txt","r",stdin);
43  #endif
44
45	while (~scanf("%d %d",&n,&m))
46	{
47	    ms(a,0);
48	    ms(cnt,0);
49	    for ( int i = 1 ; i <= n ; i++) scanf("%d",&a[i]);
50	    int mx = 0;
51	    for (  int i = 1 ; i <= n-1 ; i++)
52		for ( int j = i+1 ; j <= n ; j++)
53		    cnt[a[i]+a[j]]++,mx = max(mx,a[i]+a[j]);
54
55//	    cout<<"mx:"<<mx<<endl;
56	    int num = 0 ;
57	    for ( int i = mx ; i >=1&&num<m; i--)
58	    {
59		while (cnt[i]>0)
60		{
61		    num++;
62		    if (num!=m)
63		    printf("%d ",i);
64		    else printf("%d\n",i);
65		    cnt[i]--;
66		    if (num==m) break;
67		}
68	    }
69	 //   puts("");
70	}
71
72  #ifndef ONLINE_JUDGE
73  fclose(stdin);
74  #endif
75    return 0;
76}

Related

suffix array (转自 codechef)

·5 mins
原文链接:链接 讲了后缀数组的概念,然后从最暴力的O(nnlogn )的复杂度(O(n)用来比较字符串,O(nlogn)是排序的复杂度)逐步优化,依据各个串之间的关系,大概讲了倍增算法,以及给出了一篇The Skew Algorithm 的论文。

codeforces 429 B. Working out (dp)

·2 mins
cf429 b 题目链接 题意: n*m个格子,每个格子有一个人value a[i][j]>0,连个人,一个从左上角到右下角,每次只能向下或者向右移动,一个从左下到右上,每次只能向上或者向右移动,现在要求两个人恰好相遇一次,相遇点的a不算数,问在满足这样的条件下使得两个人的a最大。。。(很坑的一点是。。这里相遇并不考虑时间。。就是说,不在同一时间都到达过某一格子,也认为相遇。所以我认为,题目含义更准确的说法是,路径只有一个交点)