码迷,mamicode.com
首页 > 其他好文 > 详细

12/21-12/22题集

时间:2017-12-22 22:57:06      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:字典树   pac   alt   none   name   force   ble   net   inf   

日常训练:
1.CodeForces - 891B  构造,思维题
用b复制a数组,每次在bi对应位置构一个大于ai的数,然后依次下去,最后有一个bi最小值与ai最大值对应。
每次取出的序列,如果不包含最大值,那么bi之和一定大于ai之和;如果包含最大值,bi之和一定小于ai之和。(这个模拟一下看一下就懂了)
超有意思的一道题。mark
技术分享图片
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 #define PI acos(-1.0)
 5 #define INF 0x3f3f3f3f
 6 #define FAST_IO ios::sync_with_stdio(false)
 7 #define CLR(arr,val) memset(arr,val,sizeof(arr))
 8 
 9 typedef long long LL;
10 
11 const int N=23;
12 int a[N],b[N];
13 map <int,int> m;
14 
15 int main(){
16     int n;
17     cin>>n;
18     for(int i=0;i<n;i++) cin>>a[i],b[i]=a[i];
19     sort(b,b+n);
20     for(int i=0;i<n;i++){
21         m[b[i]]=b[(i+1)%n];
22     }
23     for(int i=0;i<n;i++){
24         cout<<m[a[i]]<<" ";
25     }
26     return 0;
27 }
View Code

2.CodeForces - 888D  组合数学

因为k<=4,那我们就枚举一下各种情况,然后输出对应的解。

1.当k=1时,n-1个固定,那么最后一个也固定了,也就只有一种排列,即1。

2.当k=2时,最多2个可以变的,我们先从n个中取出2个,然后错排,只有一种情况,即C(n,2)+1。

3.当k=3时,最多3个可以变的,我们先从n个中取出3个,错排,两种情况,即2*C(n,3)+C(n,2)+1。

4.当k=4时,最多4个可以变的,同理,错排,即9*C(n,4)+2*C(n,3)+C(n,2)+1。

错排公式:D(n) = n! [(-1)^2/2! + … + (-1)^(n-1)/(n-1)! + (-1)^n/n!]

技术分享图片
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 #define PI acos(-1.0)
 5 #define INF 0x3f3f3f3f
 6 #define FAST_IO ios::sync_with_stdio(false)
 7 #define CLR(arr,val) memset(arr,val,sizeof(arr))
 8 
 9 typedef long long LL;
10 
11 LL C(LL n,LL k){
12     LL ans=1;
13     for(LL i=n;i>=n-k+1;i--) ans*=i;
14     for(LL i=1;i<=k;i++) ans/=i;
15     return ans;
16 }
17 
18 int main(){
19     LL n,k;
20     FAST_IO;
21     cin>>n>>k;
22     if(k==1) cout<<1<<endl;
23     else if(k==2) cout<<C(n,2)+1<<endl;
24     else if(k==3) cout<<2*C(n,3)+C(n,2)+1<<endl;
25     else if(k==4) cout<<9*C(n,4)+2*C(n,3)+C(n,2)+1<<endl;
26     return 0;
27 }
View Code

3.CodeForces - 858D  暴力+STL

把每种情况都放进去,在同一个字符串里出现过超过两次的字符串用set都变成一次,然后存进map里,然后再枚举,对应size==1,并且长度越短的为答案。题目给了4s,我3s多刚好卡过...听说是道字典树的题目(没学过,赶紧找时间去看波...

技术分享图片
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 #define PI acos(-1.0)
 5 #define INF 0x3f3f3f3f
 6 #define FAST_IO ios::sync_with_stdio(false)
 7 #define CLR(arr,val) memset(arr,val,sizeof(arr))
 8 
 9 const int N=70000+10;
10 typedef long long LL;
11 string s[N],t;
12 set <string> ss;
13 map <string,int> m;
14 set <string>::iterator it;
15 
16 int main(){
17     FAST_IO;
18     char tmp;
19     int n;
20     cin>>n;
21     for(int k=1;k<=n;k++){
22         cin>>s[k];
23         ss.clear();
24         for(int i=0;i<9;i++){
25             t.clear();
26             for(int j=i;j<9;j++){
27                 tmp=s[k][j];
28                 t+=tmp;
29                 ss.insert(t);
30             }
31         }
32         for(it=ss.begin();it!=ss.end();it++)
33         m[*it]++;
34     }
35 
36     for(int k=1;k<=n;k++){
37         string ans=s[k];
38         for(int i=0;i<9;i++){
39             t.clear();
40             for(int j=i;j<9;j++){
41                 tmp=s[k][j];
42                 t+=tmp;
43                 if(m[t]==1&&ans.size()>t.size()){
44                     ans=t;
45                 }
46             }
47         }
48         cout<<ans<<endl;
49     }
50 
51     return 0;
52 }
View Code

4.CodeForces - 858C  贪心

标记一下辅音字母出现的个数,每次cnt==3时,更新一下,cnt置为1。当碰到元音字母的时候,cnt置为0。值得注意的是当cnt==2时,并且前面和前前面的和当前位置的字母相同,并且cnt==2时,那么此时cnt不增加,emmm...(这里我也是根据错误数据调出来的...)

技术分享图片
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 #define PI acos(-1.0)
 5 #define INF 0x3f3f3f3f
 6 #define FAST_IO ios::sync_with_stdio(false)
 7 #define CLR(arr,val) memset(arr,val,sizeof(arr))
 8 
 9 typedef long long LL;
10 
11 int main(){
12     string s;
13     cin>>s;
14     int len=s.size();
15     int cnt=0;
16     string ans="";
17     for(int i=0;i<len;i++){
18         if(s[i]==a||s[i]==e||s[i]==i||s[i]==o||s[i]==u) cnt=0;
19         else{
20             if(i>=2){
21                 if(s[i]==s[i-1]&&s[i]==s[i-2]&&cnt==2) cnt=cnt;
22                 else cnt++;
23             }
24             else cnt++;
25         }
26         if(cnt==3){
27             ans=ans+" "+s[i];
28             cnt=1;
29         }
30         else ans=ans+s[i];
31     }
32     cout<<ans<<endl;
33     return 0;
34 }
View Code

5.CodeForces - 858B  模拟

一开始没想到枚举层数,看了一波网上的题解。发现只要直接枚举层数,然后符合条件的层数,然后我们用个ans标记,如果和之前标记的层数有冲突,那么就无法确定,即输出-1。否则最后输出答案。

技术分享图片
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 #define PI acos(-1.0)
 5 #define INF 0x3f3f3f3f
 6 #define FAST_IO ios::sync_with_stdio(false)
 7 #define CLR(arr,val) memset(arr,val,sizeof(arr))
 8 
 9 typedef long long LL;
10 struct TnT{
11     int k,f;
12 }T[111];
13 
14 bool cmp(TnT a,TnT b){
15     if(a.f==b.f) return a.k<b.k;
16     return a.f<b.f;
17 }
18 map <int,int> t;
19 
20 int main(){
21     FAST_IO;
22     int n,m,ans=-1;
23     cin>>n>>m;
24     for(int i=0;i<m;i++) cin>>T[i].k>>T[i].f;
25     sort(T,T+m,cmp);
26     for(int k=1;k<=100;k++){
27         int cnt=1;
28         for(int i=1;i<=110;i+=k){
29            for(int j=i;j<=i+k-1;j++) t[j]=cnt;
30            cnt++;
31         }
32         int flag=1;
33         for(int i=0;i<m;i++){
34             if(t[T[i].k]!=T[i].f) flag=0;
35         }
36         if(flag){
37             if(ans==-1) ans=t[n];
38             else if(ans!=t[n]) {cout<<-1<<endl;return 0;}
39         }
40     }
41     cout<<ans<<endl;
42     return 0;
43 }
View Code

 

 

 

 

12/21-12/22题集

标签:字典树   pac   alt   none   name   force   ble   net   inf   

原文地址:http://www.cnblogs.com/Leonard-/p/8087786.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!