Educational Codeforces Round 127 (Rated for Div. 2) C and D

目录

C. Dolce Vita

D. Insert a Progression


C. Dolce Vita

Turbulent times are coming, so you decided to buy sugar in advance. There are nn shops around that sell sugar: the ii-th shop sells one pack of sugar for aiai coins, but only one pack to one customer each day. So in order to buy several packs, you need to visit several shops.

Another problem is that prices are increasing each day: during the first day the cost is aiai, during the second day cost is ai+1ai+1, during the third day — ai+2ai+2 and so on for each shop ii.

On the contrary, your everyday budget is only xx coins. In other words, each day you go and buy as many packs as possible with total cost not exceeding xx. Note that if you don't spend some amount of coins during a day, you can't use these coins during the next days.

Eventually, the cost for each pack will exceed xx, and you won't be able to buy even a single pack. So, how many packs will you be able to buy till that moment in total?

Input

The first line contains a single integer tt (1≤t≤10001≤t≤1000) — the number of test cases. Next tt cases follow.

The first line of each test case contains two integers nn and xx (1≤n≤2⋅1051≤n≤2⋅105; 1≤x≤1091≤x≤109) — the number of shops and your everyday budget.

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the initial cost of one pack in each shop.

It's guaranteed that the total sum of nn doesn't exceed 2⋅1052⋅105.

Output

For each test case, print one integer — the total number of packs you will be able to buy until prices exceed your everyday budget.

input
4
3 7
2 1 2
5 9
10 20 30 40 50
1 1
1
2 1000
1 1
output
11
0
1
1500

#include <iostream>
#include <string>
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>

using namespace std;

const int N = 2e5 + 10;
typedef long long ll;
typedef pair<int, int> PII;
 
int t;
 
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> t;
	while (t--) {
		int n, x;
		ll ans = 0, s = 0;
 
		cin >> n >> x;
		vector<int> a(n);
		for (int i = 0; i < n; i++) {
			cin >> a[i];
		}
		sort(a.begin(), a.end());
		for (int i = 0; i < n; i++) {
			s += a[i];
			if (s <= x) ans += (x - s) / (i + 1) + 1;
		}
		cout << ans << endl;
	}
	return 0;
}

D. Insert a Progression

You are given a sequence of nn integers a1,a2,…,ana1,a2,…,an. You are also given xx integers 1,2,…,x1,2,…,x.

You are asked to insert each of the extra integers into the sequence aa. Each integer can be inserted at the beginning of the sequence, at the end of the sequence, or between any elements of the sequence.

The score of the resulting sequence a′a′ is the sum of absolute differences of adjacent elements in it (∑i=1n+x−1|a′i−a′i+1|)(∑i=1n+x−1|ai′−ai+1′|).

What is the smallest possible score of the resulting sequence a′a′?

Input

The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of testcases.

The first line of each testcase contains two integers nn and xx (1≤n,x≤2⋅1051≤n,x≤2⋅105) — the length of the sequence and the number of extra integers.

The second line of each testcase contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤2⋅1051≤ai≤2⋅105).

The sum of nn over all testcases doesn't exceed 2⋅1052⋅105.

Output

For each testcase, print a single integer — the smallest sum of absolute differences of adjacent elements of the sequence after you insert the extra integers into it.

input

4
1 5
10
3 8
7 2 10
10 2
6 1 5 7 3 3 9 10 10 1
4 10
1 3 1 2

output

9
15
31
13

#include <iostream>
#include <string>
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
using namespace std;
const int N = 2e5 + 10;
typedef long long ll;
typedef pair<int, int> PII;
 
int t;
int n,x;
 
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin>>t;
	while(t--)
	{
		cin>>n>>x;
		vector<int> a(n);
		for(int i=0;i<n;i++)
		{
			cin>>a[i];
		}
		vector<int> b=a;            
		sort(b.begin(),b.end()); //找出最大最小
		ll ans=0;
		for(int i=1;i<n;i++) 
		{
			ans+=abs(a[i]-a[i-1]);  
		}
        //比较a中最大数的位置(第一个,中间,最后一个)
        //x大于a中最大值时
		if(x>b.back()) ans+=min({x-a.back(),x-a[0],2*(x-b.back())});
		//当a中最小值大于1时
        if(1<b[0]) ans+=min({a[0]-1,a.back()-1,2*(b[0]-1)});
		cout<<ans<<endl;
	}
	return 0;
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

'<王半仙>'

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值