Codeforces Round #277.5 (Div. 2)

Source: Internet
Author: User

Codeforces Round #277.5 (Div. 2)

Question 1:

Question: The minimum number of exchange requests does not decrease the sequence. The number of exchanges cannot exceed n.

Idea: Since n is not exceeded, select sorting is used. Although the complexity is very high, the number of exchanges is indeed at least. Find the minimum value each time, and then exchange it with the current value.

Question:

A. SwapSorttime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

In this problem your goal is to sort an array consistingNIntegers in at mostNSwaps. For the given array find the sequence of swaps that makes the array sorted in the non-descending order. Swaps are saved med consecutively, one after another.

Note that in this problem you do not have to minimize the number of swaps-your task is to find any sequence that is no longerN.

Input

The first line of the input contains integerN(1? ≤?N? ≤? 3000)-the number of array elements. The second line contains elements of array:A0 ,?A1 ,?...,?AN? -? 1 (? -? 109? ≤?AI? ≤? 109), whereAIIsI-Th element of the array. The elements are numerated from 0N? -? 1 from left to right. Some integers may appear in the array more than once.

Output

In the first line printK(0? ≤?K? ≤?N)-The number of swaps. NextKLines must contain the descriptions ofKSwaps, one per line. Each swap shocould be printed as a pair of integersI,J(0? ≤?I,?J? ≤?N? -? 1), representing the swap of elementsAIAndAJ. You can print indices in the pairs in any order. The swaps are saved med in the order they appear in the output, from the first to the last. It is allowed to printI? =?JAnd swap the same pair of elements multiple times.

If there are multiple answers, print any of them. It is guaranteed that at least one answer exists.

Sample test (s) input
55 2 5 1 4
Output
20 34 2
Input
610 20 20 40 60 60
Output
0
Input
2101 100
Output
10 1

Code:

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    # Include
     
      
# Include
      
        # Include
       
         # Include
        
          # Define eps 1e-9 # define ll long # define INF 0x3f3f3fusing namespace std; priority_queue
         
           , Greater
          
            > Q; const int maxn = 3000 + 10; int n, a [maxn]; struct Node {int x, y;} node [maxn]; int main () {int cnt = 0; while (~ Scanf ("% d", & n) {for (int I = 1; I <= n; I ++) scanf ("% d ", & a [I]); for (int I = 1; I <= n; I ++) {int p = I; for (int j = I + 1; j <= n; j ++) {if (a [j]
           
Question B

Q: Tell n boys, m girls, and then tell them the level of dancing respectively. Then the condition that men and women can match is that their level is at most 1 different ,.

Idea: arrange their levels directly, so you can be greedy and find the matching level for each boy, and then select from small to large. The smaller the value, the better, if you select a relatively large partner, it may affect the partner of a high-level boy. Therefore, it is correct to become greedy from small to large.

Question:

B. BerSU Balltime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

The Berland State University is hosting a ballroom dance in celebration of its 100500-th anniversary!NBoys andMGirls are already busy rehearsing waltz, minuet, polonaise and quadrille moves.

We know that several boy & girl pairs are going to be invited to the ball. However, the partners 'Dancing skill in each pair must differ by at most one.

For each boy, we know his dancing skills. Similarly, for each girl we know her dancing skills. Write a code that can determine the largest possible number of pairs that can be formed fromNBoys andMGirls.

Input

The first line contains an integerN(1? ≤?N? ≤? 100)-the number of boys. The second line contains sequenceA1 ,?A2 ,?...,?AN(1? ≤?AI? ≤? 100), whereAIIsI-Th boy's dancing skill.

Similarly, the third line contains an integerM(1? ≤?M? ≤? 100)-the number of girls. The fourth line contains sequenceB1 ,?B2 ,?...,?BM(1? ≤?BJ? ≤? 100), whereBJIsJ-Th girl's dancing skill.

Output

Print a single number-the required maximum possible number of pairs.

Sample test (s) input
41 4 6 255 1 5 7 9
Output
3
Input
41 2 3 4410 11 12 13
Output
0
Input
51 1 1 1 131 2 3
Output
2
Code:

#include
            
             #include
             
              #include#include
              
               using namespace std;const int maxn=100+10;int a[maxn],b[maxn],n,m;bool vis[maxn];int main(){    int ans,l,mid,r;    while(~scanf("%d",&n))    {        ans=0;        memset(vis,false,sizeof(vis));        for(int i=1;i<=n;i++)            scanf("%d",&a[i]);        scanf("%d",&m);        for(int i=1;i<=m;i++)            scanf("%d",&b[i]);        sort(a+1,a+1+n);        sort(b+1,b+1+m);        for(int i=1;i<=n;i++)        {            l=a[i]-1,mid=a[i],r=a[i]+1;            for(int j=1;j<=m;j++)            {                if(!vis[j])                {                    if(b[j]==l)                    {                        ans++;                        vis[j]=true;                        break;                    }                    else if(b[j]==mid)                    {                        ans++;                        vis[j]=true;                        break;                    }                    else if(b[j]==r)                    {                        ans++;                        vis[j]=true;                        break;                    }                }            }        }        printf("%d\n",ans);    }    return 0;}
              
             
            

Question 3:

The question "m" and "s" are given, that is, to determine two numbers. They all have m bits, and each of them is added with s.

Idea: build, the maximum value is better, that is, from the highest bit to the lowest bit, and then less than 9, the next bit is the number, and then directly jump out, the next is all 0, when constructing the smallest value, the smallest value is first assigned a value of 1 to the highest bit, then 9 is calculated for each vertex, and then the last value is assigned to this digit if less than 9 is used, if there is still a value until the highest bit, the highest bit = ss + 1, and the minimum value are also constructed ..

Ps: Pay attention to several issues. First, directly output-1-1, that is, s = 0 or 9 * m.

Question:

C. Given Length and Sum of Digits... time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

You have a positive integerMAnd a non-negative integerS. Your task is to find the smallest and the largest of the numbers that have lengthMAnd sum of digitsS. The required numbers shocould be non-negative integers written in the decimal base without leading zeroes.

Input

The single line of the input contains a pair of integersM,S(1? ≤?M? ≤? 100 ,? 0? ≤?S? ≤? (900)-the length and the sum of the digits of the required numbers.

Output

In the output print the pair of the required non-negative integer numbers-first the minimum possible number, then-the maximum possible number. if no numbers satisfying conditions required exist, print the pair of numbers "-1-1" (without the quotes ).

Sample test (s) input
2 15
Output
69 96
Input
3 0
Output
-1 -1
Code:

#include
            
             #include
             
              #include
              
               #include#include
               #include
                
                 #include
                 
                  #include
                  
                   #include
                   
                    #define eps 1e-9#define ll long long#define INF 0x3f3f3f3fusing namespace std;priority_queue
                    
                     ,greater
                     
                       >Q;const int maxn=100+10;int m,xx;char s[maxn],t[maxn];int main(){ int ss; while(~scanf("%d%d",&m,&xx)) { for(int i=0;i
                      
                       9*m||(xx==0&&m>1)) { puts("-1 -1"); continue; } else { ss=xx; ss--; t[0]='1'; for(int i=m-1;i>0;i--) { int x=min(ss,9); if(x==0) break; t[i]=x+'0'; ss-=x; } if(ss) t[0]=ss+1+'0'; for(int i=0;i
                       
                        0) { s[i]=9+'0'; ss=ss-9; } else { s[i]=ss+'0'; break; } } for(int i=0;i
                        
                         

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.