POJ3640 UVA11286 HDU1904 Conformity【map】

708 篇文章 18 订阅
509 篇文章 6 订阅

Conformity
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2981 Accepted: 908

Description

Frosh commencing their studies at Waterloo have diverse interests, as evidenced by their desire to take various combinations of courses from among those available.

University administrators are uncomfortable with this situation, and therefore wish to offer a conformity prize to frosh who choose one of the most popular combinations of courses. How many frosh will win the prize?

Input

The input consists of several test cases followed by a line containing 0. Each test case begins with an integer 1 ≤ n ≤ 10000, the number of frosh. For each frosh, a line follows containing the course numbers of five distinct courses selected by the frosh. Each course number is an integer between 100 and 499.

The popularity of a combination is the number of frosh selecting exactly the same combination of courses. A combination of courses is considered most popular if no other combination has higher popularity.

Output

For each line of input, you should output a single line giving the total number of students taking some combination of courses that is most popular.

Sample Input

3
100 101 102 103 488
100 200 300 101 102
103 102 101 488 100
3
200 202 204 206 208
123 234 345 456 321
100 200 300 400 444
0

Sample Output

2
3

Source

Waterloo Local Contest, 2007.9.23

问题链接POJ3640 UVA11286 HDU1904 Conformity
问题简述:(略)
问题分析
    选课计算问题。每个人选5门课,如果很多人选择相同5门课认为这个组合比较热门。计算选择最热门课程组合的选择人数。
    一种方法是使用map实现统计,不过在POJ中TLE, 需要更简单的计算方法。
程序说明:(略)
参考链接:(略)
题记:问题的数据表示采用机器表示才是王道,就是快。

AC的C++语言程序如下:

/* POJ3640 UVA11286 HDU1904 Conformity */

#include <iostream>
#include <algorithm>
#include <map>
#include <cstdio>

using namespace std;

typedef long long LL;
const int M = 5;
const int N = 10000;
LL sum[N + 1], scnt;
int b[N + 1];

int main()
{
    int n;
    while(~scanf("%d", &n) && n) {
        for(int i = 0; i < n; i++) {
            int cours[M];
            for(int i = 0; i < M; i++)
                scanf("%d", &cours[i]);
            sort(cours, cours + M);
            LL val = 0;
            for(int i = 0; i < M; i++)
                val *= 1000, val += cours[i];
            sum[i] = val;
        }

        sort(sum, sum + n);

        int maxv = 0, cnt = 0, k = 1;
        sum[n] = 0;
        for(int i = 0; i < n; i++)
            if(sum[i] == sum[i + 1]) k++;
            else {
                b[cnt++] = k;
                maxv = max(maxv, k);
                k = 1;
            }

        int ans = 0;
        for(int i = 0; i < cnt; i++)
            if(b[i] == maxv) ans += maxv;

        printf("%d\n", ans);
    }

    return 0;
}

AC的C++语言程序(POJ中TLE,其他OJ都AC)如下:

/* POJ3640 UVA11286 HDU1904 Conformity */

#include <iostream>
#include <algorithm>
#include <map>
#include <cstdio>

using namespace std;

typedef long long LL;
const int M = 5;

int main()
{
    int n;
    while(~scanf("%d", &n) && n) {
        map<LL, int> m;

        int maxv = 0;
        while(n--) {
            int cours[M];
            for(int i = 0; i < M; i++)
                scanf("%d", &cours[i]);
            sort(cours, cours + M);
            LL val = 0;
            for(int i = 0; i < M; i++)
                val *= 1000, val += cours[i];

            m[val]++;
            maxv = max(maxv, m[val]);
        }

        int cnt = 0;
        for(map<LL, int>::iterator iter = m.begin(); iter != m.end(); iter++)
            if(iter->second == maxv) cnt += maxv;

        printf("%d\n", cnt);
    }

    return 0;
}

AC的C++语言程序(POJ中TLE,其他OJ都AC)如下:

/* POJ3640 UVA11286 HDU1904 Conformity */

#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <cstdio>

using namespace std;

const int M = 5;

int main()
{
    std::ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int n;
    while(~scanf("%d", &n) && n) {
        map<vector<int>, int> m;

        int maxv = 0;
        while(n--) {
            vector<int> v(M);
            for(int i = 0; i < M; i++)
                scanf("%d", &v[i]);

            sort(v.begin(), v.end());

            m[v]++;
            maxv = max(maxv, m[v]);
        }

        int cnt = 0;
        for(map<vector<int>, int>::iterator iter = m.begin(); iter != m.end(); iter++)
            if((*iter).second == maxv) cnt += maxv;

        printf("%d\n", cnt);
    }

    return 0;
}

AC的C++语言程序(POJ中36行编译错误,其他OJ都AC)如下:

/* UVA11286 POJ3640 HDU1904 Conformity */

#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <cstdio>

using namespace std;

const int M = 5;

int main()
{
    std::ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int n;
    while(~scanf("%d", &n) && n) {
        map<vector<int>, int> m;

        int maxv = 0;
        while(n--) {
            vector<int> v(M);
            for(int i = 0; i < M; i++)
                scanf("%d", &v[i]);

            sort(v.begin(), v.end());

            m[v]++;
            maxv = max(maxv, m[v]);
        }

        int cnt = 0;
        for(auto v : m)
            if(v.second == maxv) cnt += maxv;

        printf("%d\n", cnt);
    }

    return 0;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值