zoukankan      html  css  js  c++  java
  • Codeforces1120 A. Diana and Liana

    Description

    At the first holiday in spring, the town Shortriver traditionally conducts a flower festival. Townsfolk wear traditional wreaths during these festivals. Each wreath contains exactly k flowers.

    The work material for the wreaths for all n citizens of Shortriver is cut from the longest flowered liana that grew in the town that year. Liana is a sequence a1, a2, ..., am, where ai is an integer that denotes the type of flower at the position i. This year the liana is very long (m≥n⋅k), and that means every citizen will get a wreath.

    Very soon the liana will be inserted into a special cutting machine in order to make work material for wreaths. The machine works in a simple manner: it cuts k flowers from the beginning of the liana, then another k flowers and so on. Each such piece of k flowers is called a workpiece. The machine works until there are less than k flowers on the liana.

    Diana has found a weaving schematic for the most beautiful wreath imaginable. In order to weave it, k flowers must contain flowers of types b1, b2, ..., bs, while other can be of any type. If a type appears in this sequence several times, there should be at least that many flowers of that type as the number of occurrences of this flower in the sequence. The order of the flowers in a workpiece does not matter.

    Diana has a chance to remove some flowers from the liana before it is inserted into the cutting machine. She can remove flowers from any part of the liana without breaking liana into pieces. If Diana removes too many flowers, it may happen so that some of the citizens do not get a wreath. Could some flowers be removed from the liana so that at least one workpiece would conform to the schematic and machine would still be able to create at least n workpieces?

    Input

    The first line contains four integers m, k, n and s (1≤n,k,m≤5⋅105, k⋅n≤m, 1≤s≤k): the number of flowers on the liana, the number of flowers in one wreath, the amount of citizens and the length of Diana's flower sequence respectively.

    The second line contains m integers a1, a2, ..., am (1≤ai≤5⋅105) — types of flowers on the liana.

    The third line contains s integers b1, b2, ..., bs (1≤bi≤5⋅105) — the sequence in Diana's schematic.

    Output

    If it's impossible to remove some of the flowers so that there would be at least n workpieces and at least one of them fullfills Diana's schematic requirements, output −1.

    Otherwise in the first line output one integer d — the number of flowers to be removed by Diana.

    In the next line output d different integers — the positions of the flowers to be removed.

    If there are multiple answers, print any.

    Solution

    显然,删掉的数的个数是(m-kn)个
    最长的序列长度就是(k+m-k
    n)
    O(n)求出是否有合法情况再暴力减就行了

    #include <cstdio>
    #include <algorithm>
    #define N 500001
    #define open(x) freopen(x".in","r",stdin);freopen(x".out","w",stdout);
    #define fo(i,a,b) for (register int i=a;i<=b;i++)
    using namespace std;
    int m,k,n,s,t,tot,cnt,del,len,l,r,a[N],ans[N],g[N],need[N];
    bool bz[N];
    void write(int x)
    {
        fo(i,x,x+len)
        {
            if (cnt==del) break;
            if (g[a[i]]) g[a[i]]--;else 
            {
                ans[++cnt]=i;
                if (cnt==del) break;
            }
        }
        printf("%d
    ",del);
        fo(i,1,cnt)
            printf("%d ",ans[i]);
        exit(0);
    }
    int main()
    {
        open("diana");
        scanf("%d%d%d%d",&m,&k,&n,&s);
        fo(i,1,m)
            scanf("%d",&a[i]);
        fo(i,1,s)
        {
            scanf("%d",&t);
            need[t]++;
            g[t]++;
            if (need[t]==1) tot++;
            bz[t]=1;
        }
        del=m-k*n;
        len=del+k;
        fo(i,1,len)
        {
            if (bz[a[i]])
    		{
    			need[a[i]]--;
            	if (!need[a[i]]) tot--;
    		}
            if (!tot) write(1);
        }
        l=1;r=len;
        fo(i,2,m-len+1)
        {
            if (bz[a[l]])
            {
                need[a[l]]++;
                if (need[a[l]]==1) tot++;
            }
            l++;r++;
            if (bz[a[r]])
            {
                need[a[r]]--;
                if (!need[a[r]]) tot--;
            }
            if (!tot && !((l-1)%k)) write(i);
        }
        printf("-1");
        return 0;
    }
    
    
    如果自己说什麽都做不到而什麽都不去做的话,那就更是什麽都做不到,什麽都不会改变,什麽都不会结束.
  • 相关阅读:
    给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。
    js 中怎么使 if(aᅠ==1 && a== 2 && ᅠa==3) 返回 true?
    最新Hadoop-2.7.2+hbase-1.2.0+zookeeper-3.4.8 HA高可用集群配置安装
    spring 4 + jpa(hibernate 3/4) + spring mvc 多数据源配置(二)+Druid连接池
    activiti 学习由浅入深
    hadoop2.4.1+hbase0.98.3实现的分布式网盘系统初步(已开源)
    【Note】Linux
    记初学CMMI,跳出码农搬砖时代,人人都是经营者
    java8_接口中的默认方法与静态方法
    java8_Stream
  • 原文地址:https://www.cnblogs.com/Sport-river/p/13721085.html
Copyright © 2011-2022 走看看