CF专题(长安大学)

来暂时总结下这几天的CF专题吧…后续还会更新…

A - Ichihime and Triangle:

Ichihime is the current priestess of the Mahjong Soul Temple. She claims to be human, despite her cat ears.

These days the temple is holding a math contest. Usually, Ichihime lacks interest in these things, but this time the prize for the winner is her favorite — cookies. Ichihime decides to attend the contest. Now she is solving the following problem.

You are given four positive integers a, b, c, d, such that a≤b≤c≤d.

Your task is to find three integers x, y, z, satisfying the following conditions:

a≤x≤b.
b≤y≤c.
c≤z≤d.
There exists a triangle with a positive non-zero area and the lengths of its three sides are x, y, and z.
Ichihime desires to get the cookie, but the problem seems too hard for her. Can you help her?

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

The next t lines describe test cases. Each test case is given as four space-separated integers a, b, c, d (1≤a≤b≤c≤d≤109).

Output
For each test case, print three integers x, y, z — the integers you found satisfying the conditions given in the statement.

It is guaranteed that the answer always exists. If there are multiple answers, print any.

Example
Input
4
1 3 5 7
1 5 5 7
100000 200000 300000 400000
1 1 977539810 977539810
Output
3 4 5
5 5 5
182690 214748 300999
1 977539810 977539810

没啥好说的,就是输出b,c,c就行了…

B - Dawid and Bags of Candies

Dawid has four bags of candies. The i-th of them contains ai candies. Also, Dawid has two friends. He wants to give each bag to one of his two friends. Is it possible to distribute the bags in such a way that each friend receives the same amount of candies in total?

Note, that you can’t keep bags for yourself or throw them away, each bag should be given to one of the friends.

Input
The only line contains four integers a1, a2, a3 and a4 (1≤ai≤100) — the numbers of candies in each bag.

Output
Output YES if it’s possible to give the bags to Dawid’s friends so that both friends receive the same amount of candies, or NO otherwise. Each character can be printed in any case (either uppercase or lowercase).

Examples
Input
1 7 11 5
Output
YES
Input
7 3 2 5
Output
NO
Note
In the first sample test, Dawid can give the first and the third bag to the first friend, and the second and the fourth bag to the second friend. This way, each friend will receive 12 candies.

In the second sample test, it’s impossible to distribute the bags.

这道题主要是容易忽略掉3:1的情况,很多人只考虑到2:2,但是没有考虑到3:1
同时这两种不是else if 的关系,像我这么写是过不了的:

#include<iostream>
#include<algorithm>
using namespace std;
int a[4];
int main(){
		for(int i=0;i<4;i++)
		cin>>a[i];
		sort(a,a+4);
		if(a[3]+a[0]==a[1]+a[2])
		cout<<"YES"<<endl;
		else if(a[3]=a[0]+a[1]+a[2])
		cout<<"YES"<<endl;
		else
		cout<<"NO"<<endl;
}

换做或语言就对了:

#include<iostream>
#include<algorithm>
using namespace std;
int a[4];
int main(){
		for(int i=0;i<4;i++)
		cin>>a[i];
		sort(a,a+4);
		if(a[3]+a[0]==a[1]+a[2]||a[0]+a[1]+a[2]==a[3])
		cout<<"YES"<<endl;
		else
		cout<<"NO"<<endl;
}

C - Suits

A new delivery of clothing has arrived today to the clothing store. This delivery consists of a ties, b scarves, c vests and d jackets.

The store does not sell single clothing items — instead, it sells suits of two types:

a suit of the first type consists of one tie and one jacket;
a suit of the second type consists of one scarf, one vest and one jacket.
Each suit of the first type costs e coins, and each suit of the second type costs f coins.

Calculate the maximum possible cost of a set of suits that can be composed from the delivered clothing items. Note that one item cannot be used in more than one suit (though some items may be left unused).

Input
The first line contains one integer a (1≤a≤100000) — the number of ties.

The second line contains one integer b (1≤b≤100000) — the number of scarves.

The third line contains one integer c (1≤c≤100000) — the number of vests.

The fourth line contains one integer d (1≤d≤100000) — the number of jackets.

The fifth line contains one integer e (1≤e≤1000) — the cost of one suit of the first type.

The sixth line contains one integer f (1≤f≤1000) — the cost of one suit of the second type.

Output
Print one integer — the maximum total cost of some set of suits that can be composed from the delivered items.

Examples
Input
4
5
6
3
1
2
Output
6
Input
12
11
13
20
4
6
Output
102
Input
17
14
5
21
15
17
Output
325
Note
It is possible to compose three suits of the second type in the first example, and their total cost will be 6. Since all jackets will be used, it’s impossible to add anything to this set.

The best course of action in the second example is to compose nine suits of the first type and eleven suits of the second type. The total cost is 9⋅4+11⋅6=102.

简单的分情况的问题.用高级点的语言就是,使用贪心算法.判断e与f哪个大,大的就优先拿jackets去凑,然后再比较jacket与其他方案里面的其他物件谁更小,若jacket更小就直接输出e*d就行了,以此类推.看看代码吧,反正一次过.

#include<iostream>
#include<math.h>
using namespace std;
int main(){
	int a,b,c,d,e,f;
	cin>>a>>b>>c>>d>>e>>f;
	int min1=a>d?d:a;
	int min2=(b>c?c:b)>d?d:(b>c?c:b);
	if(e>f){
		if(min1==d)
		cout<<e*d<<endl;
		else{
			if((d-a)<=min2)
			cout<<a*e+(d-a)*f<<endl;
			else
			cout<<a*e+min2*f<<endl;
		}
	}
	else{
		if(min2==d)
		cout<<f*d<<endl;
		else{
			if(d-min2<=min1)
			cout<<min2*f+(d-min2)*e<<endl;
			else
			cout<<min2*f+e*min1<<endl;
		}
	}
}

D - Ania and Minimizing

У Ани есть большое число S. Десятичная запись этого числа состоит из n цифр и не содержит ведущих нулей. Аня может изменить не более k цифр в S. Она хочет это сделать так, чтобы S все еще не содержало ведущих нулей и было как можно меньше. Какое число получится у Ани в итоге?

Input
В первой строке записаны два целых числа n и k (1≤n≤200000, 0≤k≤n) — количество цифр в десятичной записи S и максимальное разрешенное количество измененных цифр

Во второй строке записано целое число S. Гарантируется, что S состоит ровно из n цифр и не содержит никаких ведущих нулей.

Output
Выведите минимальное возможное число S, которое может получиться у Ани. Обратите внимание, что у полученного числа должно быть ровно n цифр.

Examples
Input
5 3
51528
Output
10028
Input
3 2
102
Output
100
Input
1 1
1
Output
0
Note
A number has leading zeroes if it consists of at least two digits and its first digit is 0. For example, numbers 00, 00069 and 0101 have leading zeroes, while 0, 3000 and 1010 don’t have leading zeroes.

** 要使得这个输出最小,最好就是第一位为1,剩下的都尽量为0.因此算法就显而易见了.若第一个不为1,就让他变成1,剩下的不为0就让它变成0.**
** 但是我在做题的时候犯下了个致命错误,也许是以前的题目导致我的惯性思维,我误以为改变的数据是需要连在一起的,这导致我一直都AC不了.后面还是学长指点了才知道.
同时,还得注意n=1的情况,n=1的时候所有的数都要变成0,而不是1,这点也容易忘记.**

亮代码:

#include <iostream>
#include <string.h>
#include<string>
using namespace std;
string s;
int main()
{
	int n,k,i=0,j=0;
	cin>>n>>k>>s;
	for(i=0;i<n;i++){
		if(j<k&&k>0){
			if(s[0]!='1'&&i==0)
			{
				s[i]='1';
				j++;
			}
			else if(i!=0&&s[i]!='0')
			{
				s[i]='0';
				j++;
			}
			if (n==1)
			s[i]='0';
		}
		else
		{break;}
	}
	cout<<s<<endl;
}

E - Emotes

There are n emotes in very popular digital collectible card game (the game is pretty famous so we won’t say its name). The i-th emote increases the opponent’s happiness by ai units (we all know that emotes in this game are used to make opponents happy).

You have time to use some emotes only m times. You are allowed to use any emotion once, more than once, or not use it at all. The only restriction is that you cannot use the same emote more than k times in a row (otherwise the opponent will think that you’re trolling him).

Note that two emotes i and j (i≠j) such that ai=aj are considered different.

You have to make your opponent as happy as possible. Find the maximum possible opponent’s happiness.

Input
The first line of the input contains three integers n,m and k (2≤n≤2⋅105, 1≤k≤m≤2⋅109) — the number of emotes, the number of times you can use emotes and the maximum number of times you may use the same emote in a row.

The second line of the input contains n integers a1,a2,…,an (1≤ai≤109), where ai is value of the happiness of the i-th emote.

Output
Print one integer — the maximum opponent’s happiness if you use emotes in a way satisfying the problem statement.

Examples
Input
6 9 2
1 3 3 7 4 2
Output
54
Input
3 1000000000 1
1000000000 987654321 1000000000
Output
1000000000000000000
Note
In the first example you may use emotes in the following sequence: 4,4,5,4,4,5,4,4,5.

简单题,将k个最大值和一个次大值作为一组,看看能循环多少次,循环不了的剩下的次数就全都用来发最大值的表情.

#include<iostream>
#include<algorithm>
using namespace std;
int a[1010000];
int main(){
	long long int n,m,k;
	cin>>n>>m>>k;
	for(int i=0;i<n;i++)
	cin>>a[i];
	sort(a,a+n);
	long long int b=m/(k+1);
	long long int c=m%(k+1);
	cout<<b*(k*a[n-1]+a[n-2])+c*a[n-1]<<endl;
}

F - Big Segment

A coordinate line has n segments, the i-th segment starts at the position li and ends at the position ri. We will denote such a segment as [li, ri].

You have suggested that one of the defined segments covers all others. In other words, there is such segment in the given set, which contains all other ones. Now you want to test your assumption. Find in the given set the segment which covers all other segments, and print its number. If such a segment doesn’t exist, print -1.

Formally we will assume that segment [a, b] covers segment [c, d], if they meet this condition a ≤ c ≤ d ≤ b.

Input
The first line contains integer n (1 ≤ n ≤ 105) — the number of segments. Next n lines contain the descriptions of the segments. The i-th line contains two space-separated integers li, ri (1 ≤ li ≤ ri ≤ 109) — the borders of the i-th segment.

It is guaranteed that no two segments coincide.

Output
Print a single integer — the number of the segment that covers all other segments in the set. If there’s no solution, print -1.

The segments are numbered starting from 1 in the order in which they appear in the input.

Examples
Input
3
1 1
2 2
3 3
Output
-1
Input
6
1 5
2 3
1 10
7 10
7 7
10 10
Output
3

大意是找下有没有一个区间能够将所以区间全部包住,那就在输入的时候找下最大值与最小值,输入完后通过一次循环

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值