zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    Java多线程之JUC包:ReentrantLock源码学习笔记
    Java多线程之JUC包:Semaphore源码学习笔记
    Java多线程之JUC包:CountDownLatch源码学习笔记
    Java多线程之JUC包:AbstractQueuedSynchronizer(AQS)源码学习笔记
    Java多线程之JUC包:CyclicBarrier源码学习笔记
    架构设计:企业总体架构要如何做?小白也能快速领悟的设计思想
    可参考才是有价值的,架构设计的技改之路从来都不容易
    架构设计:高并发读取,高并发写入,并发设计规划落地方案思考
    微服务手册:API接口9个生命节点,构建全生命周期管理
    百度api使用心得体会
  • 原文地址:https://www.cnblogs.com/Annetree/p/7205642.html
Copyright © 2011-2022 走看看