原题链接https://codeforces.com/problemset/problem/1624/C
C. Division by Two and Permutation
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given an array aa consisting of nn positive integers. You can perform operations on it.
In one operation you can replace any element of the array aiai with ⌊ai2⌋⌊ai2⌋, that is, by an integer part of dividing aiai by 22 (rounding down).
See if you can apply the operation some number of times (possible 00) to make the array aa become a permutation of numbers from 11 to nn —that is, so that it contains all numbers from 11 to nn, each exactly once.
For example, if a=[1,8,25,2]a=[1,8,25,2], n=4n=4, then the answer is yes. You could do the following:
- Replace 88 with ⌊82⌋=4⌊82⌋=4, then a=[1,4,25,2]a=[1,4,25,2].
- Replace 2525 with ⌊252⌋=12⌊252⌋=12, then a=[1,4,12,2]a=[1,4,12,2].
- Replace 1212 with ⌊122⌋=6⌊122⌋=6, then a=[1,4,6,2]a=[1,4,6,2].
- Replace 66 with ⌊62⌋=3⌊62⌋=3, then a=[1,4,3,2]a=[1,4,3,2].
Input
The first line of input data contains an integer tt (1≤t≤1041≤t≤104) —the number of test cases.
Each test case contains exactly two lines. The first one contains an integer nn (1≤n≤501≤n≤50), the second one contains integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109).
Output
For each test case, output on a separate line:
- YES if you can make the array aa become a permutation of numbers from 11 to nn,
- NO otherwise.
You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
Example
input
Copy
6 4 1 8 25 2 2 1 1 9 9 8 3 4 2 7 1 5 6 3 8 2 1 4 24 7 16 7 5 22 6 22 4 22output
Copy
YES NO YES NO NO YESNote
The first test case is explained in the text of the problem statement.
In the second test case, it is not possible to get a permutation.
AB题切的顺风顺水,C题一拿到看了眼,刚开始想的是,先sort,sort完while循环一直除2,让他等于第i位的坐标,结果发现,这样子跑出来,样例全是NO 仔细看了眼,发现,是n的排列组合,不一定从小到大。
先粘一下惨兮兮的弱智代码,记录一下自己的思维漏洞:
#include<bits/stdc++.h>
using namespace std;
int t,a[1010],i,j,k,n;
int main(){
cin>>t;
while(t--){
int f=1;
cin>>n;
for(i=1;i<=n;i++){
cin>>a[i];
}sort(a+1,a+n+1);
for(i=1;i<=n;i++){
while(a[i]>i){
a[i]=floor(a[i]/2);
}
}
for(i=1;i<=n;i++){
cout<<a[i]<<" ";
}
if(f==1){
// cout<<"YES"<<endl;
// }else{
// cout<<"NO"<<endl;
}cout<<endl;
}
}
好啦,那么AC的思路是什么呢:
优先分配,a【i】的从n到1开始标记,,比如我们标记了,n这个数,下次就跳过n了,找下一个,这样子既不会重复,也不会遗漏。标记在这个1100分的题中好像很好用,以后多做几个再看看。
贴码撤退,评论区见:!
#include<bits/stdc++.h>
using namespace std;
int t,a[1010],i,j,k,n,vis[1010];
int main(){
cin>>t;
while(t--){
int f=1;
cin>>n;
for(i=1;i<=n;i++){
cin>>a[i];
}
memset(vis,0,sizeof(vis));
for(i=1;i<=n;i++){
while(a[i]!=0&&(a[i]>n||vis[a[i]]==1)){
a[i]/=2;
}if(a[i]==0){
f=0;
break;
}vis[a[i]]=1;
}
if(f==1){
cout<<"YES"<<endl;
}else{
cout<<"NO"<<endl;
}
}
}