目录
A. Compare T-Shirt Sizes
B. Funny Permutation
C. Minimize the Thickness
D. Masha and a Beautiful Tree
镜像网站就是嘎嘎快嗷,图片传不上,就不传了。
ABCD都比较简单,明天看看能不能把E做出来,感觉对我来说有点难。
Two T-shirt sizes are given: aa and bb. The T-shirt size is either a string M or a string consisting of several (possibly zero) characters X and one of the characters S or L.
For example, strings M, XXL, S, XXXXXXXS could be the size of some T-shirts. And the strings XM, LL, SX are not sizes.
The letter M stands for medium, S for small, L for large. The letter X refers to the degree of size (from eXtra). For example, XXL is extra-extra-large (bigger than XL, and smaller than XXXL).
You need to compare two given sizes of T-shirts aa and bb.
The T-shirts are compared as follows:
- any small size (no matter how many letters X) is smaller than the medium size and any large size;
- any large size (regardless of the number of letters X) is larger than the medium size and any small size;
- the more letters X before S, the smaller the size;
- the more letters X in front of L, the larger the size.
For example:
- XXXS < XS
- XXXL > XL
- XL > M
- XXL = XXL
- XXXXXS < M
- XL > XXXS
Input
The first line of the input contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases.
Each test case consists of one line, in which aa and bb T-shirt sizes are written. The lengths of the strings corresponding to the T-shirt sizes do not exceed 5050. It is guaranteed that all sizes are correct.
Output
For each test case, print on a separate line the result of comparing aa and bb T-shirt sizes (lines "<", ">" or "=" without quotes).
很简单,思路略
#include
#include
using namespace std;
string ss,s;
unordered_map mp;
int main()
{
int t;
cin>>t;
mp['S']=0;
mp['M']=1;
mp['L']=2;
int flag;
while(t--)
{
cin>>s;
cin>>ss;
int n1=s.size();
int n2=ss.size();
if(s[n1-1]!=ss[n2-1])
{
if(mp[s[n1-1]]>mp[ss[n2-1]])
flag=-1;
else flag=1;
}
else if(s[n1-1]=='S')
{
if(n1n2) flag=-1;
else if(n1==n2) flag=0;
else flag=1;
}
if(flag==-1)
printf(">\n");
else if(flag==0)
printf("=\n");
else printf("<\n");
}
return 0;
}
A sequence of nn numbers is called permutation if it contains all numbers from 11 to nn exactly once. For example, the sequences [3,1,4,2][3,1,4,2], [11] and [2,1][2,1] are permutations, but [1,2,1][1,2,1], [0,1][0,1] and [1,3,4][1,3,4] are not.
For a given number nn you need to make a permutation pp such that two requirements are satisfied at the same time:
- For each element pipi, at least one of its neighbors has a value that differs from the value of pipi by one. That is, for each element pipi (1≤i≤n1≤i≤n), at least one of its neighboring elements (standing to the left or right of pipi) must be pi+1pi+1, or pi−1pi−1.
- the permutation must have no fixed points. That is, for every ii (1≤i≤n1≤i≤n), pi≠ipi≠i must be satisfied.
Let's call the permutation that satisfies these requirements funny.
For example, let n=4n=4. Then [4,3,1,24,3,1,2] is a funny permutation, since:
- to the right of p1=4p1=4 is p2=p1−1=4−1=3p2=p1−1=4−1=3;
- to the left of p2=3p2=3 is p1=p2+1=3+1=4p1=p2+1=3+1=4;
- to the right of p3=1p3=1 is p4=p3+1=1+1=2p4=p3+1=1+1=2;
- to the left of p4=2p4=2 is p3=p4−1=2−1=1p3=p4−1=2−1=1.
- for all ii is pi≠ipi≠i.
For a given positive integer nn, output any funny permutation of length nn, or output -1 if funny permutation of length nn does not exist.
Input
The first line of input data contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases.
The description of the test cases follows.
Each test case consists of f single line containing one integer nn (2≤n≤2⋅1052≤n≤2⋅105).
It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105.
Output
For each test case, print on a separate line:
- any funny permutation pp of length nn;
- or the number -1 if the permutation you are looking for does not exist.
这题也很简单,只有当n=3的时候是输出-1的,其他都有答案。
题意,对于一个序列,每一个位置i的左边或者右边必须有一个大于它或者小于它的数,这个序列就是好的。给你一个n,问你能不能构成一个好序列,输出任意一个好序列。
并且这个序列的a[i]!=i
思路:很简单,我们会发现当n为偶数的时候,我们只要把原数列两两交换就可以。
如果n为奇数,那么我们首先输出n,然后输出n-1,然后从1到n-2依次输出就可以了。
#include
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
scanf("%d",&n);
if(n==3)
{
printf("-1\n");
continue;
}
if(n%2==0)
{
for(int i=2;i<=n;i=i+2)
{
if(i==2) printf("%d %d",i,i-1);
else printf(" %d %d",i,i-1);
}
printf("\n");
}
else
{
printf("%d %d",n,n-1);
for(int i=1;i<=n-2;i++)
{
printf(" %d",i);
}
printf("\n");
}
}
return 0;
}
You are given a sequence a=[a1,a2,…,an]a=[a1,a2,…,an] consisting of nn positive integers.
Let's call a group of consecutive elements a segment. Each segment is characterized by two indices: the index of its left end and the index of its right end. Denote by a[l,r]a[l,r] a segment of the sequence aa with the left end in ll and the right end in rr, i.e. a[l,r]=[al,al+1,…,ar]a[l,r]=[al,al+1,…,ar].
For example, if a=[31,4,15,92,6,5]a=[31,4,15,92,6,5], then a[2,5]=[4,15,92,6]a[2,5]=[4,15,92,6], a[5,5]=[6]a[5,5]=[6], a[1,6]=[31,4,15,92,6,5]a[1,6]=[31,4,15,92,6,5] are segments.
We split the given sequence aa into segments so that:
- each element is in exactly one segment;
- the sums of elements for all segments are equal.
For example, if aa = [55,45,30,30,40,10055,45,30,30,40,100], then such a sequence can be split into three segments: a[1,2]=[55,45]a[1,2]=[55,45], a[3,5]=[30,30,40]a[3,5]=[30,30,40], a[6,6]=[100]a[6,6]=[100]. Each element belongs to exactly segment, the sum of the elements of each segment is 100100.
Let's define thickness of split as the length of the longest segment. For example, the thickness of the split from the example above is 33.
Find the minimum thickness among all possible splits of the given sequence of aa into segments in the required way.
Input
The first line contains a single integer tt (1≤t≤1001≤t≤100) — the number of test cases.
Each test case is described by two lines.
The first line of each test case contains a single integer nn (1≤n≤20001≤n≤2000) — the length of the sequence aa.
The second line of each test case contains exactly nn integers: a1,a2,…,ana1,a2,…,an (1≤ai≤1061≤ai≤106) — elements of the sequence aa.
It is guaranteed that the sum of nn for all test cases does not exceed 20002000.
Output
For each test case, output one integer — the minimum possible thickness of a split of the sequence aa into segments.
Note that there always exist a split, you can always consider whole sequence as one segment.
题意,把一个序列分割成不同的子序列,使每个子序列里面的元素之和相同。
求出子序列长度的最大值最小的时候,对应的最长序列的长度。
思路,暴力枚举就行。
#include
using namespace std;
const int N=2010;
int a[N];
long long int s[N];
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
scanf("%d",&n);
long long int sum=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
s[i]=sum;
//cout<s[i])
{
break;
}
else if(cnt==s[i]&&j!=n)
{
cnt=0;
x=max(x,j-k+1);
k=j+1;
}
j++;
}
if(j>n)
{
if(cnt==s[i])
{
x=max(x,j-k);
maxx=min(maxx,x);
}
}
}
printf("%d\n",maxx);
}
return 0;
}
The girl named Masha was walking in the forest and found a complete binary tree of height nn and a permutation pp of length m=2nm=2n.
A complete binary tree of height nn is a rooted tree such that every vertex except the leaves has exactly two sons, and the length of the path from the root to any of the leaves is nn. The picture below shows the complete binary tree for n=2n=2.
A permutation is an array consisting of nn different integers from 11 to nn. For example, [2,3,1,5,42,3,1,5,4] is a permutation, but [1,2,21,2,2] is not (22 occurs twice), and [1,3,41,3,4] is also not a permutation (n=3n=3, but there is 44 in the array).
Let's enumerate mm leaves of this tree from left to right. The leaf with the number ii contains the value pipi (1≤i≤m1≤i≤m).
For example, if n=2n=2, p=[3,1,4,2]p=[3,1,4,2], the tree will look like this:
Masha considers a tree beautiful if the values in its leaves are ordered from left to right in increasing order.
In one operation, Masha can choose any non-leaf vertex of the tree and swap its left and right sons (along with their subtrees).
For example, if Masha applies this operation to the root of the tree discussed above, it will take the following form:
Help Masha understand if she can make a tree beautiful in a certain number of operations. If she can, then output the minimum number of operations to make the tree beautiful.
Input
The first line contains single integer tt (1≤t≤1041≤t≤104) — number of test cases.
In each test case, the first line contains an integer mm (1≤m≤2621441≤m≤262144), which is a power of two — the size of the permutation pp.
The second line contains mm integers: p1,p2,…,pmp1,p2,…,pm (1≤pi≤m1≤pi≤m) — the permutation pp.
It is guaranteed that the sum of mm over all test cases does not exceed 3⋅1053⋅105.
Output
For each test case in a separate line, print the minimum possible number of operations for which Masha will be able to make the tree beautiful or -1, if this is not possible.
题意:给你一棵完美二叉树的叶子节点序列,问你能不能通过交换一个非叶子节点的两个子树,来让最后的叶子节点序列是升序的。
思路:看起来麻烦,但是想清楚了就很简单,首先对初始叶子节点来说,1号和2号必须是相差1的,这样才能通过交换把他们变成正确的序列,如果一号节点是1但是二号节点是3的话,那么他们根本不可能变成正确序列,同样,运用递归的思想,每一层都应该是这样,为了减少时间复杂度,我们可以用一个值代替一串已经交换完成的子序列。把他们放到队列里,每次出两个值,直到队列的size为1,或者中途出现不满足的情况。
每交换一个逆序对的时候,ans++。
#include
using namespace std;
const int N=300010;
int a[N];
queue v;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
if(n==1)
{
printf("0\n");
continue;
}
long long int ans=0;
for(int i=1;i<=n;i+=2)
{
if(abs(a[i]-a[i+1])!=1)
break;
if(a[i]!=min(a[i],a[i+1]))
ans++;
v.push(min(a[i],a[i+1]));
}
if(v.size()!=n/2)
{
while(!v.empty())
v.pop();
printf("-1\n");
continue;
}
int cnt=2;
int flag=0;
while(v.size()!=1)
{
int len=v.size();
int i;
for(i=1;i<=len;i+=2)
{
int x=v.front();
v.pop();
int y=v.front();
v.pop();
if(abs(x-y)!=cnt)
{
break;
}
if(x!=min(x,y))
ans++;
v.push(min(x,y));
}
if(i<=len)
{
flag=1;
break;
}
cnt*=2;
}
if(flag==1)
{
while(!v.empty())
v.pop();
printf("-1\n");
continue;
}
else
{
printf("%lld\n",ans);
}
while(!v.empty())
v.pop();
}
return 0;
}
明天瞅瞅e题我能做不