• 16 多校 8 Ball (贪心排序)很巧妙的思路啊~


    ZZX has a sequence of boxes numbered 1,2,...,n1,2,...,n. Each box can contain at most one ball. 

    You are given the initial configuration of the balls. For 1in1≤i≤n, if the ii-th box is empty then a[i]=0a[i]=0, otherwise the i-th box contains exactly one ball, the color of which is aii, a positive integer. Balls with the same color cannot be distinguished. 

    He will perform m operations in order. At the i-th operation, he collects all the balls from boxes lii,lii+1,...,rii-1,rii, and then arbitrarily put them back to these boxes. (Note that each box should always contain at most one ball) 

    He wants to change the configuration of the balls from a1..n1..n to b1..n1..n (given in the same format as a1..n1..n), using these operations. Please tell him whether it is possible to achieve his goal. 

    InputFirst line contains an integer t. Then t testcases follow. 
    In each testcase: First line contains two integers n and m. Second line contains a11,a22,...,ann. Third line contains b11,b22,...,bnn. Each of the next m lines contains two integers lii,rii. 

    1<=n<=1000,0<=m<=1000, sum of n over all testcases <=2000, sum of m over all testcases <=2000. 

    0<=aii,bii<=n. 

    1<=lii<=rii<=n.
    OutputFor each testcase, print "Yes" or "No" in a line.Sample Input

    5
    4 1
    0 0 1 1
    0 1 1 1
    1 4
    4 1
    0 0 1 1
    0 0 2 2
    1 4
    4 2
    1 0 0 0
    0 0 0 1
    1 3
    3 4
    4 2
    1 0 0 0
    0 0 0 1
    3 4
    1 3
    5 2
    1 1 2 2 0
    2 2 1 1 0
    1 3
    2 4

    Sample Output

    No
    No
    Yes
    No
    Yes

    非常巧妙的思路。以下题意和题解转自http://www.cnblogs.com/Ritchie/p/5761913.html

    题意T组数据,每组给定一个n一个m,在给定两个长度为n的数组ab,再给定m次操作,每次给定lr,每次可以把[l,r]的数进行任意调换位置,问能否在转换后使得a数组变成b数组

    题解:用结构体存储a数组,一共两个域,一个是值,一个是下标,这个下标指的是他最后应该在的位置即这个值在b数组中的下标。随后m次操作可以看做是对a数组lr这个区间进行msortsort是根据下标从小到大排序,这样会使得这个数向他应该在的位置偏移,就是把a数组往b数组上靠,该向左的向左去,该向右的向右去,如果最后的时候都偏移到了自己应该在的位置就是Yes,否则就是No。复杂度O(max((n*n),(m*n*log(n))))

     1 #include <iostream>
     2 #include <cstdio>
     3 #include<algorithm>
     4 #include<string>
     5 #include<cstring>
     6 using namespace std;
     7 
     8 struct node
     9 {
    10     int num,x;
    11 }a[1005];
    12 int b[1005];
    13 bool vis[1005];
    14 
    15 bool cmp(node p,node q)
    16 {
    17     return p.x<q.x;
    18 }
    19 
    20 int main()
    21 {
    22     int T,n,m,l,r;
    23     scanf("%d",&T);
    24     while(T--)
    25     {
    26         scanf("%d%d",&n,&m);
    27         for(int i=0;i<n;i++)
    28             scanf("%d",&a[i].num);
    29         memset(vis,false,sizeof(vis));
    30         for(int i=0;i<n;i++)
    31         {
    32             scanf("%d",&b[i]);
    33             for(int j=0;j<n;j++)
    34                 if(a[j].num==b[i]&&!vis[j])
    35                 {
    36                     a[j].x=i;
    37                     vis[j]=true;
    38                     break;
    39                 }
    40         }
    41         while(m--)
    42         {
    43             scanf("%d%d",&l,&r);
    44             sort(a+l-1,a+r,cmp);
    45         }
    46         bool flag=true;
    47         for(int i=0;i<n;i++)
    48         {
    49             if(!vis[i])
    50             {
    51                 flag=false;
    52                 break;
    53             }
    54             if(a[i].x!=i)
    55             {
    56                 flag=false;
    57                 break;
    58             }
    59         }
    60         if(flag)
    61             printf("Yes
    ");
    62         else
    63             printf("No
    ");
    64 
    65     }
    66     return 0;
    67 }
  • 相关阅读:
    iOS 11: CORE ML—浅析
    Android 平台 Native 代码的崩溃捕获机制及实现
    H5直播避坑指南
    Mac系统升级至OS X Mavericks后Genymotion出现的问题及解决方法
    Android 4.4 KitKat终于支持录屏(Screen Recording)了!
    Android开发者资源大汇总
    用AndroidSDK中的Face Detector实现人脸识别
    [Android设计模式]Android退出应用程序终极方法
    ActionBarCompat 教程-实现Action Bar
    使用Roboguice依赖注入规划Android项目
  • 原文地址:https://www.cnblogs.com/Annetree/p/7205642.html
Copyright © 2020-2023  润新知