• codeforces每日5题(均1600)-第二十八天


    Ivan and Powers of Two

    题面翻译

    有一个由 n n n 个非负整数组成的序列 a 1 a_1 a1
    a n a_n an,这个序列保证单调不降。接着,小华将上述序列作为2的次幂,写下了另一个序列:2的 a 1 a_1 a1 次幂到2的 a n a_n an 次幂。
    现在他想知道,最少要在这个序列中添加多少个形式为 2 x 2^x 2x 的数( x x x 为非负整数),才能使这个序列所有整数的和为 2 v − 1 2^v-1 2v1,其中 v v v 为某个非负整数。

    题目描述

    Ivan has got an array of n n n non-negative integers a 1 , a 2 , . . . , a n a_{1},a_{2},...,a_{n} a1,a2,...,an .

    Ivan knows that the array is sorted in the non-decreasing order.

    Ivan wrote out integers 2 a 1 , 2 a 2 , . . . , 2 a n 2^{a_{1}},2^{a_{2}},...,2^{a_{n}} 2a1,2a2,...,2an on a piece of paper.

    Now he wonders, what minimum number of integers of form 2 b 2^{b} 2b ( b > = 0 ) (b>=0) (b>=0) need to be added to the piece of paper so that the sum of all integers written on the paper equalled 2 v − 1 2^{v}-1 2v1 for some integer v v v ( v > = 0 ) (v>=0) (v>=0) .

    Help Ivan, find the required quantity of numbers.

    输入格式

    The first line contains integer n n n ( 1 < = n < = 1 0 5 1<=n<=10^{5} 1<=n<=105 ).

    The second input line contains n n n space-separated integers a 1 , a 2 , . . . , a n a_{1},a_{2},...,a_{n} a1,a2,...,an ( 0 < = a i < = 2 ⋅ 1 0 9 ) (0<=a_{i}<=2·10^{9}) (0<=ai<=2109) .

    It is guaranteed that a 1 < = a 2 < = . . . < = a n a_{1}<=a_{2}<=...<=a_{n} a1<=a2<=...<=an .

    输出格式

    Print a single integer — the answer to the problem.

    样例 #1

    样例输入 #1

    4
    0 1 1 1
    
    • 1
    • 2

    样例输出 #1

    0
    
    • 1

    样例 #2

    样例输入 #2

    1
    3
    
    • 1
    • 2

    样例输出 #2

    3
    
    • 1

    提示

    In the first sample you do not need to add anything, the sum of numbers already equals 2 3 − 1 = 7 2^{3}-1=7 231=7 .

    In the second sample you need to add numbers 2 0 , 2 1 , 2 2 2^{0},2^{1},2^{2} 20,21,22 .

    #include
    using namespace std;
    const int N=2e6+10;
    typedef long long ll;
    int n,v,ans,x;
    map<int,int>sum;
    int main(){
    	cin>>n; 
    	for(int i=1;i<=n;++i){
    		cin>>x;
    		while(sum.count(x))
    			sum.erase(x++);
    		++sum[x];
    		v=max(v,x);
    	}
    	cout<<v+1-sum.size()<<endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    Perfect Pair

    题面翻译

    题目大意:定义一对数(x,y),若x、y中至少有一个大于等于m,则称(x,y)对m完美。

    现在给定一对数(x,y),允许把其中的一个数换成x+y,问把(x,y)变成对m完美的数对至少需要几次操作。

    题目描述

    Let us call a pair of integer numbers m m m -perfect, if at least one number in the pair is greater than or equal to m m m .

    Thus, the pairs (3, 3) and (0, 2) are 2-perfect while the pair (-1, 1) is not.

    Two integers x x x , y y y are written on the blackboard.

    It is allowed to erase one of them and replace it with the sum of the numbers, ( x + y ) (x+y) (x+y) .

    What is the minimum number of such operations one has to perform in order to make the given pair of integers m m m -perfect?

    输入格式

    Single line of the input contains three integers x x x , y y y and m m m ( − 1 0 18 < = x -10^{18}<=x 1018<=x , y y y , m < = 1 0 18 m<=10^{18} m<=1018 ).

    Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preffered to use the cin, cout streams or the %I64d specifier.

    输出格式

    Print the minimum number of operations or “-1” (without quotes), if it is impossible to transform the given pair to the m m m -perfect one.

    样例 #1

    样例输入 #1

    1 2 5
    
    • 1

    样例输出 #1

    2
    
    • 1

    样例 #2

    样例输入 #2

    -1 4 15
    
    • 1

    样例输出 #2

    4
    
    • 1

    样例 #3

    样例输入 #3

    0 -1 5
    
    • 1

    样例输出 #3

    -1
    
    • 1

    提示

    In the first sample the following sequence of operations is suitable: (1, 2) (3, 2) (5, 2).

    In the second sample: (-1, 4) (3, 4) (7, 4) (11, 4) (15, 4).

    Finally, in the third sample x x x , y y y cannot be made positive, hence there is no proper sequence of operations.

    #include
    using namespace std;
    const int N=2e6+10;
    typedef long long ll;
    ll x,y,m,s=0;
    int main(){
    	cin>>x>>y>>m; 
    	if(x>=m||y>=m){
    		puts("0");
    		return 0;
    	}
    	else if(x<=0&&y<=0){
    		puts("-1");
    		return 0;
    	}
    	else if(x*y<0){
    		if(x<y){
    			s+=(-x)/y;
    			x=x%y;
    		}
    		else{
    			s+=(-y)/x;
    			y=y%x;
    		}
    	}
    	while(x<m&&y<m){
    		if(x>y)swap(x,y);
    		x+=y;
    		s++;
    	}
    	cout<<s;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    Ciel and Flowers

    题面翻译

    Fox Ciel有r朵红花,g朵绿花和b朵蓝花。她想用这些花做几个花束。

    有四种类型的花束:

    • 制作“红色花束”,要3朵红花
    • 制作“绿色花束”,要3朵绿花
    • 制作“蓝色花束”,要3朵蓝花
    • 制作“混合花束”,要1朵红,1朵绿和1朵蓝花

    输出可以制作的最大数量的花束。

    题目描述

    Fox Ciel has some flowers: r r r red flowers, g g g green flowers and b b b blue flowers.

    She wants to use these flowers to make several bouquets.

    There are 4 types of bouquets:

    • To make a “red bouquet”, it needs 3 red flowers.
    • To make a “green bouquet”, it needs 3 green flowers.
    • To make a “blue bouquet”, it needs 3 blue flowers.
    • To make a “mixing bouquet”, it needs 1 red, 1 green and 1 blue flower.

    Help Fox Ciel to find the maximal number of bouquets she can make.

    输入格式

    The first line contains three integers r r r , g g g and b b b ( 0 < = r , g , b < = 1 0 9 0<=r,g,b<=10^{9} 0<=r,g,b<=109 ) — the number of red, green and blue flowers.

    输出格式

    Print the maximal number of bouquets Fox Ciel can make.

    样例 #1

    样例输入 #1

    3 6 9
    
    • 1

    样例输出 #1

    6
    
    • 1

    样例 #2

    样例输入 #2

    4 4 4
    
    • 1

    样例输出 #2

    4
    
    • 1

    样例 #3

    样例输入 #3

    0 0 0
    
    • 1

    样例输出 #3

    0
    
    • 1

    提示

    In test case 1, we can make 1 red bouquet, 2 green bouquets and 3 blue bouquets.

    In test case 2, we can make 1 red, 1 green, 1 blue and 1 mixing bouquet.

    #include
    using namespace std;
    const int N=2e6+10;
    typedef long long ll;
    ll a,b,c,ans;
    int main(){
    	cin>>a>>b>>c;
    	ans=a/3+b/3+c/3+min(a%3,min(b%3,c%3));
    	if(a>=1&&b>=1&&c>=1) ans=max(ans,(a-1)/3+(b-1)/3+(c-1)/3+1+min((a+2)%3,min((b+2)%3,(c+2)%3)));
    	if(a>=2&&b>=2&&c>=2) ans=max(ans,(a-2)/3+(b-2)/3+(c-2)/3+2+min((a+1)%3,min((b+1)%3,(c+1)%3)));
    	printf("%d\n",ans);
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    Secrets

    题面翻译

    这个世界上只有这样面值的硬币:1,3,9,27,81,…有一个商人,某一天遇到了一个顾客,他购买了价值n的商品,发现用自己的硬币无法付给商人刚好n的钱。那个顾客会给商人大于等于n的钱且使得给商人的硬币数量最少。

    在这个顾客有的硬币可能的各种情况下,请问这个商人最多可能收到多少硬币?

    题目描述

    Gerald has been selling state secrets at leisure.

    All the secrets cost the same: n n n marks.

    The state which secrets Gerald is selling, has no paper money, only coins.

    But there are coins of all positive integer denominations that are powers of three: 1 mark, 3 marks, 9 marks, 27 marks and so on.

    There are no coins of other denominations.

    Of course, Gerald likes it when he gets money without the change.

    And all buyers respect him and try to give the desired sum without change, if possible.

    But this does not always happen.

    One day an unlucky buyer came.

    He did not have the desired sum without change.

    Then he took out all his coins and tried to give Gerald a larger than necessary sum with as few coins as possible.

    What is the maximum number of coins he could get?

    The formal explanation of the previous paragraph: we consider all the possible combinations of coins for which the buyer can not give Gerald the sum of n n n marks without change.

    For each such combination calculate the minimum number of coins that can bring the buyer at least n n n marks.

    Among all combinations choose the maximum of the minimum number of coins. This is the number we want.

    输入格式

    The single line contains a single integer n n n ( 1 < = n < = 1 0 17 1<=n<=10^{17} 1<=n<=1017 ).

    Please, do not use the %lld specifier to read or write 64 bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

    输出格式

    In a single line print an integer: the maximum number of coins the unlucky buyer could have paid with.

    样例 #1

    样例输入 #1

    1
    
    • 1

    样例输出 #1

    1
    
    • 1

    样例 #2

    样例输入 #2

    4
    
    • 1

    样例输出 #2

    2
    
    • 1

    提示

    In the first test case, if a buyer has exactly one coin of at least 3 marks, then, to give Gerald one mark, he will have to give this coin.

    In this sample, the customer can not have a coin of one mark, as in this case, he will be able to give the money to Gerald without any change.

    In the second test case, if the buyer had exactly three coins of 3 marks, then, to give Gerald 4 marks, he will have to give two of these coins.

    The buyer cannot give three coins as he wants to minimize the number of coins that he gives.

    #include
    using namespace std;
    const int N=2e6+10;
    typedef long long ll;
    ll n,s=1;
    int main(){
    	cin>>n;
    	while(n%s==0) s*=3;
    	cout<<n/s+1;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    Rational Resistance

    题面翻译

    题意翻译简化:

    迈克有许多阻值为 1 Ω 1Ω 的电阻,通过串联、并联的方式组装为一个电阻为 a b \frac{a}{b} ba 的元件,求所需的最少电阻数量。

    数据:

    1 < = a , b < = 1 0 18 1<=a,b<=10^{18} 1<=a,b<=1018,保证 a b \frac{a}{b} ba 已经最简,保证有解。

    题目描述

    Mad scientist Mike is building a time machine in his spare time.

    To finish the work, he needs a resistor with a certain resistance value.

    However, all Mike has is lots of identical resistors with unit resistance R 0 = 1 R_{0}=1 R0=1 .

    Elements with other resistance can be constructed from these resistors.

    In this problem, we will consider the following as elements:

    1. one resistor;
    2. an element and one resistor plugged in sequence;
    3. an element and one resistor plugged in parallel.

    With the consecutive connection the resistance of the new element equals R = R e + R 0 R=R_{e}+R_{0} R=Re+R0 .
    With the parallel connection the resistance of the new element equals . In this case R e R_{e} Re equals the resistance of the element being connected.

    Mike needs to assemble an element with a resistance equal to the fraction .

    Determine the smallest possible number of resistors he needs to make such an element.

    输入格式

    The single input line contains two space-separated integers a a a and b b b ( 1 < = a , b < = 1 0 18 1<=a,b<=10^{18} 1<=a,b<=1018 ).

    It is guaranteed that the fraction is irreducible. It is guaranteed that a solution always exists.

    输出格式

    Print a single number — the answer to the problem.

    Please do not use the %lld specifier to read or write 64-bit integers in С++. It is recommended to use the cin, cout streams or the %I64d specifier.

    样例 #1

    样例输入 #1

    1 1
    
    • 1

    样例输出 #1

    1
    
    • 1

    样例 #2

    样例输入 #2

    3 2
    
    • 1

    样例输出 #2

    3
    
    • 1

    样例 #3

    样例输入 #3

    199 200
    
    • 1

    样例输出 #3

    200
    
    • 1

    提示

    In the first sample, one resistor is enough.

    In the second sample one can connect the resistors in parallel, take the resulting element and connect it to a third resistor consecutively.

    Then, we get an element with resistance . We cannot make this element using two resistors.

    #include
    using namespace std;
    const int N=2e6+10;
    typedef long long ll;
    ll a,b,c,s=0;
    int main(){
    	cin>>a>>b;
    	while(b!=0){
    		s+=a/b;
    		c=a%b;
    		a=b,b=c;
    	}
    	cout<<s;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
  • 相关阅读:
    springboot和spring使用@Async注意事项
    学习 vite + vue3 + pinia + ts(四)setup异步返回 async setup
    Pytest插件
    【软考 系统架构设计师】计算机组成与体系结构③ 存储管理
    JavaScript 面试题
    obsidian加git备份,同时忽略掉自己不想同步的文件夹
    Python蒙特卡洛树搜索算法实现的黑白棋AI系统
    Ceres曲线拟合
    硬核!让初学者快速掌握经典算法的宝典——数据结构与算法经典问题解析
    王道22数据结构课后习题(第二章2.2.3)
  • 原文地址:https://blog.csdn.net/weixin_59624686/article/details/126092037