题解:Codeforces Round 961 (Div. 2) B1&B2

#本题含有B1和B2两个难度版本。由于关系相近,我把他们放在同一篇博客中,可自行选择观看

B1. Bouquet (Easy Version)

time limit per test: 1.5 seconds

memory limit per test: 512 megabytes

input: standard input

output: standard output

This is the easy version of the problem. The only difference is that in this version, the flowers are specified by enumeration.

A girl is preparing for her birthday and wants to buy the most beautiful bouquet. There are a total of n n n flowers in the store, each of which is characterized by the number of petals, and a flower with k k k petals costs k k k coins. The girl has decided that the difference in the number of petals between any two flowers she will use in her bouquet should not exceed one. At the same time, the girl wants to assemble a bouquet with the maximum possible number of petals. Unfortunately, she only has m m m coins, and she cannot spend more. What is the maximum total number of petals she can assemble in the bouquet?

这是问题的简单版本。唯一不同的是,在这个版本中,花朵是通过枚举来指定的

一个女孩准备过生日,她想买一束最漂亮的花。商店里一共有 n n n 种花,每种花的特征是花瓣数,有 k k k 个花瓣的花需要花费 k k k 个硬币。女孩决定,花束中任何两朵花的花瓣数之差都不能超过 1。同时,女孩希望花束的花瓣数尽可能多。不幸的是,她只有 m m m 枚金币,无法再花费更多。她能组合的花束的花瓣总数最多是多少?

Input

Each test consists of several test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 10   000 1 \le t \le 10\,000 1t10000) — the number of test cases. This is followed by descriptions of the test cases.

The first line of each test case contains two integers n n n, m m m ( 1 ≤ n ≤ 2 ⋅ 1 0 5 , 1 ≤ m ≤ 1 0 18 1 \le n \le 2 \cdot 10^5, 1 \le m \le 10^{18} 1n2105,1m1018) — the number of flowers in the store and the number of coins the girl possesses, respectively. The second line of each test case contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an ( 1 ≤ a i ≤ 1 0 9 1 \le a_i \le 10^9 1ai109), where a i a_i ai is the number of petals of the i i i-th flower in the store.

The sum of n n n over all test cases does not exceed 2 ⋅ 10 5 2 \cdot {10}^5 2105.

输入

每个测试由多个测试用例组成。第一行包含一个整数 t t t ( 1 ≤ t ≤ 10   000 1 \le t \le 10\,000 1t10000 ) - 测试用例数。随后是测试用例的说明。

每个测试用例的第一行包含两个整数 n n n , m m m ( 1 ≤ n ≤ 2 ⋅ 1 0 5 , 1 ≤ m ≤ 1 0 18 1 \le n \le 2 \cdot 10^5, 1 \le m \le 10^{18} 1n2105,1m1018 )–分别是商店里的鲜花数量和女孩拥有的硬币数量。每个测试用例的第二行包含 n n n 个整数 a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an ( 1 ≤ a i ≤ 1 0 9 1 \le a_i \le 10^9 1ai109 ),其中 a i a_i ai 是商店里第 i i i 朵花的花瓣数。

所有测试用例的 n n n 之和不超过 2 ⋅ 10 5 2 \cdot {10}^5 2105

Output

For each test case, output a single integer — the maximum possible number of petals in the bouquet that the girl can assemble while meeting all the conditions listed above.

输出

对于每个测试用例,输出一个整数,即在满足上述所有条件的情况下,女孩在花束中可能组合的最大花瓣数。

Example
input

5
5 10
1 1 2 2 3
8 20
4 2 7 5 6 1 1 1
8 100000
239 30 610 122 24 40 8 2
11 13
2 4 11 1 1 2 3 5 4 3 2
8 1033
206 206 206 207 207 207 207 1000

output

7
13
610
13
1033

Note

In the first test case, you can assemble a bouquet with ( 1 , 1 , 2 , 2 ) , ( 2 , 2 , 3 ) , ( 1 , 1 ) , ( 2 , 2 ) (1, 1, 2, 2), (2, 2, 3), (1, 1), (2, 2) (1,1,2,2),(2,2,3),(1,1),(2,2). The maximum over all valid bouquets not greater than 10 10 10 is 7 7 7 for ( 2 , 2 , 3 ) (2, 2, 3) (2,2,3). In the third test case, you can assemble a bouquet with only one flower of any type, so the answer is 610 610 610. In the fourth test case, you can assemble a bouquet with ( 4 , 4 , 5 ) (4, 4, 5) (4,4,5), which gives you 13 13 13 petals, and it is the maximum amount of petals that the girl can buy.

在第一个测试用例中,可以用 ( 1 , 1 , 2 , 2 ) , ( 2 , 2 , 3 ) , ( 1 , 1 ) , ( 2 , 2 ) (1, 1, 2, 2), (2, 2, 3), (1, 1), (2, 2) (1,1,2,2),(2,2,3),(1,1),(2,2) 组合一个花束。所有不大于 10 10 10 的有效花束的最大值是 7 7 7 ,即 ( 2 , 2 , 3 ) (2, 2, 3) (2,2,3) 。在第三个测试案例中,你只能用一朵任意类型的花来组合花束,因此答案是 610 610 610 。在第四个测试案例中,你可以用 ( 4 , 4 , 5 ) (4, 4, 5) (4,4,5) 组合一束花,这样就可以得到 13 13 13 个花瓣,这也是女孩可以购买的最大花瓣数量。

题意
对每一个样例
题目会

  1. 给你一个n,代表有多少朵花
  2. 给你一个k,代表你需要有多少个花瓣(不能超过这个数)
  3. 给你n个数,代表有n朵花,每一个数代表这朵花有多少个花瓣

你有一个特点,你手中每朵花的花瓣数量只能有两种,且这两种花瓣数量相差不能超过1
求你能得到的最大的花瓣数量

我的题解
第一题是简单版本,给的数据量比较小
我们可以先排一下序,然后用 双指针算法 模拟 取值

后面的指针指向的值前面的值加1 要大,就把前面的指针不断往后挪
每次更新区间和

每次对区间和做一个判断

  • 当区间和大于 k k k 时,就把前指针往后挪(减少区间和)
  • 否则,就把后指针往后挪(增大区间和)

每次循环都把区间和与答案作比较,取最大值更新答案
循环结束之后输出答案即可

我的代码

#include <bits/stdc++.h>
#define int long long

const int N = 2e5+10;
int t,n,k;
int num;
int a[N];

void solve() {
    int ans =  0;
    int tem;
    std::cin >> n >> k;
    for(int i = 0 ; i < n ; i ++) std::cin >> a[i];
    std::sort(a,a+n);
    int st = 0,en = 0;
    tem = a[0];
    while(st <= en && en < n) {
        while(a[st] +1 < a[en]) {
            tem -= a[st];
            st ++;
        }

        if(tem > k) {
            tem -= a[st];
            st ++;
        } else {
            ans = std::max(ans,tem);
            en ++;
            tem += (en < n ? a[en] : 0);
        }
    }

    std::cout << ans << "\n";
}

signed main() {
    std::cin >> t;
    while(t--) solve();
    return 0;
}

B2. Bouquet (Hard Version)

time limit per test: 1.5 seconds

memory limit per test: 256 megabytes

input: standard input

output: standard output

This is the hard version of the problem. The only difference is that in this version, instead of listing the number of petals for each flower, the number of petals and the quantity of flowers in the store is set for all types of flowers.

A girl is preparing for her birthday and wants to buy the most beautiful bouquet. There are a total of n n n different types of flowers in the store, each of which is characterized by the number of petals and the quantity of this type of flower. A flower with k k k petals costs k k k coins. The girl has decided that the difference in the number of petals between any two flowers she will use to decorate her cake should not exceed one. At the same time, the girl wants to assemble a bouquet with the maximum possible number of petals. Unfortunately, she only has m m m coins, and she cannot spend more. What is the maximum total number of petals she can assemble in the bouquet?

**这是问题的困难版本。唯一不同的是,在这个版本中,不是列出每种花的花瓣数,而是为所有类型的花设定花瓣数和商店中的花的数量。

一个女孩准备过生日,想买一束最漂亮的花。店里一共有 n n n 种不同类型的花,每种花的花瓣数和数量都有相应的特征。一朵有 k k k 个花瓣的花的价格是 k k k 个金币。女孩决定,她用来装饰蛋糕的任何两朵花的花瓣数之差都不能超过 1。同时,女孩还想用尽可能多的花瓣组合成一束花。不幸的是,她只有 m m m 枚金币,无法再花费更多。她能组合的花束花瓣总数最多是多少?

Input

Each test consists of several test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 10   000 1 \le t \le 10\,000 1t10000) — the number of test cases. This is followed by descriptions of the test cases.

The first line of each test case contains two integers n n n, m m m ( 1 ≤ n ≤ 2 ⋅ 1 0 5 , 1 ≤ m ≤ 1 0 18 1 \le n \le 2 \cdot 10^5, 1 \le m \le 10^{18} 1n2105,1m1018) — the number of types of flowers in the store and the number of coins the girl possesses, respectively. The second line of each test case contains n n n different integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an ( 1 ≤ a i ≤ 1 0 9 1 \le a_i \le 10^9 1ai109), where a i a_i ai is the number of petals of the i i i-th flower type in the store (for different indexes i ≠ j i \neq j i=j, it must be a i ≠ a j a_i \neq a_j ai=aj). The third line of each test case contains n n n integers c 1 , c 2 , … , c n c_1, c_2, \ldots, c_n c1,c2,,cn ( 1 ≤ c i ≤ 1 0 9 1 \le c_i \le 10^9 1ci109), where c i c_i ci is the quantity of the i i i-th flower type in the store.

The sum of n n n over all test cases does not exceed 2 ⋅ 10 5 2 \cdot {10}^5 2105.

输入

每个测试由多个测试用例组成。第一行包含一个整数 t t t ( 1 ≤ t ≤ 10   000 1 \le t \le 10\,000 1t10000 ) - 测试用例数。随后是测试用例的说明。

每个测试用例的第一行包含两个整数 n n n , m m m ( 1 ≤ n ≤ 2 ⋅ 1 0 5 , 1 ≤ m ≤ 1 0 18 1 \le n \le 2 \cdot 10^5, 1 \le m \le 10^{18} 1n2105,1m1018 ) - 分别是商店中鲜花的种类数和女孩拥有的硬币数。每个测试用例的第二行包含 n n n 不同的整数 a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an 1 ≤ a i ≤ 1 0 9 1 \le a_i \le 10^9 1ai109 )。( 1 ≤ a i ≤ 1 0 9 1 \le a_i \le 10^9 1ai109 ),其中 a i a_i ai 是商店中 i i i /th花朵类型的花瓣数(对于不同的索引 i ≠ j i \neq j i=j ,必须是 a i ≠ a j a_i \neq a_j ai=aj )。每个测试用例的第三行包含 n n n 个整数 c 1 , c 2 , … , c n c_1, c_2, \ldots, c_n c1,c2,,cn ( 1 ≤ c i ≤ 1 0 9 1 \le c_i \le 10^9 1ci109 ),其中 c i c_i ci 是商店中 i i i -th 鲜花类型的数量。

所有测试用例中 n n n 的总和不超过 2 ⋅ 10 5 2 \cdot {10}^5 2105

Output

For each test case, print one integer — the maximum possible number of petals in a bouquet that a girl can collect, observing all the conditions listed above.

输出

对于每个测试用例,打印一个整数 - 在遵守上述所有条件的情况下,女孩在花束中可能收集到的最大花瓣数。

Example
input

7
3 10
1 2 3
2 2 1
3 1033
206 207 1000
3 4 1
6 20
4 2 7 5 6 1
1 2 1 3 1 7
8 100000
239 30 610 122 24 40 8 2
12 13123 112 1456 124 100 123 10982
6 13
2 4 11 1 3 5
2 2 1 2 2 1
8 10330
206 210 200 201 198 199 222 1000
9 10 11 12 13 14 15 16
2 10000000000
11 12
87312315 753297050

output

7
1033
19
99990
13
10000
9999999999

Note

In the first test case, some valid bouquets are ( 1 , 1 , 2 , 2 ) , ( 2 , 2 , 3 ) , ( 1 , 1 ) , ( 2 , 2 ) (1, 1, 2, 2), (2, 2, 3), (1, 1), (2, 2) (1,1,2,2),(2,2,3),(1,1),(2,2). The maximum over all valid bouquets not greater than 10 10 10 is 7 7 7 for ( 2 , 2 , 3 ) (2, 2, 3) (2,2,3). In the second test case, you can assemble a valid bouquet with ( 206 , 206 , 207 , 207 , 207 ) (206, 206, 207, 207, 207) (206,206,207,207,207) with a sum of 1033 1033 1033, which is the maximum number of petals the girl can buy. In the third test case, you can assemble a valid bouquet with ( 5 , 5 , 5 , 4 ) (5, 5, 5, 4) (5,5,5,4) with a sum of 19 19 19. It can be seen that no valid bouquet can have 20 20 20 petals.

在第一个测试用例中,一些有效花束为 ( 1 , 1 , 2 , 2 ) , ( 2 , 2 , 3 ) , ( 1 , 1 ) , ( 2 , 2 ) (1, 1, 2, 2), (2, 2, 3), (1, 1), (2, 2) (1,1,2,2),(2,2,3),(1,1),(2,2) 。不大于 10 10 10 的所有有效花束的最大值是 ( 2 , 2 , 3 ) (2, 2, 3) (2,2,3) 7 7 7 。在第二个测试案例中,可以用 ( 206 , 206 , 207 , 207 , 207 ) (206, 206, 207, 207, 207) (206,206,207,207,207) 组合出一个有效花束,花束总数为 1033 1033 1033 ,这是女孩可以购买的最大花瓣数。在第三个测试用例中,可以用 ( 5 , 5 , 5 , 4 ) (5, 5, 5, 4) (5,5,5,4) 19 19 19 组合出一束有效的花束。由此可见,任何有效花束都不可能有 20 20 20 个花瓣。

题意
对每一个样例
题目会

  1. 给你一个n,代表有多少种花
  2. 给你一个k,代表你需要有多少个花瓣(不能超过这个数)
  3. 给你n个数,代表有每一种花有多少个花瓣
  4. 给你n个数,代表相对应的每一种花有多少朵

你有一个特点,你手中每朵花的花瓣数量只能有两种,且这两种花瓣数量相差不能超过1
求你能得到的最大的花瓣数量

我的题解
这题是困难版本
我们可以用计数数组来模拟这个双指针算法的过程以节省内存空间
当然也可以用pair存储,我用的是map存储

我的做法是计数存储之后,
声明一个关于这个map的迭代器,反向遍历
假设迭代器迭代到取花瓣数 a a a
每次遍历都只取 a a a a + 1 a+1 a+1 的花瓣数

每次计算,假设迭代器迭代到取花瓣数 a a a 和花瓣数 b ( b = a + 1 ) b(b = a + 1) b(b=a+1)
先比较 a a a b b b 总数和 k k k 的大小

  • 如果小于等于 k k k,那就全部取
  • 如果比 k k k
    1. 假设全部取 a a a ,会有两种情况
      • 全部取 a a a 之后手上的花瓣数不超过 k k k ,那就再从 b b b 那里拿一些花瓣(假设取 m m m 朵,使得手上的花瓣数最大且不超过 k k k
      • 全部取 a a a 之后手上的花瓣数超过 k k k ,那就放回部分 a a a ,使得手上的花瓣数不大于 k k k
    2. 然后,我们计算最多能把多少朵手上的a换成b(因为每换一朵花瓣数就加 1 1 1),这个数量 a d d = m i n ( 剩下的 b ,手中的 a ) add = min(剩下的b,手中的a) add=min(剩下的b,手中的a)
      也就是 a d d = m i n ( b 的总数 − m ,手中的 a ) add = min(b的总数 - m,手中的a) add=min(b的总数m,手中的a)
    3. 再把手上的花瓣数加上 a d d add add,和目前的结果比较,取最大值更新结果

输出最后的结果即可
同样的逻辑套用在B1也可以通过

我的代码

#include <bits/stdc++.h>
#define int long long

const int N = 2e5+10;
int t,n,k;
int num;
int a[N];

int fin(std::map<int,int> &mp,std::map<int,int>::iterator &poi) {
    int bi = poi->first+1;
    int teme = poi->second * poi->first + mp[bi] * bi;
    int ans_t;

    if(teme > k) {
        ans_t = std::min(k/poi->first,poi->second) * poi->first;
        int add_m = std::min((k-ans_t)/bi,mp[bi]);
        ans_t += add_m*bi;

        int add_t = std::min(std::min(k/poi->first,poi->second),mp[bi]-add_m);
        ans_t += std::min(k-ans_t,add_t);
        ans_t = std::max(ans_t,std::min(k/bi , mp[bi]) * bi);
        return ans_t;
    } else {
        return teme;
    }
}

void solve() {
    int ans =  0;

    std::cin >> n >> k;
    std::map<int,int> mp;
    for(int i = 0 ; i < n ; i ++) {
        std::cin >> a[i];
    }
    for(int i = 0 ; i < n ;i ++) {
        std::cin >> mp[a[i]];
    }

    std::map<int,int>::iterator poi = mp.end();
    poi --;

    while(poi != mp.begin() && ans < k) {
        ans = std::max(ans,fin(mp,poi));
        poi --;
    }

    ans = std::max(ans,fin(mp,poi));

    std::cout << ans << "\n";
}

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);

    std::cin >> t;
    while(t--) solve();
    return 0;
}

转载自博客https://www.cnblogs.com/jiejiejiang2004/p/18322498
博主已同意,我就是博主

  • 16
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抱歉,根据提供的引用内容,我无法理解你具体想要问什么问题。请提供更清晰明确的问题,我将竭诚为你解答。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Codeforces Round 860 (Div. 2)题解](https://blog.csdn.net/qq_60653991/article/details/129802687)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [【CodeforcesCodeforces Round 865 (Div. 2) (补赛)](https://blog.csdn.net/t_mod/article/details/130104033)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Codeforces Round 872 (Div. 2)(前三道](https://blog.csdn.net/qq_68286180/article/details/130570952)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值