A. Bad Ugly Numbers
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a integer n (n>0). Find any integer s which satisfies these conditions, or report that there are no such numbers:
In the decimal representation of s:
s>0,
s consists of n digits,
no digit in s equals 0,
s is not divisible by any of it’s digits.
Input
The input consists of multiple test cases. The first line of the input contains a single integer t (1≤t≤400), the number of test cases. The next t lines each describe a test case.
Each test case contains one positive integer n (1≤n≤105).
It is guaranteed that the sum of n for all test cases does not exceed 105.
Output
For each test case, print an integer s which satisfies the conditions described above, or “-1” (without quotes), if no such number exists. If there are multiple possible solutions for s, print any solution.
Example
inputCopy
4
1
2
3
4
outputCopy
-1
57
239
6789
Note
In the first test case, there are no possible solutions for s consisting of one digit, because any such solution is divisible by itself.
For the second test case, the possible solutions are: 23, 27, 29, 34, 37, 38, 43, 46, 47, 49, 53, 54, 56, 57, 58, 59, 67, 68, 69, 73, 74, 76, 78, 79, 83, 86, 87, 89, 94, 97, and 98.
For the third test case, one possible solution is 239 because 239 is not divisible by 2, 3 or 9 and has three digits (none of which equals zero).
题意:构造一个n位数 大于0 满足位数中不出现0 构造的数不会被其中任何位数整除 如果不存在输出-1
思路:题目没有说数组不能重复 所以就嘿嘿 我用的是57777… 有好几种 233…(tql)也是对的 只有1的时候是不行的
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pb(x) push_back(x)
#define debug(x) cout<<"..........."<<x<<endl;
#define fi first
#define se second
const int N = 1e5+5;
const int M = 1e3+5;
const int mod = 1e9+7;
const int INF = 0x3f3f3f3f;
int main()
{
int t;
while(cin >> t)
{
while(t --)
{
int n;
cin >> n;
if(n == 1)
{
printf("-1\n");
continue;
}
printf("5");
for(int i = 1;i < n;i ++)
printf("7");
printf("\n");
}
}
return 0;
}
B. Maximums
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Alicia has an array, a1,a2,…,an, of non-negative integers. For each 1≤i≤n, she has found a non-negative integer xi=max(0,a1,…,ai−1). Note that for i=1, xi=0.
For example, if Alicia had the array a={0,1,2,0,3}, then x={0,0,1,2,2}.
Then, she calculated an array, b1,b2,…,bn: bi=ai−xi.
For example, if Alicia had the array a={0,1,2,0,3}, b={0−0,1−0,2−1,0−2,3−2}={0,1,1,−2,1}.
Alicia gives you the values b1,b2,…,bn and asks you to restore the values a1,a2,…,an. Can you help her solve the problem?
Input
The first line contains one integer n (3≤n≤200000) – the number of elements in Alicia’s array.
The next line contains n integers, b1,b2,…,bn (−109≤bi≤109).
It is guaranteed that for the given array b there is a solution a1,a2,…,an, for all elements of which the following is true: 0≤ai≤109.
Output
Print n integers, a1,a2,…,an (0≤ai≤109), such that if you calculate x according to the statement, b1 will be equal to a1−x1, b2 will be equal to a2−x2, …, and bn will be equal to an−xn.
It is guaranteed that there exists at least one solution for the given tests. It can be shown that the solution is unique.
Examples
inputCopy
5
0 1 1 -2 1
outputCopy
0 1 2 0 3
inputCopy
3
1000 999999000 -1000000000
outputCopy
1000 1000000000 0
inputCopy
5
2 1 2 2 3
outputCopy
2 3 5 7 10
Note
The first test was described in the problem statement.
In the second test, if Alicia had an array a={1000,1000000000,0}, then x={0,1000,1000000000} and b={1000−0,1000000000−1000,0−1000000000}={1000,999999000,−1000000000}.
题意:根据题意 开始有个a数组 然后可以得到 x数组 xi 是前i项的最大值 b数组为ai - bi 给出b数组 还原a数组
思路:每次选最大值 加上 就好
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pb(x) push_back(x)
#define debug(x) cout<<"..........."<<x<<endl;
#define fi first
#define se second
const int N = 2e5+5;
const int M = 1e3+5;
const int mod = 1e9+7;
const int INF = 0x3f3f3f3f;
LL a[N],b[N];
int main()
{
int n;
cin >> n;
for(int i = 1;i <= n;i ++)
cin >> a[i];
LL maxx = 0;
LL sum = 0;
for(int i = 1;i <= n;i ++)
{
b[i] = a[i] + maxx;
maxx = max(maxx , b[i]);
printf("%lld ",b[i]);
}
printf("\n");
return 0;
}
C. Permutation Partitions
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a permutation p1,p2,…,pn of integers from 1 to n and an integer k, such that 1≤k≤n. A permutation means that every number from 1 to n is contained in p exactly once.
Let’s consider all partitions of this permutation into k disjoint segments. Formally, a partition is a set of segments {[l1,r1],[l2,r2],…,[lk,rk]}, such that:
1≤li≤ri≤n for all 1≤i≤k;
For all 1≤j≤n there exists exactly one segment [li,ri], such that li≤j≤ri.
Two partitions are different if there exists a segment that lies in one partition but not the other.
Let’s calculate the partition value, defined as ∑i=1kmaxli≤j≤ripj, for all possible partitions of the permutation into k disjoint segments. Find the maximum possible partition value over all such partitions, and the number of partitions with this value. As the second value can be very large, you should find its remainder when divided by 998244353.
Input
The first line contains two integers, n and k (1≤k≤n≤200000) — the size of the given permutation and the number of segments in a partition.
The second line contains n different integers p1,p2,…,pn (1≤pi≤n) — the given permutation.
Output
Print two integers — the maximum possible partition value over all partitions of the permutation into k disjoint segments and the number of such partitions for which the partition value is equal to the maximum possible value, modulo 998244353.
Please note that you should only find the second value modulo 998244353.
Examples
inputCopy
3 2
2 1 3
outputCopy
5 2
inputCopy
5 5
2 1 5 3 4
outputCopy
15 1
inputCopy
7 3
2 7 3 1 5 4 6
outputCopy
18 6
Note
In the first test, for k=2, there exists only two valid partitions: {[1,1],[2,3]} and {[1,2],[3,3]}. For each partition, the partition value is equal to 2+3=5. So, the maximum possible value is 5 and the number of partitions is 2.
In the third test, for k=3, the partitions with the maximum possible partition value are {[1,2],[3,5],[6,7]}, {[1,3],[4,5],[6,7]}, {[1,4],[5,5],[6,7]}, {[1,2],[3,6],[7,7]}, {[1,3],[4,6],[7,7]}, {[1,4],[5,6],[7,7]}. For all of them, the partition value is equal to 7+5+6=18.
The partition {[1,2],[3,4],[5,7]}, however, has the partition value 7+3+6=16. This is not the maximum possible value, so we don’t count it.
题意:将一个序列 构造成多个长度为k的序列 且使得这几个序列的和为最大值 输出最大值并求出构造的方式有多少种
思路:贪心选出前k大的值 这样最大值一定是满足的 记录他们的下标 方式就是ans * (v[i] - v[i - 1]) 累乘即可
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pb(x) push_back(x)
#define debug(x) cout<<"..........."<<x<<endl;
#define fi first
#define se second
const int N = 2e5+5;
const int M = 1e3+5;
const int mod = 998244353;
const int INF = 0x3f3f3f3f;
pair <LL,int> a[N];
int main()
{
int n , k;
cin >> n >> k;
for(int i = 1;i <= n;i ++)
{
cin >> a[i].fi;
a[i].se = i;
}
sort(a + 1,a + 1 + n);
LL res = 0;
vector <int> v;
for(int i = n - k + 1;i <= n;i ++)
res += a[i].fi,v.pb(a[i].se);
sort(v.begin(),v.end());
LL ans = 1;
for(int i = 1;i < v.size();i ++)
ans = ans * (v[i] - v[i - 1])%mod;
cout << res << ' ' << ans << '\n';
return 0;
}
This is the easy version of the problem. The difference is the constraint on the sum of lengths of strings and the number of test cases. You can make hacks only if you solve all versions of this task.
You are given a string s, consisting of lowercase English letters. Find the longest string, t, which satisfies the following conditions:
The length of t does not exceed the length of s.
t is a palindrome.
There exists two strings a and b (possibly empty), such that t=a+b ( “+” represents concatenation), and a is prefix of s while b is suffix of s.
Input
The input consists of multiple test cases. The first line contains a single integer t (1≤t≤1000), the number of test cases. The next t lines each describe a test case.
Each test case is a non-empty string s, consisting of lowercase English letters.
It is guaranteed that the sum of lengths of strings over all test cases does not exceed 5000.
Output
For each test case, print the longest string which satisfies the conditions described above. If there exists multiple possible solutions, print any of them.
Example
inputCopy
5
a
abcdfdcecba
abbaxyzyx
codeforces
acbba
outputCopy
a
abcdfdcba
xyzyx
c
abba
Note
In the first test, the string s=“a” satisfies all conditions.
In the second test, the string “abcdfdcba” satisfies all conditions, because:
Its length is 9, which does not exceed the length of the string s, which equals 11.
It is a palindrome.
“abcdfdcba” = “abcdfdc” + “ba”, and “abcdfdc” is a prefix of s while “ba” is a suffix of s.
It can be proven that there does not exist a longer string which satisfies the conditions.
In the fourth test, the string “c” is correct, because “c” = “c” + “” and a or b can be empty. The other possible solution for this test is “s”.
题意:从字符串s中构造回文串t 要求长度小于s 并且在s中找得到两个字符串a和b使得 t = a + b
思路:先从两边开始构造回文串 然后在剩下的字符串去找
#include <bits/stdc++.h>
using namespace std;
bool ok(string s,int l,int r)
{
while(l<=r&&s[l]==s[r])
++l,--r;
return l>r;
}
void run()
{
string s;
cin >> s;
int l=0,r=s.size()-1;
while(l< r&&s[l] == s[r])
++l,--r;
int rr,ll;
for(rr =r; rr >= l; rr --)
if(ok(s,l,rr))
break;
for(ll = l; ll <= r; ll ++)
if(ok(s,ll,r))
break;
cout<<s.substr(0,l)
<<((rr - l > r - ll)?s.substr(l,rr-l+1):s.substr(ll,r-ll+1))
<< s.substr(r+1)
<< "\n";
}
int main()
{
int t;
cin >> t;
while(t --)
run();
return 0;
}
简单版的话就是纯粹暴力 难度更高的可以考虑回文自动机 或者 马拉车 但是我不会