CF670C Cinema

难度:4

这道题是离散化的例题,题目里面给的数字是10的9次方范围内的,但是实际上个数只有10的6次方级别甚至更少,所以使用离散化,下面采用了两种方式,一种是map映射,时间是1.6s,然后学习了书上的离散化写法,降到了0.234s,时间复杂度的进步是可观的,

总结一下整数离散化的步骤,首先是把所有数据存在一起,然后排序,然后把数据去重存到新的数组里面,然后就是把老数组里面的数据换成新数组的下标,这一步是用stl里面的二分来实现的,

map代码

#include <bits/stdc++.h>

#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pa;

const int maxn = 200005;

int main() {
    int n, m;
    cin >> n;
    int a[maxn], b[maxn], c[maxn];
    int tot[maxn * 3], index = 0;
    map<int, int> mp;
    for (int i = 0; i < n; i++) {
        scanf("%d", &a[i]);
        tot[index++] = a[i];
    }
    cin >> m;
    for (int i = 0; i < m; i++) {
        scanf("%d", &b[i]);
        tot[index++] = b[i];
    }
    for (int i = 0; i < m; i++) {
        scanf("%d", &c[i]);
        tot[index++] = c[i];
    }
    sort(tot, tot + index);
    int cnt = 0;
    for (int i = 0; i < index; i++) {
        if (!i || i && tot[i] != tot[i - 1]) {
            mp[tot[i]] = cnt++;
        }
    }
    int num[cnt + 5] = {};
    for (int i = 0; i < n; i++) {
        num[mp[a[i]]]++;
    }
    int ans;
    int max1 = -1, max2 = -1;
    for (int i = 0; i < m; i++) {
        if (num[mp[b[i]]] > max1) {
            max1 = num[mp[b[i]]];
            max2 = num[mp[c[i]]];
            ans = i + 1;
        } else if (num[mp[b[i]]] == max1 && num[mp[c[i]]] > max2) {
            max2 = num[mp[c[i]]];
            ans = i + 1;
        }
    }
    cout << ans;
    return 0;
}

离散化代码

#include <bits/stdc++.h>

#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pa;

const int maxn = 200005;

int main() {
    int n, m;
    cin >> n;
    int a[maxn], b[maxn], c[maxn];
    int tot[maxn * 3], index = 0;
    map<int, int> mp;
    for (int i = 0; i < n; i++) {
        scanf("%d", &a[i]);
        tot[index++] = a[i];
    }
    cin >> m;
    for (int i = 0; i < m; i++) {
        scanf("%d", &b[i]);
        tot[index++] = b[i];
    }
    for (int i = 0; i < m; i++) {
        scanf("%d", &c[i]);
        tot[index++] = c[i];
    }
    sort(tot, tot + index);
    int cnt = 0;
    int d[maxn * 3];
    for (int i = 0; i < index; i++) {
        if (!i || i && tot[i] != tot[i - 1]) d[cnt++] = tot[i];
    }
    for (int i = 0; i < n; i++) {
        a[i] = lower_bound(d, d + cnt, a[i]) - d;
    }
    for (int i = 0; i < m; i++) {
        b[i] = lower_bound(d, d + cnt, b[i]) - d;
        c[i] = lower_bound(d, d + cnt, c[i]) - d;
    }
    int num[cnt + 5] = {};
    for (int i = 0; i < n; i++) {
        num[a[i]]++;
    }
    int ans;
    int max1 = -1, max2 = -1;
    for (int i = 0; i < m; i++) {
        if (num[b[i]] > max1) {
            max1 = num[b[i]];
            max2 = num[c[i]];
            ans = i + 1;
        } else if (num[b[i]] == max1 && num[c[i]] > max2) {
            max2 = num[c[i]];
            ans = i + 1;
        }
    }
    cout << ans;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值