AcWing's 82nd weekly match

kth number

Given a sequence of integers a1,a2,…,ana1,a2,…,an of length nn and an integer kk.

Please calculate and output the kkth number of the sequence sorted from big to small .

input format

The first line contains two integers n,kn,k.

The second line contains nn integers a1,a2,…,ana1,a2,…,an.

output format

An integer representing the kkth number after sorting the sequence from largest to smallest .

data range

The first three test points satisfy 1≤n≤101≤n≤10.
All test points satisfy 1≤n≤10001≤n≤1000, 1≤k≤n1≤k≤n, 0≤ai≤1000≤ai≤100.

Input sample 1:

5 3
20 10 30 40 10

Output sample 1:

20

Input sample 2:

6 5
90 20 35 40 60 100

Output sample 2:

35

Input sample 3:

4 3
4 3 3 2

Output sample 3:

3
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++) 
using namespace std;
typedef long long LL; 
int a[1001];
bool cmp(int a,int b)
{
    return a>b;
}
int main()
{
    int n,k;
    cin>>n>>k;
    rep(i,1,n)cin>>a[i];
    sort(a+1,a+1+n,cmp);
    cout<<a[k];
}

dominoes

Arrange n dominoes in a row, with each domino standing vertically.

At the beginning, some dominoes are pushed down at the same time, some to the right and some to the left.

It is guaranteed that among the dominoes that are pushed down at the beginning, there is at least one domino that falls in the opposite direction between any two dominoes that fall in the same direction.

After each second, each domino falling to the left knocks down the domino adjacent to its left, and each domino falling to the right knocks down the domino adjacent to its right.

If, at a certain moment, the adjacent dominoes on both sides of a domino fall towards it at the same time, the domino will remain vertically upright due to the balance of forces.

The figure below gives a possible example of this process.

Given the initial state of each domino, please determine how many dominoes remain vertical after the knockdown process is complete.

input format

The first line contains the integer nn.

The second line contains a string of length nn, where the ii-th character represents the initial state of the ii-th domino:

  • L Indicates that the domino starts to fall to the left.
  • R Indicates that the domino starts to fall to the right.
  • . Indicates that the domino does not fall at the beginning.

Guaranteed, for (i,j)(i,j):

  • If i<ji<j and sisi and sjsj are both  L, then there must exist kk such that i<k<ji<k<j and sksk is  R.
  • If i<ji<j and sisi and sjsj are both  R, then there must exist kk such that i<k<ji<k<j and sksk is  L.

output format

An integer representing the number of dominoes that will eventually remain vertical.

data range

The first 66 test points satisfy 1≤n≤151≤n≤15.
All test points satisfy 1≤n≤30001≤n≤3000.

Input sample 1:

14
.L.R...LR..L..

Output sample 1:

4

Input sample 2:

5
R....

Output sample 2:

0

Input sample 3:

1
.

Output sample 3:

1
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 3010;

int n;
string s;
int l[N], r[N];
int main () 
{
    cin >> n >> s;
    s = ' ' + s;
    for (int i = 1; i <= n; i ++ ) 
        if (s[i] == 'R') l[i] = i;
        else if (s[i] != 'L') l[i] = l[i - 1];

    for (int i = n; i >= 1; i -- ) 
        if (s[i] == 'L') r[i] = i;
        else if (s[i] != 'R') r[i] = r[i + 1];

    int ans = 0;
    for (int i = 1; i <= n; i ++ )
        if (((!l[i] && !r[i]) || (l[i] && r[i] && i - l[i] == r[i] - i)) && s[i] == '.') ans ++ ;

    cout << ans;
    return 0;
}
 

construct sequence

Please construct a 0101 sequence, the sequence needs to meet all the following requirements:

  • Contains exactly nn 00s and mm 11s.
  • There are no two or more 00s consecutively adjacent.
  • There are no three or more 11s in a row adjacent to each other.

input format

A total of one line, containing two integers n,mn,m.

output format

Output a total of one line, if there is a 0101 sequence that meets the condition, then output the 0101 sequence that meets the condition, otherwise output  -1.

If the answer is not unique, then output any reasonable answer.

data range

The first 66 test points satisfy 1≤n, m≤101≤n, m≤10.
All test points satisfy 1≤n, m≤1061≤n, m≤106.

Input sample 1:

1 2

Output sample 1:

101

Input sample 2:

4 8

Output sample 2:

110110110101

Input sample 3:

4 10

Output sample 3:

11011011011011

Input sample 4:

1 5

Output sample 4:

-1

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n, m;
    scanf("%d%d", &n, &m);

    if (m >= n - 1 && m <= 2 * n + 2)
    {
        m -= n - 1;
        if (m == 1) {printf("10"); m = 0;}
        else if (m >= 2) {printf("110"); m -= 2;}
        else printf("0");
        for (int i = 2; i <= n; i ++ )
        {
            if (m >= 1) {printf("110"); m -- ;}
            else printf("10");
        }
        if (m == 2) printf("11");
        if (m == 1) printf("1");
    }
    else puts("-1");

    return 0;
}
 

Guess you like

Origin blog.csdn.net/GeekAlice/article/details/128427325