codeforces 620C

题目链接:https://codeforces.com/problemset/problem/620/C

题目分析

             题意:给你一串珍珠,每个珍珠都有一个对应值,需要分割这n个珍珠(必须连续),使得每一串珍珠中含有对应值相同的两个珍珠,并让这个样的珍珠串数目达到最多。

             首先看到这个题目的时候想用数组保存各个数出现的位置的,但是由于题目的要求,数组太大,故改用map来保存各个数出现的位置。

             具体实现,用 s 代表当前珍珠串的起点, e 代表当前搜索的位置, 开始时 s == e ,实现用map通过第e个珍珠的对应值,来访问为这个珍珠的位置 e 。 如果当前位置的珍珠 e 满足

map[e] != 0 ,说明在区间[s,e-1] 含有和珍珠 e 相同的珍珠,也就是说 [ s,e] 是可以分割的最小珍珠串,分割了当前串后,将 s = e +1 ,e = s ,分割位置后移,准备分割下一串珍珠。

             最后,由于题目要求n个珍珠必须分割完毕,所以最后一个珍珠串需要包含到最后一个珍珠(前提是至少有一个满足条件的珍珠串)

代码区

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include <set>
#include <map>
typedef long long ll;
using namespace std;
const int Max = 1e9;
const int Max2 = 3e5 + 10;
const int inf = 0x3f3f3f3f;

int v[Max2];
map<int, int>m;            //前一个数为数值,后一个为位置
pair<int, int>p[Max2];

int main()
{
    int n;
    while (scanf("%d", &n) != EOF)
    {
        int sum = 0;
        for (int i = 1; i <= n; i++)
        {
            scanf("%d", v + i);
        }
        int s = 1, e = 1;
        m.clear();
        while (e <= n)
        {
            if (m[v[e]])    //出现过
            {
                p[++sum] = { s,e };
                s = e + 1;
                e = s;
                m.clear();
            }
            else
            {
                m[v[e]] = e;
                e++;
            }
        }
        p[sum].second = n;    //wa了一次,发现所以的珍珠都需要被使用
        if (sum == 0)
        {
            printf("-1
");
            continue;
        }
        printf("%d
", sum);
        for (int i = 1; i <= sum; i++)
        {
            printf("%d %d
", p[i].first, p[i].second);
        }

    }
    return 0;
}

              

原文地址:https://www.cnblogs.com/winter-bamboo/p/10654540.html