BZOJ4371: [IOI2015]sorting排序

版权声明:...............转载说一声并注明出处qaq............... https://blog.csdn.net/L_0_Forever_LF/article/details/80727013

我们假设E不操作,A把所有元素复位的最优解是枚举i,若他不在位置i上就和位置i交换,把他转化到图上正确性显然
现在E操作,我们假设位置0~n-1上有碟子0~n-1,碟子i上有苹果i
我们让E操作是交换碟子,A操作是交换苹果
发现这和原问题是等价的,于是我们就可以把E的操作和A的操作分离开来

二分答案,执行完E的前mid次操作后判A是否可以按照上面提到的最优解复原

code:

#include<set>
#include<map>
#include<deque>
#include<queue>
#include<stack>
#include<cmath>
#include<ctime>
#include<bitset>
#include<string>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<complex>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;

inline void read(int &x)
{
    char c; while(!((c=getchar())>='0'&&c<='9'));
    x=c-'0';
    while((c=getchar())>='0'&&c<='9') (x*=10)+=c-'0';
}
int ch[10]; int cn;
inline void output(int x)
{
    if(!x) { putchar('0');return; }
    while(x) ch[++cn]=x%10,x/=10;
    while(cn) putchar('0'+ch[cn--]);
}
const int maxn = 210000;

int n,m;
int s[maxn],X[maxn],Y[maxn];

int p[maxn],pi[maxn];
int a[maxn],ai[maxn];

int t[maxn][2],tp;
int judge(int mid,int ans)
{
    for(int i=0;i<n;i++) a[i]=s[i];
    for(int i=1;i<=mid;i++) swap(a[X[i]],a[Y[i]]);
    int use=0;
    for(int i=0;i<n;i++) while(i!=a[i]) swap(a[i],a[a[i]]),use++;

    if(ans)
    {
        for(int i=0;i<n;i++) p[i]=i,pi[i]=i;
        for(int i=1;i<=mid;i++)
        {
            int x=pi[X[i]],y=pi[Y[i]];
            swap(p[x],p[y]);
            pi[p[x]]=x,pi[p[y]]=y;
        }

        for(int i=0;i<n;i++) a[i]=s[i],ai[a[i]]=i;
        tp=0;
        for(int i=0;i<n&&tp<=mid;i++) if(p[ai[i]]!=i)
        {
            ++tp,t[tp][0]=i,t[tp][1]=a[pi[i]];
            int x=ai[t[tp][0]],y=ai[t[tp][1]];
            swap(a[x],a[y]);
            ai[a[x]]=x,ai[a[y]]=y;
        }
        while(ans&&tp<mid) ++tp,t[tp][0]=t[tp][1]=0;

        for(int i=0;i<n;i++) p[i]=i,pi[i]=i;
        for(int i=0;i<n;i++) a[i]=s[i],ai[a[i]]=i;
        for(int i=1;i<=mid;i++)
        {
            int x,y;
            x=pi[X[i]],y=pi[Y[i]];
            swap(p[x],p[y]);
            pi[p[x]]=x,pi[p[y]]=y;

            x=ai[t[i][0]],y=ai[t[i][1]];
            t[i][0]=p[x],t[i][1]=p[y];
            swap(a[x],a[y]);
            ai[a[x]]=x,ai[a[y]]=y;
        }
    }

    return use<=mid;
}

int main()
{
    //freopen("tmp.in","r",stdin);
    //freopen("tmp.out","w",stdout);

    read(n);
    for(int i=0;i<n;i++) read(s[i]);
    read(m); m=n-1;
    for(int i=1;i<=m;i++) read(X[i]),read(Y[i]);

    int l=0,r=n-1;
    while(l<=r)
    {
        int mid=(l+r)>>1;
        if(judge(mid,0)) r=mid-1;
        else l=mid+1;
    }
    printf("%d\n",r+1);
    judge(r+1,1);
    for(int i=1;i<=r+1;i++) output(t[i][0]),putchar(' '),output(t[i][1]),putchar('\n');

    return 0;
}
阅读更多

DNA Sorting(DNA排序)

06-13

求大神用java搞定,一定要是java啊,谢啦。rnrn1 第一题rn DescriptionrnOne measure of ``unsortedness'' in a sequence is the number of pairs of entries that are out of order with respect to each other. For instance, in the letter sequence ``DAABEC'', this measure is 5, since D is greater than four letters to its right and E is greater than one letter to its right. This measure is called the number of inversions in the sequence. The sequence ``AACEDGG'' has only one inversion (E and D)---it is nearly sorted---while the sequence ``ZWQM'' has 6 inversions (it is as unsorted as can be---exactly the reverse of sorted). rnrnYou are responsible for cataloguing a sequence of DNA strings (sequences containing only the four letters A, C, G, and T). However, you want to catalog them, not in alphabetical order, but rather in order of ``sortedness'', from ``most sorted'' to ``least sorted''. All the strings are of the same length. (一个序列有一个度量的方法,比如“DAABEC”的度量就是5,首先D比它右边的四个字母大,而E比它右边的一个字母大,其余字母没有比自己右边字母大则为0,加起来就是5啦,本程序就是实现将多个字符串按这种度量方式从小排到大)rn InputrnThe first line contains two integers: a positive integer n (0 < n <= 50) giving the length of the strings; and a positive integer m (0 < m <= 100) giving the number of strings. These are followed by m lines, each containing a string of length n.rn OutputrnOutput the list of input strings, arranged from ``most sorted'' to ``least sorted''. Since two strings can be equally sorted, then output them according to the orginal order.rn Sample Inputrn10 6rnAACATGAAGGrnTTTTGGCCAArnTTTGGCCAAArnGATCAGATTTrnCCCGGGGGGArnATCGATGCATrnrnSample OutputrnrnCCCGGGGGGArnAACATGAAGGrnGATCAGATTTrnATCGATGCATrnTTTTGGCCAArnTTTGGCCAAArn2 第二题rn DescriptionrnAn ascending sorted sequence of distinct values is one in which some form of a less-than operator is used to order the elements from smallest to largest. For example, the sorted sequence A, B, C, D implies that A < B, B < C and C < D. in this problem, we will give you a set of relations of the form A < B and ask you to determine whether a sorted order has been specified or not. rn InputrnInput consists of multiple problem instances. Each instance starts with a line containing two positive integers n and m. the first value indicated the number of objects to sort, where 2 <= n <= 26. The objects to be sorted will be the first n characters of the uppercase alphabet. The second value m indicates the number of relations of the form A < B which will be given in this problem instance. Next will be m lines, each containing one such relation consisting of three characters: an uppercase letter, the character "<" and a second uppercase letter. No letter will be outside the range of the first n letters of the alphabet. Values of n = m = 0 indicate end of input.rn OutputrnFor each problem instance, output consists of one line. This line should be one of the following three: rnrnSorted sequence determined after xxx relations: yyy...y. rnSorted sequence cannot be determined. rnInconsistency found after xxx relations. rnrnwhere xxx is the number of relations processed at the time either a sorted sequence is determined or an inconsistency is found, whichever comes first, and yyy...y is the sorted, ascending sequence. (本程序要求根据给出的字母之间的大小关系,来进行排序,输出有三种形式:1、Sorted sequence determined after xxx relations: yyy...y. 表示在遍历了xxx个“<”号后,得到了字母的排序为yyy...y;2、Sorted sequence cannot be determined.表示根据给出的字母的大小关系无法得出字母的排列顺序;3、 Inconsistency found after xxx relations.表示给出的字母大小关系是冲突的,比如:A

没有更多推荐了,返回首页