《算法竞赛入门经典(大白书)》1.2 && 1.3 【待续】

来源:互联网 发布:mac qq群文件在哪 编辑:程序博客网 时间:2024/05/16 09:14

uva 11462 - Age Sort

题意:
  给你最多2000000个数据,大小是1到99的数,让你排序输出。

思路:
  对于内存的限制,所以我们考虑不开数组记录所有数字,而是选择用1到99每个数字出现了多少次。

#include <stdio.h>#include <bits/stdc++.h>using namespace std;const int mod = 1e9 + 7;const int maxn = 5000 + 5;const int INF = 0x3f3f3f3f;typedef long long LL;typedef pair<int, int>pii;typedef pair<double, double>pdd;typedef pair<LL, LL>pLL;typedef unsigned long long ull;int age[105];int main(){    int n;    while(~scanf("%d", &n))    {        if(n == 0)  break;        memset(age, 0, sizeof(age));        for(int i = 0; i < n; i++)        {            int x;            scanf("%d", &x);            age[x]++;        }        int cnt = 0;        for(int i = 1; i <= 100; i++)        {            while(age[i]--) printf("%d%c", i, ++cnt == n ? '\n' : ' ');        }    }    return 0;}

uva 11078 - Open Credit System

题意:
  给出n个数,找出两个数之间差的最大值,要求num[i] - num[j](i < j)

思路:
  仔细思考一下,其实只需要枚举j,并在过程中保存j的最小值即可。

#include <stdio.h>#include <bits/stdc++.h>using namespace std;const int mod = 1e9 + 7;const int maxn = 100000 + 5;const int INF = 0x3f3f3f3f;typedef long long LL;typedef pair<int, int>pii;typedef pair<double, double>pdd;typedef pair<LL, LL>pLL;typedef unsigned long long ull;int a[maxn];int main(){    int T;    scanf("%d", &T);    while(T--)    {        int n;        scanf("%d", &n);        for(int i = 0; i < n; i++)  scanf("%d", &a[i]);        int ans = -INF, maxx = a[0];        for(int j = 1; j < n; j++)        {            ans = max(ans, maxx - a[j]);            maxx = max(maxx, a[j]);        }        printf("%d\n", ans);    }    return 0;}