Codeforces Round #609 (Div. 2)


A. Temporarily unavailable


time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Polycarp lives on the coordinate axis Ox and travels from the point x=a to x=b. It moves uniformly rectilinearly at a speed of one unit of distance per minute.

On the axis Ox at the point x=c the base station of the mobile operator is placed. It is known that the radius of its coverage is r. Thus, if Polycarp is at a distance less than or equal to r from the point x=c, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.

Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from x=a to x=b. His speed — one unit of distance per minute.

Input
The first line contains a positive integer t (1≤t≤1000) — the number of test cases. In the following lines are written t test cases.

The description of each test case is one line, which contains four integers a, b, c and r (−108≤a,b,c≤108, 0≤r≤108) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively.

Any of the numbers a, b and c can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.

Output
Print t numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.

画个坐标轴慢慢分类讨论,官方题解更好

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve()
{
	ll a,b,c,r;
	scanf("%lld%lld%lld%lld",&a,&b,&c,&r);
	if(b<a)
	swap(a,b);
	if(b<c-r||a>c+r)
	{
		printf("%lld\n",b-a);
		return;
	}
	if(b<=c+r&&a<=c-r)
	{
	printf("%lld\n",b-a-(b-c+r));
	return;
	}
	if(b<=c+r&&a>c-r)
	{
		printf("0\n");
		return;
	}
	if(b>=c+r&&a<=c-r)
	{
		printf("%lld\n",(b-a)-2*r);
		return;
	}
	if(b>=c+r&&a>c-r)
	{
		printf("%lld\n",b-(c+r));
		return;
	}
}
int main()
{
	/*
	freopen("da ta.in","r",stdin);
	freopen("data.out","w",stdout);
	*/
	int t; 
	scanf("%d",&t);
	while(t--)
	
    	solve();
    
    return 0;
}

官方题解

#include <bits/stdc++.h>

using namespace std;

#define forn(i, n) for (int i = 0; i < int(n); i++)

int main() {
    int t;
    cin >> t;
    forn(tt, t) {
        int a, b, c, r;
        cin >> a >> b >> c >> r;
        int L = max(min(a, b), c - r);
        int R = min(max(a, b), c + r);
        cout << max(a, b) - min(a, b) - max(0, R - L) << endl;
    }
}

B2. K for the Price of One (Hard Version)


time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
This is the hard version of this problem. The only difference is the constraint on k — the number of gifts in the offer. In this version: 2≤k≤n.

Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky — today the offer “k of goods for the price of one” is held in store.

Using this offer, Vasya can buy exactly k of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has.

More formally, for each good, its price is determined by ai — the number of coins it costs. Initially, Vasya has p coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary:

Vasya can buy one good with the index i if he currently has enough coins (i.e p≥ai). After buying this good, the number of Vasya’s coins will decrease by ai, (i.e it becomes p:=p−ai).
Vasya can buy a good with the index i, and also choose exactly k−1 goods, the price of which does not exceed ai, if he currently has enough coins (i.e p≥ai). Thus, he buys all these k goods, and his number of coins decreases by ai (i.e it becomes p:=p−ai).
Please note that each good can be bought no more than once.

For example, if the store now has n=5 goods worth a1=2,a2=4,a3=3,a4=5,a5=7, respectively, k=2, and Vasya has 6 coins, then he can buy 3 goods. A good with the index 1 will be bought by Vasya without using the offer and he will pay 2 coins. Goods with the indices 2 and 3 Vasya will buy using the offer and he will pay 4 coins. It can be proved that Vasya can not buy more goods with six coins.

Help Vasya to find out the maximum number of goods he can buy.

Input
The first line contains one integer t (1≤t≤104) — the number of test cases in the test.

The next lines contain a description of t test cases.

The first line of each test case contains three integers n,p,k (2≤n≤2⋅105, 1≤p≤2⋅109, 2≤k≤n) — the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them.

The second line of each test case contains n integers ai (1≤ai≤104) — the prices of goods.

It is guaranteed that the sum of n for all test cases does not exceed 2⋅105.

Output
For each test case in a separate line print one integer m — the maximum number of goods that Vasya can buy.

做法:贪心,易证得,能用优惠政策的时候一定用,一个物品的钱买两个血赚。但便宜的物品最好先买了,后面贵的物品再每k个用优惠策略。所以枚举从前k-1个物品开始要优惠政策便可。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int price[200005];
void solve()
{
		ll n,p,k,pp;
		scanf("%lld%lld%lld",&n,&p,&k);
		for(int i=0;i<n;i++)
		scanf("%d",&price[i]);
		sort(price, price+n);
		int maxI = -1;
		int startSum = 0;
		for(int o=0; o<k; o++)
		{
			int money = p;
			if(o<k-1)
			{
				money -= startSum;
				if(money < 0) 
					continue;
				startSum += price[o];
			}
 
			for(int j=o; j<n; j+=k)
			{
				if(price[j] > money) 
					break;
				money -= price[j];
				if(j>maxI) maxI = j;
			}
		}
		printf("%d\n",maxI+1);
}
int main()
{
	/*
	freopen("da ta.in","r",stdin);
	freopen("data.out","w",stdout);
	*/
	ll t; 
	scanf("%lld",&t);
	while(t--)
    	solve();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值