1. 程式人生 > >12/21-12/22題集

12/21-12/22題集

ins 每次 isp b+ 暴力 char += ios n!

日常訓練: 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題集