C. Swaps CodeForces 134C

Source: Internet
Author: User

C. Swaps CodeForces 134C

 

 

C. Swaps time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

There areNPlayers sitting at a round table. All of them haveSCardsNColors in total. besides, initially the first person had cards of only the first color, the second one had cards of only the second color and so on. they can swap the cards by the following rules:

  • As the players swap, a player can give a card of his color only;
  • A player can't accept a card of a color he already has (maid, he can't take cards of his color, no matter whether he has given out all of them or not );
  • During one swap a pair of people swaps cards (each person gives one card and takes one card ).

    The aim of allNPeople is as follows: each of them shoshould give out all the cards he had initially (that is, all cards of his color ). your task is to denote whether such sequence of swaps is possible. if the answer is positive, you should list all the swaps.

    Input

    The first line contains integersN(1? ≤?N? ≤? 200000) andS(1? ≤?S? ≤? (200000). The second line containsNNumbers,I-Th number stands for how many cardsI-Th player has by the moment the game starts. It is possible that a player has no cards initially.

    Output

    On the first line print "No" if such sequence of swaps is impossible. Otherwise, print "Yes". If the answer is positive, next print numberK-The number of the swaps. Then onKLines describe the swaps by pairs of indices of the swapping players. Print the swaps and the numbers of the swaps in any order.

    Sample test (s) input
    4 82 2 2 2
    Output
    Yes44 34 21 31 2
    Input
    6 121 1 2 2 3 3
    Output
    Yes66 56 46 35 45 32 1
    Input
    5 50 0 0 0 5
    Output
    No


     

    The question has three conditions.

    1. Each person can only exchange their own colors.

    2. Everyone cannot accept their own colors or their own colors, even if they do not have their own colors.

    3. Each person swaps their cards each time they exchange

     

    Priority queue + bfs is similar to a simulated process. Here, the make_pair () function is skillfully used, which is equivalent to a first second struct.

     

     

    # Include
       
        
    # Include
        
         
    # Include
         
          
    # Include
          
           
    # Include
           
            
    # Include
            
              # Define N 222222 # define ll _ int64 # define INF 0x3f3f3fusing namespace std; typedef pair
             
               Pp; pp ans [N]; int a, B; int x; int n, s; int main () {cin> n> s; priority_queue
              
                Q; for (int I = 1; I <= n; I ++) {scanf ("% d", & x); q. push (make_pair (x, I) ;}// for (int I = 1; I <= n; I ++) // sort by first in ascending order, then sort by second from large to small // {// pp a; // a = q. top (); // q. pop (); // cout <
              
             
            
           
          
         
        
       

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.