BestCoder Round #93 MG loves gold hdu 6019

map水过的,下面的离散化的代码是copy,正在学习离散化中

这是题目链接

  MG loves gold

#include <iostream>
#include <cstdio>
#include <string.h>
#include <cstdlib>
#include <algorithm>
#include <map>

using namespace std;

int main()
{
    int t;
    scanf("%d", &t);
    int a[112345];
    while(t--){
        map<int,int>mp;
        int n;
        scanf("%d", &n);
        for(int i = 0;i <n;i++){
            scanf("%d", &a[i]);
        }
        int sum = 1;
        for(int i = 0;i < n;i++){
            mp[a[i]]++;
            if(mp[a[i]]!=1){
                mp.clear();
                sum++;
                mp[a[i]]++;
            }
        }
        printf("%d\n", sum);
    }

    return 0;
} 



先对数据进行离散化,因为C<=2000000000,所以c的范围很大,但是n很小 1<=n<=100000, 数据之间只存在大小关系,与具体的数值无关,所以可以离散化

然后进行线性的扫描就行了,每次换新铲子的时候把计数的数组重置即可


#include <bits/stdc++.h>

using namespace std;

const int maxn = 100005;

int a[maxn],f[maxn],n;

void read(int &x)
{
    char c;
    bool f = 0;
    while (c = getchar(),(c < '0' || c > '9') && c != '-');
    if (c == '-') f = 1,x = 0; else x = c - 48;
    while (c = getchar(),c >= '0' && c <= '9') x = x * 10 + c - 48;
    if (f) x = -x;
}

void work()
{
    static int v[maxn],c[maxn];
    memset(c,0,sizeof c);
    read(n);
    for(int i = 1;i <= n;i ++) read(a[i]),v[i] = a[i];
    sort(v + 1,v + n + 1);
    for(int i = 1;i <= n;i ++) a[i] = lower_bound(v + 1,v + n + 1,a[i]) - v;
    for(int i = 1,l = 1;i <= n;i ++)
    {
        c[a[i]] ++;
        for(;c[a[i]] > 1;c[a[l]] --,l ++);
        f[i] = f[l - 1] + 1;
    }
    printf("%d\n", f[n]);
}

int main()
{
    int t;
    scanf("%d", &t);
    for(;t;t --) work();
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值