Codeforces Round #847 (DFiv. 3)
A Polycarp and the Day of Pi
题目描述
On March 14, the day of the number $ \pi $ is celebrated all over the world. This is a very important mathematical constant equal to the ratio of the circumference of a circle to its diameter.
Polycarp was told at school that the number $ \pi $ is irrational, therefore it has an infinite number of digits in decimal notation. He wanted to prepare for the Day of the number $ \pi $ by memorizing this number as accurately as possible.
Polycarp wrote out all the digits that he managed to remember. For example, if Polycarp remembered $ \pi $ as $ 3.1415 $ , he wrote out 31415.
Polycarp was in a hurry and could have made a mistake, so you decided to check how many first digits of the number $ \pi $ Polycarp actually remembers correctly.
输入格式
The first line of the input data contains the single integer $ t $ ( $ 1 \le t \le 10^3 $ ) — the number of test cases in the test.
Each test case is described by a single string of digits $ n $ , which was written out by Polycarp.
The string $ n $ contains up to $ 30 $ digits.
输出格式
Output $ t $ integers, each of which is the answer to the corresponding test case, that is how many first digits of the number $ \pi $ Polycarp remembers correctly.
in
9
000
3
4141592653
141592653589793238462643383279
31420
31415
314159265358
27182
314159265358979323846264338327
out
0
1
0
0
3
5
12
0
30
思路
我们先保存好$ \pi $的前30位,然后每组数据进行配对就行
看数据范围不会超时
key code
string ans="314159265358979323846264338327";
void solve(){
//try it again.
int cnt=0;
string s;
cin>>s;
n=s.size()-1;
up(0,n){
if(s[o]!=ans[o])break;
else cnt++;
}
cout<<cnt<<endl;
}
B Taisia and Dice
题目描述
Taisia has $ n $ six-sided dice. Each face of the die is marked with a number from $ 1 $ to $ 6 $ , each number from $ 1 $ to $ 6 $ is used once.
Taisia rolls all $ n $ dice at the same time and gets a sequence of values $ a_1, a_2, \ldots, a_n $ ( $ 1 \le a_i \le 6 $ ), where $ a_i $ is the value on the upper face of the $ i $ -th dice. The sum of this sequence is equal to $ s $ .
Suddenly, Taisia's pet cat steals exactly one dice with maximum value $ a_i $ and calculates the sum of the values on the remaining $ n-1 $ dice, which is equal to $ r $ .
You only know the number of dice $ n $ and the values of $ s $ , $ r $ . Restore a possible sequence $ a $ that fulfills the constraints.
输入格式
The first line contains the integer $ t $ ( $ 1 \le t \le 1000 $ ) — the number of testcases.
Each testcase is given on a separate line and contains three integers $ n $ , $ s $ , $ r $ ( $ 2 \le n \le 50 $ , $ 1 \le r < s \le 300 $ ).
It is guaranteed that a solution exists.
输出格式
For each testcase, print: $ n $ integers $ a_1, a_2, \ldots, a_n $ in any order. It is guaranteed that such sequence exists.
If there are multiple solutions, print any.
in
7
2 2 1
2 4 2
4 9 5
5 17 11
3 15 10
4 4 3
5 20 15
out
1 1
2 2
1 2 2 4
6 4 2 3 2
5 5 5
1 1 1 1
1 4 5 5 5
思路:
就是小猫偷走了一个骰子,这一个骰子是确定的点数,而且剩下的骰子里面都不能比这一个的点数大。
于是就转换成了有\(r\)个点数分成\(n_0\)个骰子能不能分完的情况
根据题意可以知道一定是可以分完的,也就是说一定有可行的解
我们再依次从大到小进行选择
如果选择了这一个点数那么剩下的全都是1的点数仍然比\(r\)大那么就跳过这一个点数,进而选更小的
key code
void solve(){
//try it again.
int n,s,r;
cin>>n>>s>>r;
int tt=s-r;//偷的一个
int n0=n-1;
//分n0个骰子为r
// debug(n0);
// debug(r);
cout<<tt<<" ";
dn(tt,1){
while((o+n0-1)<=r&&n0>0){
n0--;
r-=o;
cout<<o<<" ";
}
}
cout<<endl;
}
C Premutation
题目描述
A sequence of $ n $ numbers is called permutation if it contains all integers from $ 1 $ to $ n $ exactly once. For example, the sequences [ $ 3, 1, 4, 2 $ ], [ $ 1 $ ] and [ $ 2,1 $ ] are permutations, but [ $ 1,2,1 $ ], [ $ 0,1 $ ] and [ $ 1,3,4 $ ] — are not.
Kristina had a permutation $ p $ of $ n $ elements. She wrote it on the whiteboard $ n $ times in such a way that:
- while writing the permutation at the $ i $ -th ( $ 1 \le i \le n) $ time she skipped the element $ p_i $
So, she wrote in total $ n $ sequences of length $ n-1 $ each.For example, suppose Kristina had a permutation $ p $ = $ [4,2,1,3] $ of length $ 4 $ . Then she did the following:
- Wrote the sequence $ [2, 1, 3] $ , skipping the element $ p_1=4 $ from the original permutation.
- Wrote the sequence $ [4, 1, 3] $ , skipping the element $ p_2=2 $ from the original permutation.
- Wrote the sequence $ [4, 2, 3] $ , skipping the element $ p_3=1 $ from the original permutation.
- Wrote the sequence $ [4, 2, 1] $ , skipping the element $ p_4=3 $ from the original permutation.
You know all $ n $ of sequences that have been written on the whiteboard, but you do not know the order in which they were written. They are given in arbitrary order. Reconstruct the original permutation from them.
For example, if you know the sequences $ [4, 2, 1] $ , $ [4, 2, 3] $ , $ [2, 1, 3] $ , $ [4, 1, 3] $ , then the original permutation will be $ p $ = $ [4, 2, 1, 3] $ .
输入格式
The first line of input data contains a single integer $ t $ ( $ 1 \le t \le 10^4 $ ) — the number of test cases.
The description of the test cases follows.
The first line of each test case contains one integer $ n $ ( $ 3 \le n \le 100 $ ).
This is followed by $ n $ lines, each containing exactly $ n-1 $ integers and describing one of the sequences written out on the whiteboard.
It is guaranteed that all sequences could be obtained from some permutation $ p $ , and that the sum $ n^2 $ over all input sets does not exceed $ 2 \cdot 10^5 $ .
输出格式
For each test case, output on a separate line a permutation $ p $ such that the given $ n $ sequences could be obtained from it.
It is guaranteed that the answer exists and it is the only one. In other words, for each test case the required permutation is sure to exist.
in
5
4
4 2 1
4 2 3
2 1 3
4 1 3
3
2 3
1 3
1 2
5
4 2 1 3
2 1 3 5
4 2 3 5
4 1 3 5
4 2 1 5
4
2 3 4
1 3 4
1 2 3
1 2 4
3
2 1
1 3
2 3
out
4 2 1 3
1 2 3
4 2 1 3 5
1 2 3 4
2 1 3
提示
The first test case is described in the problem statement.
In the second test case, the sequences are written in the correct order.
思路:
显然我们可以知道在原排列中靠前的比靠后的下标要小
同理那么在子排列中原来靠前的也比原来靠后的下标要小
进而可得原来靠前的也比原来靠后的下标之和要小
那么我们就可以根据下标的和来进行排序
key code
PII cnt[110];
bool cmp(PII a,PII b){
return a.se<b.se;
}
void solve(){
//try it again.
// map<int,int>mp;
mem0(cnt);
cin>>n;
up(1,n){
cnt[o].fi=o;
}
up(1,n){
fup(i,1,n-1){
int x;
cin>>x;
cnt[x].se+=i;
}
}
sort(cnt+1,cnt+1+n,cmp);
up(1,n){
if(cnt[o].se)cout<<cnt[o].fi<<" ";
}
cout<<endl;
}
D Matryoshkas
题目描述
Matryoshka is a wooden toy in the form of a painted doll, inside which you can put a similar doll of a smaller size.
A set of nesting dolls contains one or more nesting dolls, their sizes are consecutive positive integers. Thus, a set of nesting dolls is described by two numbers: $ s $ — the size of a smallest nesting doll in a set and $ m $ — the number of dolls in a set. In other words, the set contains sizes of $ s, s + 1, \dots, s + m - 1 $ for some integer $ s $ and $ m $ ( $ s,m > 0 $ ).
You had one or more sets of nesting dolls. Recently, you found that someone mixed all your sets in one and recorded a sequence of doll sizes — integers $ a_1, a_2, \dots, a_n $ .
You do not remember how many sets you had, so you want to find the minimum number of sets that you could initially have.
For example, if a given sequence is $ a=[2, 2, 3, 4, 3, 1] $ . Initially, there could be $ 2 $ sets:
- the first set consisting of $ 4 $ nesting dolls with sizes $ [1, 2, 3, 4] $ ;
- a second set consisting of $ 2 $ nesting dolls with sizes $ [2, 3] $ .
According to a given sequence of sizes of nesting dolls $ a_1, a_2, \dots, a_n $ , determine the minimum number of nesting dolls that can make this sequence.
Each set is completely used, so all its nesting dolls are used. Each element of a given sequence must correspond to exactly one doll from some set.
输入格式
The first line of input data contains a single integer $ t $ ( $ 1 \le t \le 10^4 $ ) — the number of test cases.
The description of the test cases follows.
The first line of each test case contains one integer $ n $ ( $ 1 \le n \le 2 \cdot 10^5 $ ) — the total number of matryoshkas that were in all sets.
The second line of each test case contains $ n $ integers $ a_1, a_2, \dots, a_n $ ( $ 1 \le a_i \le 10^9 $ ) — the sizes of the matryoshkas.
It is guaranteed that the sum of values of $ n $ over all test cases does not exceed $ 2\cdot10^5 $ .
输出格式
For each test case, print one integer $ k $ — the minimum possible number of matryoshkas sets.
in
10
6
2 2 3 4 3 1
5
11 8 7 10 9
6
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
8
1 1 4 4 2 3 2 3
6
1 2 3 2 3 4
7
10 11 11 12 12 13 13
7
8 8 9 9 10 10 11
8
4 14 5 15 6 16 7 17
8
5 15 6 14 8 12 9 11
5
4 2 2 3 4
out
2
1
6
2
2
2
2
2
4
3
提示
The first test case is described in the problem statement.
In the second test case, all matryoshkas could be part of the same set with minimum size $ s=7 $ .
In the third test case, each matryoshka represents a separate set.
思路:
这个题就是看值的变化规律
先给原数组排序并计次数
然后去掉重复的项
如果相邻两项之间的差值大于1,那么就会组成一个新的套娃
如果相邻两项之间的差值等于1,而且后面的次数大于前面的次数,那么就要把多出来的次数也加上
key code
int a[N];
bool st[N];
void solve(){
//try it again.
cin>>n;
up(1,n){
cin>>a[o];
}
map<int,int>mp;
map<int,bool>st;
sort(a+1,a+1+n);
int cnt=0;
// up(1,n)cout<<a[o]<<" \n"[o==n];
up(1,n){
mp[a[o]]++;
}
up(1,n){
if(a[o]>a[o-1]+1){
cnt+=mp[a[o]];
}
else if(a[o]==a[o-1]+1){
cnt+=(mp[a[o]]>mp[a[o-1]]?mp[a[o]]-mp[a[o-1]]:0);
}
}
cout<<cnt<<endl;
}
E Vlad and a Pair of Numbers
题目描述
Vlad found two positive numbers $ a $ and $ b $ ( $ a,b>0 $ ). He discovered that $ a \oplus b = \frac{a + b}{2} $ , where $ \oplus $ means the bitwise exclusive OR , and division is performed without rounding..
Since it is easier to remember one number than two, Vlad remembered only $ a\oplus b $ , let's denote this number as $ x $ . Help him find any suitable $ a $ and $ b $ or tell him that they do not exist.
输入格式
The first line of the input data contains the single integer $ t $ ( $ 1 \le t \le 10^4 $ ) — the number of test cases in the test.
Each test case is described by a single integer $ x $ ( $ 1 \le x \le 2^{29} $ ) — the number that Vlad remembered.
输出格式
Output $ t $ lines, each of which is the answer to the corresponding test case. As the answer, output $ a $ and $ b $ ( $ 0 < a,b \le 2^{32} $ ), such that $ x = a \oplus b = \frac{a + b}{2} $ . If there are several answers, output any of them. If there are no matching pairs, output -1.
in
6
2
5
10
6
18
36
out
3 1
-1
13 7
-1
25 11
50 22
思路:
好吧其实这个题我不会哈哈哈
我是硬打表做的
fup(i,1,100){
fup(j,i+1,100){
int x=i+j>>1;
if(((i^j)<<1)==(i+j)){
debug(((i)&x));
debug(((i&j)&x));
debug3(i,j,x);
}
}
}
((i)&x)=0
((i&j)&x)=0
i=1 j=3 x=2
((i)&x)=0
((i&j)&x)=0
i=2 j=6 x=4
((i)&x)=0
((i&j)&x)=0
i=4 j=12 x=8
((i)&x)=0
((i&j)&x)=0
i=5 j=15 x=10
((i)&x)=2
((i&j)&x)=0
i=7 j=13 x=10
((i)&x)=0
((i&j)&x)=0
i=8 j=24 x=16
((i)&x)=0
((i&j)&x)=0
i=9 j=27 x=18
((i)&x)=0
((i&j)&x)=0
通过打表我们可以发现如果存在解那么一定存在\(j\)&\({i}\) & \({x}=0\)的条件
那我们就写出来打表的代码
key code
void solve(){
//try it again.
cin>>n;
if(n&1)cout<<-1<<endl;
else{
int x=n;
if(((x>>1)&x)==0)cout<<(x>>1)<<" "<<(x*2-((x>>1)))<<endl;
else cout<<-1<<endl;
}
}
然后就过了hhh,好神奇
标签:847,10,le,sequence,题解,number,case,test,Div From: https://www.cnblogs.com/liangqianxing/p/17069614.html