本文主要是介绍Codeforces Round #643 (Div. 2)题目+详解+代码(A\B\C\D),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- A. Sequence with Digits
- B. Young Explorers
- C. Count Triangles
- D. Game With Array
A. Sequence with Digits
来源:http://codeforces.com/contest/1355/problem/A
Let’s define the following recurrence:
an+1=an+minDigit(an)⋅maxDigit(an).
Here minDigit(x) and maxDigit(x) are the minimal and maximal digits in the decimal representation of x without leading zeroes. For examples refer to notes.
Your task is calculate aK for given a1 and K.
Input
The first line contains one integer t (1≤t≤1000) — the number of independent test cases.
Each test case consists of a single line containing two integers a1 and K (1≤a1≤1018, 1≤K≤1016) separated by a space.
Output
For each test case print one integer aK on a separate line.
Example
inputCopy
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=487
a2=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=528
a4=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=564
a6=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=628
题意:
给定一个数NNN,定义操作op
op:
N=N+min(N的每一位数字)∗max(N的每一位数字)
思路:
可以发现,多次操作后N可能出现某一位出现0,所以,min(N的每一位数字)*max(N的每一位数字)为0,即:出现0后无论怎么操作N都不会改变
所以,执行K次模拟,当N出现0的时候break掉
代码:
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long ll;
using namespace std;
ll zh = 1;
ll gg(ll n)
{ll minn = 10; ll maxx = 0;for (int i = 1;; i++){ll a = n % 10;n = n / 10;minn = min(minn, a);maxx = max(maxx, a);if (a == 0){zh = 0;break;}if (n <= 0) break;}return maxx * minn;
}
int main()
{int t; cin >> t;while (t--){zh = 1;ll a, k; cin >> a >> k;ll yy = 0;for (ll i = 2; i <= k; i++){a += gg(a);if (zh == 0)break;}cout << a << endl;}
}
B. Young Explorers
来源:http://codeforces.com/contest/1355/problem/B
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 ei — his inexperience. Russell decided that an explorer with inexperience e can only join the group of e 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 T(1≤T≤2⋅105). Next 2T lines contain description of test cases.
The first line of description of each test case contains the number of young explorers N (1≤N≤2⋅105).
The second line contains N integers e1,e2,…,eN (1≤ei≤N), where ei is the inexperience of the i-th explorer.
It’s guaranteed that sum of all N doesn’t exceed 3⋅105.
Output
Print T numbers, each number on a separate line.
In i-th line print the maximum number of groups Russell can form in i-th test case.
Example
inputCopy
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 1, so it’s not less than the size of his group.
In the second example we can organize two groups. Explorers with inexperience 1, 2 and 3 will form the first group, and the other two explorers with inexperience equal to 2 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 2, and the second group using only one explorer with inexperience equal to 1. In this case the young explorer with inexperience equal to 3 will not be included in any group.
题意:
每人都有一个组团人数,值为ei的人只能加入大于等于ei个人的团,求最多能组成多少个团.
思路:
就是一个贪心,先从小到大排序,然后积累人数看是否大于ei ,如果大于则人数重新积累,否则积累人数+1;
具体实现见代码
#include<iostream>
using namespace std;
int a[300010];
int main() {int t, n;cin >> t;while (t--) {int ans = 0;cin >> n;for (int i = 0; i < n; i++) cin >> a[i];sort(a, a + n);for (int i =0, cnt = 0; i < n; i++){if (++cnt >= a[i]) {cnt = 0;ans++;}}cout << ans << endl;}return 0;
}
C. Count Triangles
来源:http://codeforces.com/contest/1355/problem/C
Like any unknown mathematician, Yuri has favourite numbers: A, B, C, and D, where A≤B≤C≤D. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides x, y, and z exist, such that A≤x≤B≤y≤C≤z≤D holds?
Yuri is preparing problems for a new contest now, so he is very busy. That’s why he asked you to calculate the number of triangles with described property.
The triangle is called non-degenerate if and only if its vertices are not collinear.
Input
The first line contains four integers: A, B, C and D (1≤A≤B≤C≤D≤5⋅105) — Yuri’s favourite numbers.
Output
Print the number of non-degenerate triangles with integer sides x, y, and z such that the inequality A≤x≤B≤y≤C≤z≤D holds.
Examples
inputCopy
1 2 3 4
outputCopy
4
inputCopy
1 2 2 5
outputCopy
3
inputCopy
500000 500000 500000 500000
outputCopy
1
Note
In the first example Yuri can make up triangles with sides (1,3,3), (2,2,3), (2,3,3) and (2,3,4).
In the second example Yuri can make up triangles with sides (1,2,2), (2,2,2) and (2,2,3).
In the third example Yuri can make up only one equilateral triangle with sides equal to 5⋅105.
题意:
给定A、B、C、D
给定限定条件A<=x<=B<=y<=C<=z<=D
问有多少个(x,y,z)能组成三角形
思路:
题目即统计满足x+y<z的方案数
容易想到枚举一个,O(1)计算方案数,但是细节有点多,不好码
还有一种做法是枚举x+y,设当前枚举到的x+y为i
那么需要计算的有两个:
1.满足i<z的z的数量
2.满足x<=y且x+y=i的(x,y)的数量
考虑如何计算2:
如果x为A,那么y为i-A
如果x为A+1,那么y为i-A-1
…
如果x为B,那么y为i-B
容易观察到y的变化范围为[i-B,i-A]
不过y还需要在[B,C]之内,取区间交就行了
代码:
#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){int a,b,c,d;cin>>a>>b>>c>>d;int ans=0;for(int i=a+b;i<=b+c;i++){if(i>=c){int l=max(i-b,b),r=min(i-a,c);int cnt=min(i-c,d-c+1);ans+=cnt*(r-l+1);}}cout<<ans<<endl;return 0;
}
D. Game With Array
来源:http://codeforces.com/contest/1355/problem/D
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 N positive integers. Sum of all elements in his array should be equal to S. Then Petya has to select an integer K such that 0≤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 K or S−K. Otherwise Vasya loses.
You are given integers N and S. 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 N and S (1≤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 N positive integers with sum equal to S. In the third line print K. 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).
Examples
inputCopy
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(1 <= k <= S)使得这个数组中的任意子数组的和不为k,如果存在输出yes,并且输出这个数组和k,否则输出no。
思路:
我们先贪心一下,直接把数组的前n-1个数,赋值成1,然后最后一个数赋值成s-(n-1)。
1.先看这个数组的前n-1个数的子数组的和的范围。
很容易知道,范围为:1到n-1。
2.再看包含最后一个数的子数组的范围是多少。
范围为s-(n-1)到s。
3.再判断第一步的最大值(也就是n-1)是否大于或等于第二步的最小值-1。
实际上,就是最大化数组中的和的范围,然后再判断其中是否存在和不连续的情况
#include <bits/stdc++.h>
using namespace std;
int main()
{int n, s;cin >> n >> s;int Min = s - n + 1, Max = n - 1;if (Max >= Min - 1) cout << "NO" << endl;else{cout << "YES" << endl;for (int i = 1; i <= n - 1; i++) cout << 1 << " ";cout << Min << endl;cout << Max + 1 << endl;}
}
本人水平有限,如有不足之处,请指正
这篇关于Codeforces Round #643 (Div. 2)题目+详解+代码(A\B\C\D)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!