#A
#B
#C
继续上次的cf坑
A
A. Three swimmers
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.
It takes the first swimmer exactly a minutes to swim across the entire pool and come back, exactly b minutes for the second swimmer and c minutes for the third. Hence, the first swimmer will be on the left side of the pool after 0, a, 2a, 3a, … minutes after the start time, the second one will be at 0, b, 2b, 3b, … minutes, and the third one will be on the left side of the pool after 0, c, 2c, 3c, … minutes.
You came to the left side of the pool exactly p minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
Input
The first line of the input contains a single integer t (1≤t≤1000) — the number of test cases. Next t lines contains test case descriptions, one per line.
Each line contains four integers p, a, b and c (1≤p,a,b,c≤1018), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
Output
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
Example
inputCopy
4
9 5 4 8
2 6 10 9
10 2 5 10
10 9 9 9
outputCopy
1
4
0
8
Note
In the first test case, the first swimmer is on the left side in 0,5,10,15,… minutes after the start time, the second swimmer is on the left side in 0,4,8,12,… minutes after the start time, and the third swimmer is on the left side in 0,8,16,24,… minutes after the start time. You arrived at the pool in 9 minutes after the start time and in a minute you will meet the first swimmer on the left side.
In the second test case, the first swimmer is on the left side in 0,6,12,18,… minutes after the start time, the second swimmer is on the left side in 0,10,20,30,… minutes after the start time, and the third swimmer is on the left side in 0,9,18,27,… minutes after the start time. You arrived at the pool 2 minutes after the start time and after 4 minutes meet the first swimmer on the left side.
In the third test case, you came to the pool 10 minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!
In the fourth test case, all swimmers are located on the left side in 0,9,18,27,… minutes after the start time. You arrived at the pool 10 minutes after the start time and after 8 minutes meet all three swimmers on the left side.
题目大意:A,B,C三个人来回一趟需要a,b,c分钟,有一名工作人员p分钟到起点,询问最少等待几分钟就可以看到人靠岸(也就是完成一个来回)。
解题思路:
写出一个式子然后对三个人取最值就行了,那么只考虑a。
对a来说,工作人员等待的时间是 (q + a - 1) / a * a - q,至于为什么,大家可以自己思考一下。
代码如下:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
using namespace std;
#define pb emplace_back
#define PII pair<int,int>
#define ll long long
#define mp make_pair
const int maxn = 2e6 + 10;
void solve() {
long long q, a, b ,c,ans = 0;
cin >> q >> a >> b >> c;
ans = min((q + a - 1) / a * a - q,min((q + b - 1) / b * b - q, (q + c - 1) / c * c - q));
cout << ans << '\n';
}
int main()
{
cin.sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
B
B. Card Deck
You have a deck of n cards, and you’d like to reorder it to a new one.
Each card has a value between 1 and n equal to pi. All pi are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. p1 stands for the bottom card, pn is the top card.
In each step you pick some integer k>0, take the top k cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)
Let’s define an order of a deck as ∑i=1nnn−i⋅pi.
Given the original deck, output the deck with maximum possible order you can make using the operation above.
Input
The first line contains a single integer t (1≤t≤1000) — the number of test cases.
The first line of each test case contains the single integer n (1≤n≤105) — the size of deck you have.
The second line contains n integers p1,p2,…,pn (1≤pi≤n; pi≠pj if i≠j) — values of card in the deck from bottom to top.
It’s guaranteed that the sum of n over all test cases doesn’t exceed 105.
Output
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top.
If there are multiple answers, print any of them.
Example
inputCopy
4
4
1 2 3 4
5
1 5 2 4 3
6
4 2 5 3 6 1
1
1
outputCopy
4 3 2 1
5 2 4 3 1
6 1 5 3 4 2
1
Note
In the first test case, one of the optimal strategies is the next one:
take 1 card from the top of p and move it to p′: p becomes [1,2,3], p′ becomes [4];
take 1 card from the top of p: p becomes [1,2], p′ becomes [4,3];
take 1 card from the top of p: p becomes [1], p′ becomes [4,3,2];
take 1 card from the top of p: p becomes empty, p′ becomes [4,3,2,1].
In result, p′ has order equal to 43⋅4+42⋅3+41⋅2+40⋅1 = 256+48+8+1=313.
In the second test case, one of the optimal strategies is:
take 4 cards from the top of p and move it to p′: p becomes [1], p′ becomes [5,2,4,3];
take 1 card from the top of p and move it to p′: p becomes empty, p′ becomes [5,2,4,3,1];
In result, p′ has order equal to 54⋅5+53⋅2+52⋅4+51⋅3+50⋅1 = 3125+250+100+15+1=3491.
In the third test case, one of the optimal strategies is:
take 2 cards from the top of p and move it to p′: p becomes [4,2,5,3], p′ becomes [6,1];
take 2 cards from the top of p and move it to p′: p becomes [4,2], p′ becomes [6,1,5,3];
take 2 cards from the top of p and move it to p′: p becomes empty, p′ becomes [6,1,5,3,4,2].
In result, p′ has order equal to 65⋅6+64⋅1+63⋅5+62⋅3+61⋅4+60⋅2 = 46656+1296+1080+108+24+2=49166.
题目大意:
给定一个数组(元素互不相同),从后往前找最大值,输出并且删除最大值后面的部分,继续这步骤直至数组被删完。
解题思路:
挑一个简单而又好想(才不是因为我写不出来)的思路来讲,我们只要在输入的时候预处理对于每一个数字之前的最大值是多少,下标是多少,然后直接模拟就行了。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
using namespace std;
#define pb emplace_back
#define PII pair<int,int>
#define ll long long
#define mp make_pair
const int maxn = 2e6 + 10;
int st[maxn];//这个下标之前的最大值
int inde[maxn];//下标的数字对应的下标
void solve() {
int n;
cin >> n;
int ma = n;
vector<int>a(n);
for (int i = 0; i < n; ++i)
{
cin >> a[i];
if (i)st[i] = max(a[i], st[i - 1]);
else st[0] = a[0];
inde[a[i]] = i;
}
int ind = n - 1;
while (ind >= 0)
{
int mav = inde[st[ind]];
for (int j = mav; j <= ind; ++j)
{
cout << a[j] << ' ';
}ind = mav - 1;
}
puts("");
memset(st, 0, sizeof st);
}
int main()
{
cin.sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
C
C. Maximum width
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: s of length n and t of length m.
A sequence p1,p2,…,pm, where 1≤p1<p2<…<pm≤n, is called beautiful, if spi=ti for all i from 1 to m. The width of a sequence is defined as max1≤i<m(pi+1−pi).
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings s and t there is at least one beautiful sequence.
Input
The first input line contains two integers n and m (2≤m≤n≤2⋅105) — the lengths of the strings s and t.
The following line contains a single string s of length n, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string t of length m, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
Output
Output one integer — the maximum width of a beautiful sequence.
Examples
inputCopy
5 3
abbbc
abc
outputCopy
3
inputCopy
5 2
aaaaa
aa
outputCopy
4
inputCopy
5 5
abcdf
abcdf
outputCopy
1
inputCopy
2 2
ab
ab
outputCopy
1
Note
In the first example there are two beautiful sequences of width 3: they are {1,2,5} and {1,4,5}.
In the second example the beautiful sequence with the maximum width is {1,5}.
In the third example there is exactly one beautiful sequence — it is {1,2,3,4,5}.
In the fourth example there is exactly one beautiful sequence — it is {1,2}.
题目大意:
找出a串中与b串相同的子序列,输出该序列在a串中相邻两个元素坐标差的最大值
解题思路:
从前往后扫描一遍,可以知道最前面的子序列是哪个,从后往前扫一遍,可以知道最后面的子序列是哪个,然后比对这两个子序列,找相邻元素差值的最值就可以。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
using namespace std;
#define pb emplace_back
#define PII pair<int,int>
#define ll long long
#define mp make_pair
const int maxn = 2e6 + 10;
vector<int>zuiqian(maxn), zuihou(maxn);
void solve() {
int lena, lenb;
string a, b;
cin >> lena >> lenb >> a >> b;
int ind = -1;
for (int i = 0; i < lenb; i++)
{
while (a[++ind] != b[i]);
zuiqian[i] = ind;
}
ind = lena;
for ( int i = lenb-1; i >= 0; i --)
{
while (a[--ind] != b[i]);
zuihou[i] = ind;
}
int ans = 1;
for (int i = 1; i < lenb; ++i)
{
ans = max(ans, zuihou[i] - zuiqian[i - 1]);
}
cout << ans << '\n';
}
int main()
{
cin.sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}