持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第6天,点击查看活动详情
A
You work in the quality control department of technical support for a large company. Your job is to make sure all client issues have been resolved.
Today you need to check a copy of a dialog between a client and a technical support manager. According to the rules of work, each message of the client must be followed by one or several messages, which are the answer of a support manager. However, sometimes clients ask questions so quickly that some of the manager's answers to old questions appear after the client has asked some new questions.
Due to the privacy policy, the full text of messages is not available to you, only the order of messages is visible, as well as the type of each message: a customer question or a response from the technical support manager. It is guaranteed that the dialog begins with the question of the client.
You have to determine, if this dialog may correspond to the rules of work described above, or the rules are certainly breached.
Input
Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤5001≤t≤500). Description of the test cases follows.
The first line of each test case contains one integer nn (1≤n≤1001≤n≤100) — the total number of messages in the dialog.
The second line of each test case consists of nn characters "Q" and "A", describing types of messages in the dialog in chronological order. Character "Q" denotes the message with client question, and character "A" — the message with technical support manager answer. It is guaranteed that the first character in the line equals to "Q".
Output
For each test case print "Yes" (without quotes) if dialog may correspond to the rules of work, or "No" (without quotes) otherwise.
Example
input
Copy
5
4
QQAA
4
QQAQ
3
QAA
1
Q
14
QAQQAQAAQQQAAA
output
Copy
Yes
No
Yes
No
Yes
Note
In the first test case the two questions from the client are followed with two specialist's answers. So this dialog may correspond to the rules of work.
In the second test case one of the first two questions was not answered.
In the third test case the technical support manager sent two messaged as the answer to the only message of the client.
思路:就是遍历过去要保证A的个数大于等于Q的个数,同时后面的A能把Q消掉
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve()
{
int n;
scanf("%d ",&n);
string s;
cin >> s;
int cnt1 = 0,cnt2 = 0;
stack<char>c;
for(int i = 0;i < int(s.size());i ++)
{
if(c.size())
{
if(s[i] == 'A')
c.pop();
}
if(s[i] == 'Q')
c.push(s[i]);
}
if(c.size())
cout << "no" << '\n';
else
cout << "yes" << '\n';
}
int main()
{
int t;
t = 1;
scanf("%d",&t);
while(t--)
{
solve();
}
}
B
For his birthday, Kevin received the set of pairwise distinct numbers 1,2,3,…,n1,2,3,…,n as a gift.
He is going to arrange these numbers in a way such that the minimum absolute difference between two consecutive numbers be maximum possible. More formally, if he arranges numbers in order p1,p2,…,pnp1,p2,…,pn, he wants to maximize the value
mini=1n−1|pi+1−pi|,mini=1n−1|pi+1−pi|,
where |x||x| denotes the absolute value of xx.
Help Kevin to do that.
Input
Each test consists of multiple test cases. The first line contains a single integer tt (1≤t≤1001≤t≤100) — the number of test cases. Description of the test cases follows.
The only line of each test case contains an integer nn (2≤n≤10002≤n≤1000) — the size of the set.
Output
For each test case print a single line containing nn distinct integers p1,p2,…,pnp1,p2,…,pn (1≤pi≤n1≤pi≤n) describing the arrangement that maximizes the minimum absolute difference of consecutive elements.
Formally, you have to print a permutation pp which maximizes the value mini=1n−1|pi+1−pi|mini=1n−1|pi+1−pi|.
If there are multiple optimal solutions, print any of them.
Example
input
Copy
2
4
3
output
Copy
2 4 1 3
1 2 3
Note
In the first test case the minimum absolute difference of consecutive elements equals min{|4−2|,|1−4|,|3−1|}=min{2,3,2}=2min{|4−2|,|1−4|,|3−1|}=min{2,3,2}=2. It's easy to prove that this answer is optimal.
In the second test case each permutation of numbers 1,2,31,2,3 is an optimal answer. The minimum absolute difference of consecutive elements equals to 11.
思路:找规律发现他每次都是n/2这个点在最开头,然后分下去,判断一下奇偶就可以了
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve()
{
int n;
scanf("%d ",&n);
vector<int>ans;
if(n%2)
{
int x = n/2;
x ++;
//vector<int>ans;
for(;x>=1;x--,n--)
{
ans.push_back(x);
if(x!=1)
ans.push_back(n);
}
}
else
{
int x = n/2;
//x ++;
//vector<int>ans;
for(;x>=1;x--,n--)
{
ans.push_back(x);
ans.push_back(n);
}
}
for(int i = 0;i < ans.size();i ++)
cout << ans[i] << " ";
cout << '\n';
}
int main()
{
int t;
t = 1;
scanf("%d",&t);
while(t--)
{
solve();
}
}
c
This is the easy version of the problem. The difference is that in this version the array can not contain zeros. You can make hacks only if both versions of the problem are solved.
You are given an array [a1,a2,…an][a1,a2,…an] consisting of integers −1−1 and 11. You have to build a partition of this array into the set of segments [l1,r1],[l2,r2],…,[lk,rk][l1,r1],[l2,r2],…,[lk,rk] with the following property:
- Denote the alternating sum of all elements of the ii-th segment as sisi: sisi = ali−ali+1+ali+2−ali+3+…±ariali−ali+1+ali+2−ali+3+…±ari. For example, the alternating sum of elements of segment [2,4][2,4] in array [1,0,−1,1,1][1,0,−1,1,1] equals to 0−(−1)+1=20−(−1)+1=2.
- The sum of sisi over all segments of partition should be equal to zero.
Note that each sisi does not have to be equal to zero, this property is about sum of sisi over all segments of partition.
The set of segments [l1,r1],[l2,r2],…,[lk,rk][l1,r1],[l2,r2],…,[lk,rk] is called a partition of the array aa of length nn if 1=l1≤r1,l2≤r2,…,lk≤rk=n1=l1≤r1,l2≤r2,…,lk≤rk=n and ri+1=li+1ri+1=li+1 for all i=1,2,…k−1i=1,2,…k−1. In other words, each element of the array must belong to exactly one segment.
You have to build a partition of the given array with properties described above or determine that such partition does not exist.
Note that it is not required to minimize the number of segments in the partition.
Input
Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤100001≤t≤10000). Description of the test cases follows.
The first line of each test case contains an integer nn (1≤n≤2000001≤n≤200000) — the length of the array aa.
The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (aiai is −1−1 or 11) — the elements of the given array.
It's guaranteed that the sum of nn over all test cases does not exceed 200000200000.
Output
For each test case, if required partition does not exist, print −1−1. Otherwise, print an integer kk — the number of segments in the partition.
Then in the ii-th of the following kk lines print two integers lili and riri — description of the ii-th segment. The following conditions should be satisfied:
- li≤rili≤ri for each ii from 11 to kk.
- li+1=ri+1li+1=ri+1 for each ii from 11 to (k−1)(k−1).
- l1=1,rk=nl1=1,rk=n.
If there are multiple correct partitions of the array, print any of them.
Example
input
Copy
4
4
1 1 1 1
6
-1 1 1 1 1 1
3
1 -1 1
1
1
output
Copy
1
1 4
2
1 3
4 6
-1
-1
Note
In the first test case we can build a partition of one segment of length 44. The sum of this segment will be equal to 1−1+1−1=01−1+1−1=0.
In the second test case we can build a partition of two segments of length 33. The sum of the first segment will be equal to −1−1+1=−1−1−1+1=−1, and the sum of the second segment: 1−1+1=11−1+1=1. So, the total sum will be equal to −1+1=0−1+1=0.
In the third and in the fourth test cases it can be proved that there are no required partition.
思路:两个两个一判断,如果这两个相同那么他们一定为0,如果不同就把他们分开成单个区间最后相加还是0
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[200100];
void solve()
{
int n;
scanf("%d ",&n);
for(int i = 1;i <= n;i ++)
cin >> a[i];
if(n%2)
{
cout << -1 << '\n';
return ;
}
int cnt = 0,num = 0;
vector<pair<int,int> > ans;
int j = 0;
for(int i = 1;i <= n;i+=2)
{
//cnt++;
if(a[i] == a[i+1])
ans.push_back({i,i+1});
else
{
ans.push_back({i,i});
ans.push_back({i+1,i+1});
}
}
//if(num == 0)
{
cout << ans.size() << '\n';
for(auto c:ans)
{
cout << c.first << " " << c.second << '\n';
}
}
//return ;
// else
// cout << -1 << '\n';
}
int main()
{
int t;
t = 1;
scanf("%d",&t);
while(t--)
{
solve();
}
}
D
You are given an integer xx and an array of integers a1,a2,…,ana1,a2,…,an. You have to determine if the number a1!+a2!+…+an!a1!+a2!+…+an! is divisible by x!x!.
Here k!k! is a factorial of kk — the product of all positive integers less than or equal to kk. For example, 3!=1⋅2⋅3=63!=1⋅2⋅3=6, and 5!=1⋅2⋅3⋅4⋅5=1205!=1⋅2⋅3⋅4⋅5=120.
Input
The first line contains two integers nn and xx (1≤n≤5000001≤n≤500000, 1≤x≤5000001≤x≤500000).
The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤x1≤ai≤x) — elements of given array.
Output
In the only line print "Yes" (without quotes) if a1!+a2!+…+an!a1!+a2!+…+an! is divisible by x!x!, and "No" (without quotes) otherwise.
Examples
input
Copy
6 4
3 2 2 2 3 3
output
Copy
Yes
input
Copy
8 3
3 2 2 2 2 2 1 1
output
Copy
Yes
input
Copy
7 8
7 7 7 7 7 7 7
output
Copy
No
input
Copy
10 5
4 3 2 1 4 3 2 4 3 4
output
Copy
No
input
Copy
2 500000
499999 499999
output
Copy
No
Note
In the first example 3!+2!+2!+2!+3!+3!=6+2+2+2+6+6=243!+2!+2!+2!+3!+3!=6+2+2+2+6+6=24. Number 2424 is divisible by 4!=244!=24.
In the second example 3!+2!+2!+2!+2!+2!+1!+1!=183!+2!+2!+2!+2!+2!+1!+1!=18, is divisible by 3!=63!=6.
In the third example 7!+7!+7!+7!+7!+7!+7!=7⋅7!7!+7!+7!+7!+7!+7!+7!=7⋅7!. It is easy to prove that this number is not divisible by 8!8!.
思路:你会发现都是123....这样下去所以只要保证可以和x!就是123....*n一个一个消除就可以
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int cnt[500100];
void solve()
{
int n,x;
cin >> n >> x;
for (int i = 0; i < n; i++) {
int input;
cin >> input;
cnt[input]++;
}
for (int i = 1; i <= x; i++) {
int t = cnt[i] / (i + 1);
cnt[i] -= t * (i + 1);
cnt[i + 1] += t;
}
bool isYes = true;
for (int i = 1; i < x; i++) {
if (cnt[i] > 0) {
isYes = false;
break;
}
}
if (isYes)
cout << "YES\n";
else
cout << "NO\n";
}
int main()
{
int t;
t = 1;
//scanf("%d",&t);
while(t--)
{
solve();
}
}