2020.4.19 个人rating赛 解题+补题报告

A - Buggy Sorting

 1.题意

  给定一个排序方法使n个数非降序排列,找出使它排序不成功的反例,没有反例则输出-1 。

 2.题解

  给定的排序方法类似于冒泡排序,用给定的方法试验即可。发现当n=1或n=2时,排序成功;当n>2时,对于一组降序数,排序会失败。

 3.代码

 1 #include<bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int maxn=1e5+5;
 5 int n;
 6 int a[maxn];
 7 int main(){
 8     ios::sync_with_stdio(false);
 9     cin>>n; 
10 //    for(int i=1;i<=n;i++)
11 //        cin>>a[i];
12     
13     if(n==1||n==2){
14         cout<<-1<<endl;
15         return 0;
16     }
17     
18 //    for(int i=1;i<=n-1;i++){
19 //        for(int j=i;j<=n-1;j++){
20 //            if(a[j]>a[j+1]){
21 //                int temp=a[j];
22 //                a[j]=a[j+1];
23 //                a[j+1]=temp;
24 //            }
25 //        }
26 //    }
27     cout<<n;
28     for(int i=1;i<n;i++)
29         cout<<' '<<n-i;
30         
31     return 0;
32 }

B - Increase and Decrease

 1.题意

  给定n个数,可以任选其中两个数,使一个数+1,另一个数-1,这样的操作可以进行无数次,问最终相同的数会有多少。

 2.题解

扫描二维码关注公众号,回复: 11079814 查看本文章

  若n能被这n个数的和整除,则会达到平衡状态,最后n个数都相等,否则会有n-1个数相等。

 3.代码

 1 #include<bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int maxn=1e5+5;
 5 int n,sum;
 6 int a[maxn];
 7 int main(){
 8     ios::sync_with_stdio(false);
 9     cin>>n; 
10     for(int i=1;i<=n;i++){
11         cin>>a[i];
12         sum+=a[i];
13     }
14     
15     if(sum%n)    
16         cout<<n-1<<endl;
17     else
18         cout<<n<<endl;
19         
20     return 0;
21 }

C - Beauty Pageant

 1.题意

  有n个人,k天庆典,每天都要派出n人中任意数量的人组成一个队伍,队伍的美值为队伍所有人美值的和,要求每天的美值不能相同,输出每天的安排。

 2.题解

  分别让1,2···n个人组成队伍,先取出前i-1大的数,再从前往后分别输出一个数。

 3.代码

 1 #include<bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int maxn=1e5+5;
 5 int n,k;
 6 int a[maxn];
 7 int main(){
 8     ios::sync_with_stdio(false);
 9     cin>>n>>k; 
10     for(int i=1;i<=n;i++)
11         cin>>a[i];
12     sort(a+1,a+n+1);
13     
14     for(int i=1;i<=n&&k;i++){
15         for(int j=1;j<=n-i+1&&k;j++){
16             cout<<i<<' ';
17             for(int q=n;q>n-i+1;q--)
18                 cout<<a[q]<<' ';
19             cout<<a[j]<<endl;
20             k--;
21         }
22     }
23 
24     return 0;
25 }

E - Dividing Orange

 1.题意

  给定从1到n*k共n*k个数,有k个人,每人得到n个数,每个人都有一个他必须得到的数,输出每人得到的数的情况。

 2.题解

  用map标记数字是否被用,先把每人要的那个数给他,再给他n-1个数即可。

 3.代码

 1 #include<bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int maxn=1e5+5;
 5 int n,k;
 6 int a[maxn];
 7 map<int,int> mp;
 8 int main(){
 9     ios::sync_with_stdio(false);
10     cin>>n>>k; 
11     for(int i=1;i<=k;i++){
12         cin>>a[i];
13         mp[a[i]]=1;
14     }
15         
16     
17     for(int i=1;i<=k;i++){
18         cout<<a[i];
19         for(int j=1,x=1;j<=k*n&&x<n;j++){
20             if(j==a[i])
21                 continue;
22             if(!mp[j]){
23                 cout<<' '<<j;
24                 mp[j]=1;
25                 x++;
26             }
27         }
28         cout<<endl;
29     }
30         
31     return 0;
32 }

F - Undoubtedly Lucky Numbers

 1.题意

  给定数字n,问从1到n共有多少个幸运数。(幸运数:最多由两个数字组成,例:1,12,122,4455)。

 2.题解

  利用深搜dfs(x,y,z),使z,y不相等,每次搜索x后面分别加上一位y和z,最后减去0。

 3.代码

 1 #include<bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 ll n,i,j;
 5 set<ll> st;
 6 void dfs(ll x,ll y,ll z){
 7     if(x>n) 
 8         return;
 9     st.insert(x);
10     if(10*x+y!=x)
11           dfs(10*x+y,y,z);
12     if(10*x+z!=x)
13            dfs(10*x+z,y,z);
14 
15 } 
16 int main(){
17     cin>>n;
18     for(i=0;i<=9;i++)
19         for(j=0;j<i;j++)
20             dfs(0,i,j);
21     
22     cout<<st.size()-1;
23     
24     return 0;
25 } 

猜你喜欢

转载自www.cnblogs.com/lvguapi/p/12740384.html