文章目录
- A. Lily
- M.Youth Finale
- C.Array Concatenation
- E.Draw a triangle
A. Lily
A. Lily
time limit per test1 second
memory limit per test512 megabytes
inputstandard input
outputstandard output
-
They serve the purpose of changing hydrogen into breathable oxygen, and they’re as necessary here, as the air is on earth.
-
But I still say, they’re flowers.
-
If you like.
-
Do you sell them?
-
I’m afraid not.
-
But, maybe we can make a deal.
-
What do you mean?
-
Oh you see, you don’t have to send them anywhere. I’ll pay for them. But then, I’ll leave them here, for you.
— Assignment Outerspace, 1960
Everything that has a beginning has an ending. My journey has been reaching its ending, and I’ve been ready to say goodbye to my yesterday. But you, my dear friend, your journey is still thriving here at the 2022 CCPC Guilin Site. We sincerely hope you find a brand new milestone here, and forge ahead in the future with your love and passion.
Lily means a kind of beautiful flower. She usually only blooms once a year, so it could be very precious if you see a lily blooming. However, she is highly toxic to cats, so you must be aware of keeping curious cats away from lovely lilies.
You have n grids of soil land in a row, for 1 to n, some with lilies blooming. We don’t want to hurt lilies, as well as cats. You can put some cat food on the grids, but for any grid i with cat food, grids with indices falling in the range [i−1,i+1] must not contain lily flowers. You love cats and lilies, so you want to maximize the number of grids having cat food.
Design a plan to fulfill the above requirements.
Input
There’s a single integer n (1≤n≤1000) in the first line, denoting the number of grids.
The second line contains a string R consisting of only ‘L’ and ‘.’, denoting the grids with and without lilies.
Output
The output contains a single line with string R′ consisting of only ‘L’, ‘.’ and ‘C’, where ‘C’ means the cat food you assigned to the empty grids in R while fulfilling the above requirements.
If there are multiple solutions, print any.
Examples
inputCopy
5
…L…
outputCopy
C.L.C
inputCopy
2
…
outputCopy
CC
题意:
- 给出一个长为n的字符串,由.和L组成。
- 在.的位置放C,要求满足L的相邻两个没有C,求构造一种方案输出。
思路:
- 扫一遍,对于.,如果前后都没有L就改成C,然后输出即可。
#include<bits/stdc++.h>
using namespace std;
int main(){
int n; string s; cin>>n>>s;
if(s[0]=='.' && s[1]!='L')s[0] = 'C';
if(s[n-1]=='.' && s[n-2]!='L')s[n-1] = 'C';
for(int i = 1; i < n-1; i++){
if(s[i]=='.' && s[i+1] != 'L' && s[i-1]!='L'){
s[i] = 'C';
}
}
cout<<s<<"\n";
return 0;
}
M.Youth Finale
M. Youth Finale
time limit per test3 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
Finales are born to be exciting. Performers play hard to draw audiences’ attention and then take a perfect curtain call. As the last problem and the finale of the problem set, however, we want you to recall a simple algorithm. Like me, it may be the first algorithm you’ve learned, called Bubble Sort.
void bubble_sort(int a[], int n) { // 0-based, sort from lowest to highest
for (int i = 1; i < n; i++) {
for (int j = 0; j < n - i; j++) {
if (a[j] > a[j + 1]) {
swap(a[j], a[j + 1]);
}
} // after i-th inner iteration, a[n - i] is correct
}
}
Given a permutation of length n, as you might know, Bubble Sort runs in Ω(n2) in the worst case. It’s quite a traditional idea to count the number of calls of “swap” in the algorithm. As you are stronger now, you want to count that number in a dynamic permutation with the following events that might happen:
Reverse the permutation, meaning that the permutation is replaced with
p′={pn,pn−1,…,p2,p1}.
Shift the permutation to the left by 1, meaning that the permutation is replaced with
p′={p2,p3,…,pn,p1}.
All you need to do is to output the number of “swap” that would be called if we sort the permutation with the above Bubble Sort code after each operation.
Input
The first line contains two integers n,m(1≤n≤3×105,1≤m≤6×105), denoting the length of permutation and the number of operations.
The second line contains n integers separated by spaces, and the i-th denotes the initial pi.
The third line contains a single string containing m letters consisting of ‘R’ and ‘S’. The i-th letter denotes the i-th operation, where ‘R’ or ‘S’ denotes the Reverse or Shift operation, respectively.
It’s guaranteed that p forms a correct permutation of 1,2,…,n.
Output
In the first line, print the number of “swap” would be called when Bubble Sort the initial p.
In the second line, print a single string of m digits. The i-th denotes the number of “swap” would be called to Bubble Sort the permutation, modulo 10.
Example
inputCopy
5 10
5 4 3 2 1
SSSSRSSSSR
outputCopy
10
6446466400
题意:
- 给出一个长为n的排列,每次操作可以反转排列,或将排列循环左移一位。给出m次操作的操作方案。
- 给出一个冒泡从低到高排序的代码。求每次操作后,需要多少次冒泡排序将该数组变成排好序的数组。 输出答案%10。
思路:
- 不难发现未操作时的冒泡排序次数就是原数组的逆序对个数。
- 对于每次操作,因为是个排列,如果他左移,逆序对个数就会少掉比他小的数的个数,并多上比他大的数的个数。如果翻转,则逆序对个数和正序对个数交换。
- 知道规律后, deque维护一下数组,然后根据规律输出即可。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 1e6+10;
LL n, m, a[maxn];
deque<LL>q;
LL b[maxn];
void add(LL x, LL v){ for(LL i = x; i <= n; i+=i&(-i))b[i]+=v;}
LL query(LL x){ LL ans=0;for(LL i=x;i>0;i-=i&(-i))ans+=b[i];return ans;}
int main(){
cin>>n>>m;
for(LL i = 0; i < n; i++)cin>>a[i], q.push_back(a[i]);
long long ans = 0;
for(LL i = n-1; i >= 0; i--){
add(a[i], 1);
ans += query(a[i]-1);
}
cout<<ans<<"\n";
string s; cin>>s;
LL ward = 0, x;
for(LL i = 0; i < m; i++){
if(s[i]=='S'){
if(ward==0){
x = q.front(); q.pop_front();
q.push_back(x);
}else{
x = q.back(); q.pop_back();
q.push_front(x);
}
ans = (ans-(x-1)+10)%10;
ans = (ans+(n-x)+10)%10;
}else{
ans = (n*(n-1)/2-ans+10)%10;
ward = !ward;
}
ans = (ans+10)%10;
cout<<ans;
}
return 0;
}
C.Array Concatenation
C. Array Concatenation
time limit per test1 second
memory limit per test512 megabytes
inputstandard input
outputstandard output
Little relyt871 has a magical machine. In each operation, his machine can do one of the following operations to the input array b:
Generate a copy of b and concatenate it after b. More formally, the resulting array should be
b′={b1,b2,…,b|b|,b1,b2,…,b|b|}.
Generate a copy of b, reverse it, then concatenate it before b. More formally, the resulting array should be
b′={b|b|,b|b−1|,…,b1,b1,b2,…,b|b|}.
Initially, he has an array a of length n. Then, he wants to operate the machine exactly m times using the array on his hand while maximizing the sum of all prefix sums of the final array. Since he has a somewhat finite brain, when he adds some integers, he only cares about the sum modulo 1000000007. Formally, suppose after all m operations he has array b of length n′, he wants to maximize the following value:
(∑i=1n′∑j=1ibj)(mod1000000007).
Please note that you should maximize the value after taking the modulo: the array with answer 1000000007 before taking the modulo is considered less than the array with answer 1.
Input
The first line contains two integers n and m (1≤n,m≤105).
The second line contains n integers a1, a2,…, an (1≤ai≤109) separated by spaces.
Output
Print a single integer in one line, denoting the answer.
Examples
inputCopy
2 1
1 2
outputCopy
15
inputCopy
5 10
26463 39326 86411 75307 85926
outputCopy
806275469
题意:
- 给出一个长为n的数组,进行m次操作。每次操作可以将原数组复制一份放到原数组的前面,或者复制一份并翻转后放到原数组的前面,构成一个新的长度为2n的数组。
- 求最终进行完m次操作后,最后数组的前缀和的和%1e9+7的最大值。 注意是取模后的最大值。
思路:
- 对于样例2,打表发现答案是一次翻一次不翻的情况,再发现修改m的次数对最终的结果并没有造成影响。
- 所以不难猜规律肯定是特殊的情况,要么先翻再不翻,或者先不翻再翻,要么就是全翻转,要么就是全不翻转,一共就四种情况。取个max就可以过。
- 官方题解证明:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL maxn = 1e6+10, mod = 1e9+7;
LL n, m, a[maxn];
LL pows(LL a, LL x) { if(x==0)return 1; LL t = pows(a, x>>1); if(x%2==0)return t*t%mod; return t*t%mod*a%mod; }
int main(){
cin>>n>>m;
LL s = 0, ss = 0;
for(LL i = 1; i <= n; i++){cin>>a[i]; s += a[i]; ss += a[i]*(n-i+1)%mod;}
s %= mod; ss %= mod;
LL res1 = 2*n+1;
for(LL i = 1; i < m; i++){
res1 = (res1*2%mod+n*pows(2,2*i)%mod)%mod;
}
res1 = res1*s%mod;
LL res2 = ss;
for(LL i = 1; i <= m; i++){
res2 = (res2*2%mod+s*n%mod)%mod;
s = s*2%mod;
n = n*2%mod;
}
cout<<max(res1,res2)%mod<<"\n";
return 0;
}
E.Draw a triangle
E. Draw a triangle
time limit per test1 second
memory limit per test512 megabytes
inputstandard input
outputstandard output
Little Desprado2 is a student of Springfield Flowers Kindergarten. On this day, he had just learned how to draw triangles on grid coordinate paper. However, he soon found it very dull, so he came up with a more interesting question:
He had drawn two integral points of the triangle on the grid paper, and he denotes them (x1,y1) and (x2,y2). Now, he wanted to know the answer to the following question: where can he draw the third point (x3,y3) so that the area of the triangle is positive but minimized?
Obviously, he can’t solve this problem because he is too young and simple. Can you tell him the answer?
Please note that your answer’s coordinates must consist of integers because he is drawing on grid paper, and the triangle shouldn’t be a degenerated triangle to keep the area positive.
Input
The first line contains one integer T (1≤T≤50000), denoting the number of Little Desprado2’s queries.
For each test case, there’s a single line contains four integers x1, y1, x2, y2 (−109≤x1, y1, x2, y2≤109) seperated by spaces, denoting two points are at (x1,y1) and (x2,y2), respectively.
It is guaranteed that the two points won’t coincide.
Output
For each test case, print two integers x3, y3 (−1018≤x3, y3≤1018) in a separated line, denoting your answer.
If there are multiple answers, you can print any one of them. It is guaranteed that there exists a solution in the above range.
Example
inputCopy
3
1 0 1 4
0 1 0 9
0 0 2 2
outputCopy
2 0
1 1
-1 0
题意:
- 给出平面上的两个点(x1,y1)(x2,y2),求第三个整数点(x3,y3)让三个点构成的三角形面积最小。
- 5e4次询问,输入小于正负1e9,输出小于正负1e18。
思路:
- 官方
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL maxn = 1e6+10, mod = 1e9+7;
LL exgcd(LL a, LL b, LL &x, LL &y){
if(b==0){ x=1; y=0; return a; }
LL r = exgcd(b,a%b,x,y);
LL temp = y;
y=x-(a/b)*y;
x=temp;
return r;
}
int main(){
LL T; cin>>T;
while(T--){
LL x1, y1, x2, y2; cin>>x1>>y1>>x2>>y2;
//AB=(a,b)=(x2-x1, y2-y1), AC=(c,d)=(x3-x1,y3-y1)
//S = ABxAC = -b*c+a*d = gcd(a,b), 所以a=-(y2-y1), b = x2-x1, exgcd得c,d
LL a = -(y2-y1), b = x2-x1, c, d;
LL k1 = 1, k2 = 1; //符号取正最后再乘回来
if(a <= 0)k1=-k1, a=-a;
if(b <= 0)k2=-k2, b=-b;
exgcd(a, b, c, d);
c *= k1; d *= k2;
cout<<(c+x1)<<" "<<(d+y1)<<"\n";
}
return 0;
}