zoukankan      html  css  js  c++  java
  • Codeforces Round #127 (Div. 1) D. Brand New Problem 暴力dp

    D. Brand New Problem

    题目连接:

    http://www.codeforces.com/contest/201/problem/D

    Description

    A widely known among some people Belarusian sport programmer Lesha decided to make some money to buy a one square meter larger flat. To do this, he wants to make and carry out a Super Rated Match (SRM) on the site Torcoder.com. But there's a problem — a severe torcoder coordinator Ivan does not accept any Lesha's problem, calling each of them an offensive word "duped" (that is, duplicated). And one day they nearely quarrelled over yet another problem Ivan wouldn't accept.

    You are invited to act as a fair judge and determine whether the problem is indeed brand new, or Ivan is right and the problem bears some resemblance to those used in the previous SRMs.

    You are given the descriptions of Lesha's problem and each of Torcoder.com archive problems. The description of each problem is a sequence of words. Besides, it is guaranteed that Lesha's problem has no repeated words, while the description of an archive problem may contain any number of repeated words.

    The "similarity" between Lesha's problem and some archive problem can be found as follows. Among all permutations of words in Lesha's problem we choose the one that occurs in the archive problem as a subsequence. If there are multiple such permutations, we choose the one with the smallest number of inversions. Then the "similarity" of a problem can be written as , where n is the number of words in Lesha's problem and x is the number of inversions in the chosen permutation. Note that the "similarity" p is always a positive integer.

    The problem is called brand new if there is not a single problem in Ivan's archive which contains a permutation of words from Lesha's problem as a subsequence.

    Help the boys and determine whether the proposed problem is new, or specify the problem from the archive which resembles Lesha's problem the most, otherwise.

    Input

    The first line contains a single integer n (1 ≤ n ≤ 15) — the number of words in Lesha's problem. The second line contains n space-separated words — the short description of the problem.

    The third line contains a single integer m (1 ≤ m ≤ 10) — the number of problems in the Torcoder.com archive. Next m lines contain the descriptions of the problems as "k s1 s2 ... sk", where k (1 ≤ k ≤ 500000) is the number of words in the problem and si is a word of the problem description.

    All words from all problem descriptions contain no more than 10 lowercase English letters. It is guaranteed that the total length of words in all problem descriptions does not exceed 500015.

    Output

    If Lesha's problem is brand new, print string "Brand new problem!" (without quotes).

    Otherwise, on the first line print the index of the archive problem which resembles Lesha's problem most. If there are multiple such problems, print the one with the smallest index. On the second line print a string consisting of characters [:, character | repeated p times, and characters :], where p is the "similarity" between this problem and Lesha's one. The archive problems are numbered starting from one in the order in which they are given in the input.

    Sample Input

    4
    find the next palindrome
    1
    10 find the previous palindrome or print better luck next time

    Sample Output

    1
    [:||||||:]

    Hint

    题意

    现在给你n个单词,保证n个单词都不相同,n<=15,单词长度<=10

    然后有q次询问

    每次询问给你m个单词,这m个单词可能是之前给你的n个之一,也可能不是,然后你需要找出一个n个单词的排列(每个单词只出现一次,且恰好是那n个单词),使得逆序数最小

    然后q次询问之后,输出n*(n-1)/2 - 最小的询问答案 + 1

    输出比较特别,答案是多少,就输出多少个竖线

    如果找不到排列,输出Brand new problem!

    题解:

    第一个想法,暴力枚举排列的样子,15!*O(n),显然tle

    然后我们怎么办呢?我们可以2^15*O(n)

    dp[i]表示在i状态下,最小的逆序对数,转移很显然:dp[i|(1<<p)]=min(dp[i|(1<<p)],dp[i]+__builtin_popcount(i & ~((1 << p) - 1)));

    i|(1<<p)是当前状态,从没有这个数的状态转移过来,可以获得前面有多少个本来在他后面的数

    然后直接跑就好了,当然这样也会TLE的。

    不过跑的最快的代码,就是这样做的,他加了一个迷之剪枝……

    如果这个数之前的状态没有更新的话,就直接continue,跑的飞起……


    如果没有剪枝的悟性怎么办?没事儿,还有一种dp

    dp[i][j]表示考虑到了i状态,当前逆序数为j的最小位置是啥

    转移也很简单:dp[i|(1<<k)][j+ones[i>>k]]=min(dp[i|(1<<k)][j+ones[i>>k]],nxt[dp[i][j]][k]);

    当然时间复杂度是15^2*2^15*15,也是迷的飞起……

    跑的飞快的代码

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 1<<15+5;
    const int inf = 1e9;
    char s[16][11],t[11];
    int dp[maxn];
    int ans1=0,ans2=inf;
    int vis[20];
    int n;
    int ones[maxn];
    int fi(char m[])
    {
        for(int i=0;i<n;i++)
            if(strcmp(s[i],m)==0)
                return i;
        return n;
    }
    int main()
    {
        scanf("%d",&n);
        for(int i=1;i<1<<n;i++)
            ones[i]=ones[i>>1]+(i&1);
        for(int i=0;i<n;i++)
            scanf("%s",s[i]);
        int m;
        scanf("%d",&m);
        for(int id=1;id<=m;id++)
        {
            int num;scanf("%d",&num);
            for(int i=0;i<(1<<n);i++)
                dp[i]=inf;
            memset(vis,0,sizeof(vis));
            dp[0]=0;
            int pre = 0;
            while(num--)
            {
                scanf("%s",t);
                int p = fi(t);
                if(p==n)continue;
                if(pre&(1<<p))continue;
                //如果之前的状态都没有更新过的话,就直接continue,非常厉害的剪枝……
                //总而言之,就是暴力出奇迹……
                pre=(pre&((1<<p)-1))|(1<<p);
                for(int i=(1<<n)-1;i>=0;i--)
                    if(dp[i]<inf&&(i&(1<<p))==0)
                        dp[i|(1<<p)]=min(dp[i|(1<<p)],dp[i]+ones[i & ~((1 << p) - 1)]);
            }
            if(ans2>dp[(1<<n)-1])
            {
                ans1=id;
                ans2=dp[(1<<n)-1];
            }
        }
        if(ans1==0)printf("Brand new problem!
    ");
        else{
            printf("%d
    ",ans1);
            printf("[:");
            for(int i=0;i<n*(n-1)/2-ans2+1;i++)
                printf("|");
            printf(":]
    ");
        }
    }
    

    辣鸡dp

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 1<<15+5;
    const int maxn2 = 15*(15-1)/2+1;
    const int inf = 1e9;
    char s[16][11],t[11];
    int dp[1 << 15][15 * (15 - 1) / 2 + 1];
    int nxt[500016][16];
    int ans1=0,ans2=inf;
    int vis[20];
    int n;
    int ones[maxn];
    int fi(char m[])
    {
        for(int i=0;i<n;i++)
            if(strcmp(s[i],m)==0)
                return i;
        return n;
    }
    int solve()
    {
        int num;scanf("%d",&num);
        fill(dp[0], dp[1 << n], inf);
        memset(nxt,-1,sizeof(nxt));
        int cnt = 0;
        for(int i=0;i<num;i++)
        {
            scanf("%s",t);
            int p = fi(t);
            if(p==n)continue;
            nxt[cnt][p]=cnt;
            cnt++;
        }
        for(int i=cnt-2;i>=0;i--)
            for(int j=0;j<n;j++)
                if(nxt[i][j]==-1)
                    nxt[i][j]=nxt[i+1][j];
    
        dp[0][0]=0;
        for(int i=0;i<(1<<n);i++)
        {
            for(int j=0;j<=n*(n-1)/2;j++)
            {
                if(dp[i][j]==inf)continue;
                for(int k=0;k<n;k++)
                {
                    if((1<<k)&i)continue;
                    if(nxt[dp[i][j]][k]==-1)continue;
                    dp[i|(1<<k)][j+ones[i>>k]]=min(dp[i|(1<<k)][j+ones[i>>k]],nxt[dp[i][j]][k]);
                }
            }
        }
        for(int i=0;i<=n*(n-1)/2;i++)
            if(dp[(1<<n)-1][i]!=inf)
                return i;
        return inf;
    }
    int main()
    {
        scanf("%d",&n);
        for(int i=1;i<1<<n;i++)
            ones[i]=ones[i>>1]+(i&1);
        for(int i=0;i<n;i++)
            scanf("%s",s[i]);
        int m;
        scanf("%d",&m);
        for(int id=1;id<=m;id++)
        {
            int tmp = solve();
            if(ans2>tmp)
            {
                ans1=id;
                ans2=tmp;
            }
        }
        if(ans1==0)printf("Brand new problem!
    ");
        else{
            printf("%d
    ",ans1);
            printf("[:");
            for(int i=0;i<n*(n-1)/2-ans2+1;i++)
                printf("|");
            printf(":]
    ");
        }
    }
  • 相关阅读:
    【中文分词】条件随机场CRF
    【中文分词】最大熵马尔可夫模型MEMM
    【中文分词】二阶隐马尔可夫模型2-HMM
    【中文分词】隐马尔可夫模型HMM
    Elasticsearch的CRUD:REST与Java API
    d3的比例尺和坐标轴
    webpack DllPlugin的用法
    webpack单独启动目录方法
    d3的常用方法和数据类型
    d3中的enter,exit,update概念
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5196947.html
Copyright © 2011-2022 走看看