CODEFORCES ROUND #643 (DIV. 2)

http://www.yyycode.cn/index.php/2020/05/17/codeforces-round-643-div-2/


A. Sequence with Digits

Let’s define the following recurrence:an+1=an+minDigit(an)⋅maxDigit(an).an+1=an+minDigit(an)⋅maxDigit(an).

Here minDigit(x)minDigit(x) and maxDigit(x)maxDigit(x) are the minimal and maximal digits in the decimal representation of xx without leading zeroes. For examples refer to notes.

Your task is calculate aKaK for given a1a1 and KK.Input

The first line contains one integer tt (1≤t≤10001≤t≤1000) — the number of independent test cases.

Each test case consists of a single line containing two integers a1a1 and KK (1≤a1≤10181≤a1≤1018, 1≤K≤10161≤K≤1016) separated by a space.Output

For each test case print one integer aKaK on a separate line.ExampleinputCopy

8
1 4
487 1
487 2
487 3
487 4
487 5
487 6
487 7

outputCopy

42
487
519
528
544
564
588
628

Note

a1=487a1=487

a2=a1+minDigit(a1)⋅maxDigit(a1)=487+min(4,8,7)⋅max(4,8,7)=487+4⋅8=519a2=a1+minDigit(a1)⋅maxDigit(a1)=487+min(4,8,7)⋅max(4,8,7)=487+4⋅8=519

a3=a2+minDigit(a2)⋅maxDigit(a2)=519+min(5,1,9)⋅max(5,1,9)=519+1⋅9=528a3=a2+minDigit(a2)⋅maxDigit(a2)=519+min(5,1,9)⋅max(5,1,9)=519+1⋅9=528

a4=a3+minDigit(a3)⋅maxDigit(a3)=528+min(5,2,8)⋅max(5,2,8)=528+2⋅8=544a4=a3+minDigit(a3)⋅maxDigit(a3)=528+min(5,2,8)⋅max(5,2,8)=528+2⋅8=544

a5=a4+minDigit(a4)⋅maxDigit(a4)=544+min(5,4,4)⋅max(5,4,4)=544+4⋅5=564a5=a4+minDigit(a4)⋅maxDigit(a4)=544+min(5,4,4)⋅max(5,4,4)=544+4⋅5=564

a6=a5+minDigit(a5)⋅maxDigit(a5)=564+min(5,6,4)⋅max(5,6,4)=564+4⋅6=588a6=a5+minDigit(a5)⋅maxDigit(a5)=564+min(5,6,4)⋅max(5,6,4)=564+4⋅6=588

a7=a6+minDigit(a6)⋅maxDigit(a6)=588+min(5,8,8)⋅max(5,8,8)=588+5⋅8=628a7=a6+minDigit(a6)⋅maxDigit(a6)=588+min(5,8,8)⋅max(5,8,8)=588+5⋅8=628


题意:给一个数,下一个数为这个数+(这个数分解的最大数)*(这个数分解的最小数)

技巧:考虑到分解的数出0,当有0的时候及时break,就不会超时了


B. Young Explorers

Young wilderness explorers set off to their first expedition led by senior explorer Russell. Explorers went into a forest, set up a camp and decided to split into groups to explore as much interesting locations as possible. Russell was trying to form groups, but ran into some difficulties…

Most of the young explorers are inexperienced, and sending them alone would be a mistake. Even Russell himself became senior explorer not long ago. Each of young explorers has a positive integer parameter eiei — his inexperience. Russell decided that an explorer with inexperience ee can only join the group of ee or more people.

Now Russell needs to figure out how many groups he can organize. It’s not necessary to include every explorer in one of the groups: some can stay in the camp. Russell is worried about this expedition, so he asked you to help him.Input

The first line contains the number of independent test cases TT(1≤T≤2⋅1051≤T≤2⋅105). Next 2T2T lines contain description of test cases.

The first line of description of each test case contains the number of young explorers NN (1≤N≤2⋅1051≤N≤2⋅105).

The second line contains NN integers e1,e2,…,eNe1,e2,…,eN (1≤ei≤N1≤ei≤N), where eiei is the inexperience of the ii-th explorer.

It’s guaranteed that sum of all NN doesn’t exceed 3⋅1053⋅105.Output

Print TT numbers, each number on a separate line.

In ii-th line print the maximum number of groups Russell can form in ii-th test case.ExampleinputCopy

2
3
1 1 1
5
2 3 1 2 2

outputCopy

3
2

Note

In the first example we can organize three groups. There will be only one explorer in each group. It’s correct because inexperience of each explorer equals to 11, so it’s not less than the size of his group.

In the second example we can organize two groups. Explorers with inexperience 11, 22 and 33 will form the first group, and the other two explorers with inexperience equal to 22 will form the second group.

This solution is not unique. For example, we can form the first group using the three explorers with inexperience equal to 22, and the second group using only one explorer with inexperience equal to 11. In this case the young explorer with inexperience equal to 33 will not be included in any group.


题意:每个人有经验值,要分组,这个要求是:每个人去的组要求经验值<=人数

思路:贪心,从小到大排序,然后一个一个拉进去,当枚举到的这个人的经验值<=当前的组的人数,就可以多加一组。总的来说就是让组尽可能的多,所以要”小”组

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=2e5+10;
typedef long long LL;
LL a[maxn];
int main(void)
{
	LL t;cin>>t;
	while(t--)
	{
		LL n;cin>>n;
		for(LL i=1;i<=n;i++)
			cin>>a[i];
		sort(a+1,a+1+n);
		LL sum=0;LL ans=0;//sum表示最终组,ans表示枚举的人数 
		for(LL i=1;i<=n;i++)
		{
			ans++;
			if(ans>=a[i])
			{
				sum++;
				ans=0;
			}
			
		}	
		cout<<sum<<endl;
	}
return 0;
}

D. Game With Array

Petya and Vasya are competing with each other in a new interesting game as they always do.

At the beginning of the game Petya has to come up with an array of NN positive integers. Sum of all elements in his array should be equal to SS. Then Petya has to select an integer KK such that 0≤K≤S0≤K≤S.

In order to win, Vasya has to find a non-empty subarray in Petya’s array such that the sum of all selected elements equals to either KK or S−KS−K. Otherwise Vasya loses.

You are given integers NN and SS. You should determine if Petya can win, considering Vasya plays optimally. If Petya can win, help him to do that.Input

The first line contains two integers NN and SS (1≤N≤S≤1061≤N≤S≤106) — the required length of the array and the required sum of its elements.Output

If Petya can win, print “YES” (without quotes) in the first line. Then print Petya’s array in the second line. The array should contain NN positive integers with sum equal to SS. In the third line print KK. If there are many correct answers, you can print any of them.

If Petya can’t win, print “NO” (without quotes).

You can print each letter in any register (lowercase or uppercase).ExamplesinputCopy

1 4

outputCopy

YES
4
2

inputCopy

3 4

outputCopy

NO

inputCopy

3 8

outputCopy

YES
2 1 5
4

题意:给一个N,S,构造一个长为N的数组,使得总和为S;问你能不能构造存在一个数组使K和S-K不等于这个数组的非空子集。

思路:构造题,要尽量简单地去想,可以找找平均值,端点值,就极端值,那就取1,S-1,那么构造的只要(S/N)>=2,就说明每个值都>=2,那么1不会出现,除去最后一个值的总和为S-X,X>=2,也不会取得S-1.所以成立

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1e5;
typedef long long LL;

int main(void)
{
	LL n,s;cin>>n>>s;
	if(s/n>=2)
	{
		cout<<"YES"<<endl;
		for(int i=1;i<=n-1;i++) cout<<2<<' ';
		cout<<s-(n-1)*2<<endl;
		cout<<1<<endl;
	}
	else cout<<"NO"<<endl;

return 0;
}

E. Restorer Distance

You have to restore the wall. The wall consists of NN pillars of bricks, the height of the ii-th pillar is initially equal to hihi, the height is measured in number of bricks. After the restoration all the NN pillars should have equal heights.

You are allowed the following operations:

  • put a brick on top of one pillar, the cost of this operation is AA;
  • remove a brick from the top of one non-empty pillar, the cost of this operation is RR;
  • move a brick from the top of one non-empty pillar to the top of another pillar, the cost of this operation is MM.

You cannot create additional pillars or ignore some of pre-existing pillars even if their height becomes 00.

What is the minimal total cost of restoration, in other words, what is the minimal total cost to make all the pillars of equal height?Input

The first line of input contains four integers NN, AA, RR, MM (1≤N≤1051≤N≤105, 0≤A,R,M≤1040≤A,R,M≤104) — the number of pillars and the costs of operations.

The second line contains NN integers hihi (0≤hi≤1090≤hi≤109) — initial heights of pillars.Output

Print one integer — the minimal cost of restoration.ExamplesinputCopy

3 1 100 100
1 3 8

outputCopy

12

inputCopy

3 100 1 100
1 3 8

outputCopy

9

inputCopy

3 100 100 1
1 3 8

outputCopy

4

inputCopy

5 1 2 4
5 5 3 6 5

outputCopy

4

inputCopy

5 1 2 2
5 5 3 6 5

outputCopy

3

题意:有三堆砖头高度,有加上一个砖头的A,有减去一个砖头的B,有转移一个砖头的R。求一个高度使得他们变成同一个高度花费最小

思考:别问我为什么是凹函数,我不会证明。我单纯把这题当成一个典型题。

同时有一个之前遇到的类似(cf之前遇到的两个:一个雷击,另一个忘了)的贪心想法:移一个和加一个减一个什么时候相等?当R=A+B的时候相等,所以当R<A+B时,加一个减一个的操作用转移一个来代替。那我们就统计出要加的和要减的,然后让R和A+B比较,枚举H用三分

#include<iostream>
#include<vector>
#include<queue>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=2e5;
typedef long long LL;
LL h[maxn];
LL N,A,R,M;
LL f(LL _high)
{
	LL sub=0;LL add=0;LL res=0;
	for(LL i=1;i<=N;i++) 
	{
		if(h[i]<_high) add+=(_high-h[i]);//需要多少添加 
		else sub+=(h[i]-_high);//正数 
	}

	if(M<A+R)
	{
		LL _m=min(add,(sub) );//LL dif=sub+add;
		res+=_m*M;
		
		if( sub<add ) res+=(add-_m)*A;
		else res+=(sub-_m)*R ;			
	}
	else res= add*A+abs(sub)*R;
	
	return res;
}
int main(void)
{	
	cin>>N>>A>>R>>M;
	LL h_max=0;
	for(LL i=1;i<=N;i++) cin>>h[i],h_max=max(h_max,h[i]);///这里要有max...我忘记加max只有括号wa8wa了两小时
	LL l=0;LL r=h_max;
	LL lans=0;LL rans=0;
	while(l<r)
	{
		LL lmid=l+(r-l)/3;
		LL rmid=r-(r-l)/3;
		lans=f(lmid); rans=f(rmid);
		//求凹函数的极小值 
		if(lans<=rans )  r=rmid-1;
		else l=lmid+1;	
	} 
	cout<<min(lans,rans)<<endl;

return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值