CF div2 a b c

给定n个点的坐标,求至少删除多少个点,使得剩余点的集合直径不超过d。通过枚举左端点,找出最优解。另外两个问题分别涉及动态规划求解改变数字到1的最小代价,以及找到字母子集下字典序最大的字符串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A. Points on the line
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

We’ve got no test cases. A big olympiad is coming up. But the problemsetters’ number one priority should be adding another problem to the round.

The diameter of a multiset of points on the line is the largest distance between two points from this set. For example, the diameter of the multiset {1, 3, 2, 1} is 2.

Diameter of multiset consisting of one point is 0.

You are given n points on the line. What is the minimum number of points you have to remove, so that the diameter of the multiset of the remaining points will not exceed d?
Input

The first line contains two integers n and d (1 ≤ n ≤ 100, 0 ≤ d ≤ 100) — the amount of points and the maximum allowed diameter respectively.

The second line contains n space separated integers (1 ≤ xi ≤ 100) — the coordinates of the points.
Output

Output a single integer — the minimum number of points you have to remove.
Examples
Input
Copy

3 1
2 1 4

Output

1

Input
Copy

3 0
7 7 7

Output

0

Input
Copy

6 3
1 3 4 6 9 10

Output

3

Note

In the first test case the optimal strategy is to remove the point with coordinate 4. The remaining points will have coordinates 1 and 2, so the diameter will be equal to 2 - 1 = 1.

In the second test case the diameter is equal to 0, so its is unnecessary to remove any points.

In the third test case the optimal strategy is to remove points with coordinates 1, 9 and 10. The remaining points will have coordinates 3, 4 and 6, so the diameter will be equal to 6 - 3 = 3.
题意:
         给你一个点的集合,问你最少要去掉多少个点,可以使点的集合任意两个点的距离不大于d.
思路:
         我们保留的点肯定是一段段的,所以我们只要枚举左端点就行
code:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<string>
#include<string.h>
#include<set>
#include<stdlib.h>
#define INF 0x3f3f3f
using namespace std;
typedef long long LL;
int arr[110], n, d,ans;
int main()
{
    cin >> n >> d;
    for (int i = 1; i <= n; i++)
        cin >> arr[i];
    sort(arr + 1, arr + 1 + n);
    for (int i = 1; i <= n; i++)
    {
        int temp = 1;
        for (int j = i + 1; j <= n; j++)
        {
            if (arr[j] - arr[i] <= d)
            {
                temp++;
            }
            else
                break;
        }
        ans = max(ans, temp);
    }
    cout << n-ans << endl;
    return 0;
}

B. Our Tanya is Crying Out Loud
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Right now she actually isn’t. But she will be, if you don’t solve this problem.

You are given integers n, k, A and B. There is a number x, which is initially equal to n. You are allowed to perform two types of operations:

Subtract 1 from x. This operation costs you A coins.
Divide x by k. Can be performed only if x is divisible by k. This operation costs you B coins. 

What is the minimum amount of coins you have to pay to make x equal to 1?
Input

The first line contains a single integer n (1 ≤ n ≤ 2·109).

The second line contains a single integer k (1 ≤ k ≤ 2·109).

The third line contains a single integer A (1 ≤ A ≤ 2·109).

The fourth line contains a single integer B (1 ≤ B ≤ 2·109).
Output

Output a single integer — the minimum amount of coins you have to pay to make x equal to 1.
Examples
Input
Copy

9
2
3
1

Output

6

Input
Copy

5
5
2
20

Output

8

Input
Copy

19
3
4
2

Output

12

Note

In the first testcase, the optimal strategy is as follows:

Subtract 1 from x (9 → 8) paying 3 coins.
Divide x by 2 (8 → 4) paying 1 coin.
Divide x by 2 (4 → 2) paying 1 coin.
Divide x by 2 (2 → 1) paying 1 coin. 

The total cost is 6 coins.

In the second test case the optimal strategy is to subtract 1 from x 4 times paying 8 coins in total.

题意:
&emsp;&emsp;&emsp;给你一个数n,可以进行减一操作和/k操作,代价分别为a和b,问你最少代价是多少
思路:
&emsp;&emsp;&emsp;模拟就行,但是有一些小细节,例如k为1等.
code:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<string>
#include<string.h>
#include<set>
#include<stdlib.h>
#define INF 0x3f3f3f
using namespace std;
typedef long long LL;
LL n, k, a, b,ans;
void ms()
{
    scanf("%lld%lld%lld%lld", &n, &k, &a, &b);
}
void solve()
{
    if (k == 1)
    {
        ans += (n - 1)*a;
        return;
    }
    while (n != 1)
    {
        if (n >= k)
        {
            if (n%k)
            {
                ans += (n%k)*a;
                n -= n%k;
            }
            else
            {
                ans += min(b, (n - n / k)*a);
                n /= k;
            }
        }
        else
        {
            ans += a*(n - 1);
            n = 1;
        }
    }
}
int main()
{
    ms();
    solve();
    cout << ans << endl;
    return 0;
}

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

And where the are the phone numbers?

You are given a string s consisting of lowercase English letters and an integer k. Find the lexicographically smallest string t of length k, such that its set of letters is a subset of the set of letters of s and s is lexicographically smaller than t.

It’s guaranteed that the answer exists.

Note that the set of letters is a set, not a multiset. For example, the set of letters of abadaba is {a, b, d}.

String p is lexicographically smaller than string q, if p is a prefix of q, is not equal to q or there exists i, such that pi < qi and for all j < i it is satisfied that pj = qj. For example, abc is lexicographically smaller than abcd , abd is lexicographically smaller than abec, afa is not lexicographically smaller than ab and a is not lexicographically smaller than a.
Input

The first line of input contains two space separated integers n and k (1 ≤ n, k ≤ 100 000) — the length of s and the required length of t.

The second line of input contains the string s consisting of n lowercase English letters.
Output

Output the string t conforming to the requirements above.

It’s guaranteed that the answer exists.
Examples
Input
Copy

3 3
abc

Output

aca

Input
Copy

3 2
abc

Output

ac

Input
Copy

3 3
ayy

Output

yaa

Input
Copy

2 3
ba

Output

baa

Note

In the first example the list of strings t of length 3, such that the set of letters of t is a subset of letters of s is as follows: aaa, aab, aac, aba, abb, abc, aca, acb, …. Among them, those are lexicographically greater than abc: aca, acb, …. Out of those the lexicographically smallest is aca.

题意:
   找第一个字典序比给定的字符串大的字符串,且长度为k

思路:
   模拟就行- -
code:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<string>
#include<string.h>
#include<set>
#include<stdlib.h>
#define INF 0x3f3f3f
using namespace std;
typedef long long LL;
char s[100010], ans[100010];
int n, k,book[150];
int main()
{
    memset(book, 0, sizeof(book));
    scanf("%d%d", &n, &k);
    scanf("%s", s + 1);
    for (int i = 1; i <= n; i++)
        book[s[i]] = 1;
    int flag = 0;
    int temp = k;
    k = min(n, k);
    int i = k;
    if (temp <= n)
    {
        for (; i >= 1; i--)
        {
            for (int j = s[i] + 1; j <= 'z'; j++)
            {
                if (book[j])
                {
                    ans[i] = j;
                    flag = 1;
                    break;
                }
            }
            if (flag)
                break;
            for (int j = 'a'; j <= 'z'; j++)
            {
                if (book[j])
                {
                    ans[i] = j;
                    break;
                }
            }
        }
        for (int j = 1; j < i; j++)
            ans[j] = s[j];
        if (temp > k)
        {
            for (i = k + 1; i <= temp; i++)
            {
                for (int j = 'a'; j <= 'z'; j++)
                {
                    if (book[j])
                    {
                        ans[i] = j;
                        break;
                    }
                }
            }
        }
    }
    else
    {
        for (int i = 1; i <= n; i++)
            ans[i] = s[i];
        for (int i = n + 1; i <= temp; i++)
            for (int j = 'a'; j <= 'z'; j++)
                if (book[j])
                {
                    ans[i] = j;
                    break;
                }
    }
    ans[temp + 1] = '\0';
    printf("%s\n", ans + 1);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值