2019HPU-ICPC-Training-1

byl太強了,學弟們太強了~全程被吊打,嚶嚶嚶~html

A題  Connecting Vertices

 

http://codeforces.com/problemset/problem/888/Fios

不會c++

B題 Local Extrema

 

http://codeforces.com/problemset/problem/888/Aide

給一列數字,判斷一個數它的左右是否同時比它大,或者同時比它小,若知足的話那麼count++,獲得最後的count值,那麼很明顯,直接暴力的遍歷一遍就能夠了。spa

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 #define ull unsigned long long
 4 #define ms(a,b) memset(a,b,sizeof(a))
 5 const int inf=0x3f3f3f3f;  6 const ll INF=0x3f3f3f3f3f3f3f3f;  7 const int maxn=1e6+10;  8 const int mod=1e9+7;  9 const int maxm=1e3+10; 10 using namespace std; 11 int a[maxn]; 12 int main(int argc, char const *argv[]) 13 { 14  #ifndef ONLINE_JUDGE 15         freopen("in.txt", "r", stdin); 16         freopen("out.txt", "w", stdout); 17         srand((unsigned int)time(NULL)); 18     #endif
19     ios::sync_with_stdio(false); 20     cin.tie(0); 21     int n; 22     cin>>n; 23     for(int i=1;i<=n;i++) 24         cin>>a[i]; 25     int ans=0; 26     for(int i=2;i<n;i++) 27  { 28         if(a[i]<a[i-1]&&a[i]<a[i+1]) 29             ans++; 30         if(a[i]>a[i-1]&&a[i]>a[i+1]) 31             ans++; 32  } 33     cout<<ans<<endl; 34  #ifndef ONLINE_JUDGE 35         cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl; 36     #endif
37     return 0; 38 }
View Code

C題 Xor-MST

 

http://codeforces.com/problemset/problem/888/Gcode

不會htm

D題 Buggy Robot

 

http://codeforces.com/problemset/problem/888/Bblog

機器人有四種指令,找出在給出的一大串指令中,最多有多少指令是正確的排序

找到LR和UD有多少對,乘以2就好了ci

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 #define ull unsigned long long
 4 #define ms(a,b) memset(a,b,sizeof(a))
 5 const int inf=0x3f3f3f3f;  6 const ll INF=0x3f3f3f3f3f3f3f3f;  7 const int maxn=1e6+10;  8 const int mod=1e9+7;  9 const int maxm=1e3+10; 10 using namespace std; 11 int main(int argc, char const *argv[]) 12 { 13  #ifndef ONLINE_JUDGE 14         freopen("in.txt", "r", stdin); 15         freopen("out.txt", "w", stdout); 16         srand((unsigned int)time(NULL)); 17     #endif
18     ios::sync_with_stdio(false); 19     cin.tie(0); 20     int n; 21     cin>>n; 22     string s; 23     cin>>s; 24     map<char,int>mp; 25     for(int i=0;i<n;i++) 26         mp[s[i]]++; 27     int ans=0; 28     ans+=min(mp['L'],mp['R']); 29     ans+=min(mp['U'],mp['D']); 30     cout<<ans*2<<endl; 31  #ifndef ONLINE_JUDGE 32         cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl; 33     #endif
34     return 0; 35 }
View Code

E題 K-Dominant Character

 

http://codeforces.com/problemset/problem/888/C

給出一個字符串,找出一個最小的長度k,使得每一個長度爲k的子串中都包含一個相同的字符記錄下來每一個字符的位置,找兩個相同字符的最大距離,對這個最大距離取最小值

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 #define ull unsigned long long
 4 #define ms(a,b) memset(a,b,sizeof(a))
 5 const int inf=0x3f3f3f3f;  6 const ll INF=0x3f3f3f3f3f3f3f3f;  7 const int maxn=1e6+10;  8 const int mod=1e9+7;  9 const int maxm=1e3+10; 10 using namespace std; 11 int main(int argc, char const *argv[]) 12 { 13  #ifndef ONLINE_JUDGE 14         freopen("in.txt", "r", stdin); 15         freopen("out.txt", "w", stdout); 16         srand((unsigned int)time(NULL)); 17     #endif
18     ios::sync_with_stdio(false); 19     cin.tie(0); 20     string s; 21     cin>>s; 22     int l=s.length(); 23     vector<int>ve[30]; 24     for(int i=0;i<26;i++) 25         ve[i].push_back(-1); 26     for(int i=0;i<l;i++) 27         ve[s[i]-'a'].push_back(i); 28     for(int i=0;i<26;i++) 29  ve[i].push_back(l); 30     int ans=inf; 31     for(int i=0;i<26;i++) 32  { 33         int res=0; 34         int sz=ve[i].size(); 35         for(int j=1;j<sz-1;j++) 36             res=max(res,max(ve[i][j]-ve[i][j-1],ve[i][j+1]-ve[i][j])); 37         if(res==0) 38             continue; 39         ans=min(ans,res); 40  } 41     cout<<ans<<endl; 42  #ifndef ONLINE_JUDGE 43         cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl; 44     #endif
45     return 0; 46 }
View Code

F題 Maximum Subsequence

 

http://codeforces.com/problemset/problem/888/E

給出n個數,從這n個數中選出幾個數(能夠不選),使得這些數的和對m取餘後的值最大

題解連接:http://www.javashuo.com/article/p-aswqpzlj-ha.html

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 #define ull unsigned long long
 4 #define ms(a,b) memset(a,b,sizeof(a))
 5 const int inf=0x3f3f3f3f;  6 const ll INF=0x3f3f3f3f3f3f3f3f;  7 const int maxn=1e6+10;  8 const int mod=1e9+7;  9 const int maxm=1e3+10; 10 using namespace std; 11 int a[maxn]; 12 int Left[maxn]; 13 int Right[maxn]; 14 int cntl,cntr; 15 int n,m; 16 int main(int argc, char const *argv[]) 17 { 18  #ifndef ONLINE_JUDGE 19         freopen("in.txt", "r", stdin); 20         freopen("out.txt", "w", stdout); 21         srand((unsigned int)time(NULL)); 22     #endif
23     ios::sync_with_stdio(false); 24     cin.tie(0); 25     cin>>n>>m; 26     for(int i=0;i<n;i++) 27         cin>>a[i],a[i]%=m; 28     int res=0; 29     int l,r; 30     l=r=n/2; 31     for(int i=0;i<(1<<r);i++) 32  { 33         res=0; 34         for(int j=0;j<r;j++) 35             if(i>>j&1) 36                 res+=a[j],res%=m; 37         Left[cntl++]=res; 38  } 39     res=0; 40     r=n; 41     int num=r-l+1; 42     for(int i=0;i<(1<<num);i++) 43  { 44         res=0; 45         for(int j=0;j<num;j++) 46             if(i>>j&1) 47                 res+=a[l+j],res%=m; 48         Right[cntr++]=res; 49  } 50     Left[cntl++]=0; 51     Right[cntr++]=0; 52     sort(Left,Left+cntl); 53     sort(Right,Right+cntr); 54     cntl=unique(Left,Left+cntl)-Left; 55     cntr=unique(Right,Right+cntr)-Right; 56     int ans=0; 57     for(int i=0;i<cntl;i++) 58  { 59         int res=m-Left[i]-1; 60         int pos=upper_bound(Right,Right+cntr,res)-Right; 61         int num=Right[pos-1]; 62         ans=max(ans%m,(num+Left[i])%m); 63  } 64     cout<<ans<<endl; 65  #ifndef ONLINE_JUDGE 66         cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl; 67     #endif
68     return 0; 69 }
View Code

G題 Almost Identity Permutations

 

http://codeforces.com/problemset/problem/888/D

給出n的全排列,求有多少種排列,知足至少n−k個位置上的數和下標相同(下標從1開始)

錯排公式+組合數

題解連接:http://www.javashuo.com/article/p-slggzloy-hg.html

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 #define ull unsigned long long
 4 #define ms(a,b) memset(a,b,sizeof(a))
 5 const int inf=0x3f3f3f3f;  6 const ll INF=0x3f3f3f3f3f3f3f3f;  7 const int maxn=1e6+10;  8 const int mod=1e9+7;  9 const int maxm=1e3+10; 10 using namespace std; 11 ll C(int n,int m) 12 { 13     ll fenmu=1LL; 14     ll fenzi=1LL; 15     for(int i=1;i<=m;i++) 16  { 17         fenmu=1LL*fenmu*(n-i+1); 18         fenzi=1LL*fenzi*i; 19  } 20     return fenmu/fenzi; 21 } 22 int main(int argc, char const *argv[]) 23 { 24  #ifndef ONLINE_JUDGE 25         freopen("in.txt", "r", stdin); 26         freopen("out.txt", "w", stdout); 27         srand((unsigned int)time(NULL)); 28     #endif
29     ios::sync_with_stdio(false); 30     cin.tie(0); 31     int n,k; 32     cin>>n>>k; 33     ll ans=0; 34     if(k>=1) 35         ans+=1; 36     if(k>=2) 37         ans+=(n*(n-1)/2); 38     if(k>=3) 39         ans+=2*C(n,3); 40     if(k>=4) 41         ans+=9*C(n,4); 42     cout<<ans<<endl; 43  #ifndef ONLINE_JUDGE 44         cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl; 45     #endif
46     return 0; 47 }
View Code

H題 Alyona and Spreadsheet

 

http://codeforces.com/problemset/problem/777/C

給出一個n×m的矩陣,判斷第l行~第r行中是否有一列是非遞減的

預處理每一行能往上延伸到的位置,注意矩陣的存法

題解連接:http://www.javashuo.com/article/p-ahlstlpp-hq.html

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 #define ull unsigned long long
 4 #define ms(a,b) memset(a,b,sizeof(a))
 5 const int inf=0x3f3f3f3f;  6 const ll INF=0x3f3f3f3f3f3f3f3f;  7 const int maxn=1e6+10;  8 const int mod=1e9+7;  9 const int maxm=1e3+10; 10 using namespace std; 11 vector<int>ve[maxn]; 12 // 當前行能往上延伸的最高位置
13 int can[maxn]; 14 // 當前列能往上的最高位置
15 int line[maxn]; 16 int main(int argc, char const *argv[]) 17 { 18  #ifndef ONLINE_JUDGE 19         freopen("in.txt", "r", stdin); 20         freopen("out.txt", "w", stdout); 21         srand((unsigned int)time(NULL)); 22     #endif
23     ios::sync_with_stdio(false); 24     cin.tie(0); 25     int n,m; 26     cin>>n>>m; 27     int x; 28     for(int i=0;i<m;i++) 29         ve[0].push_back(0); 30     for(int i=1;i<=n;i++) 31         for(int j=0;j<m;j++) 32             cin>>x,ve[i].push_back(x); 33     for(int i=1;i<=n;i++) 34  { 35         can[i]=i; 36         for(int j=0;j<m;j++) 37  { 38             int now_num=ve[i][j]; 39             int up_num=ve[i-1][j]; 40             if(now_num<up_num) 41                 line[j]=i; 42             can[i]=min(can[i],line[j]); 43  } 44  } 45     int t; 46     cin>>t; 47     while(t--) 48  { 49         int l,r; 50         cin>>l>>r; 51         if(can[r]>l) 52             cout<<"No\n"; 53         else
54             cout<<"Yes\n"; 55  } 56  #ifndef ONLINE_JUDGE 57         cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl; 58     #endif
59     return 0; 60 }
View Code

I題 Shell Game

 

http://codeforces.com/problemset/problem/777/A

如今一共有三個小盒子,其中有一個盒子中有小球.一共進行了n次操做,操做規律有:

①奇數次操做,交換第一個和中間的盒子。

②偶數次操做,交換第三個和中間的盒子。

如今已知操做了n次以後小球在x號盒子中(0,1,2),問初始的時候小球在哪裏

循環節,每六個數字一個循環

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 #define ull unsigned long long
 4 #define ms(a,b) memset(a,b,sizeof(a))
 5 const int inf=0x3f3f3f3f;  6 const ll INF=0x3f3f3f3f3f3f3f3f;  7 const int maxn=1e6+10;  8 const int mod=1e9+7;  9 const int maxm=1e3+10; 10 using namespace std; 11 int main(int argc, char const *argv[]) 12 { 13  #ifndef ONLINE_JUDGE 14         freopen("in.txt", "r", stdin); 15         freopen("out.txt", "w", stdout); 16         srand((unsigned int)time(NULL)); 17     #endif
18     ios::sync_with_stdio(false); 19     cin.tie(0); 20     int a[6][3]={{0,1,2},{1,0,2},{1,2,0},{2,1,0},{2,0,1},{0,2,1}}; 21     int n,x; 22     cin>>n>>x; 23     n%=6; 24     cout<<a[n][x]<<endl; 25  #ifndef ONLINE_JUDGE 26         cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl; 27     #endif
28     return 0; 29 }
View Code

J題 Hanoi Factory

 

http://codeforces.com/problemset/problem/777/E

有n個空心圓柱體,第i個圓柱體的內徑、外徑、高分別爲:ai,bi,hi。將這些圓柱體堆起來,要求:從上到下,外徑非遞減,而且上面的外徑小於下面的內徑。問最高能堆多高

貪心,用棧維護

題解連接:http://www.javashuo.com/article/p-wieycrlo-gb.html

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 #define ull unsigned long long
 4 #define ms(a,b) memset(a,b,sizeof(a))
 5 const int inf=0x3f3f3f3f;  6 const ll INF=0x3f3f3f3f3f3f3f3f;  7 const int maxn=1e6+10;  8 const int mod=1e9+7;  9 const int maxm=1e3+10; 10 using namespace std; 11 struct wzy 12 { 13     int a,b,h; 14 }p[maxn]; 15 bool cmp(wzy u,wzy v) 16 { 17     if(u.b==v.b) 18  { 19         if(u.a==v.a) 20             return u.h>v.h; 21         return u.a>v.a; 22  } 23     return u.b>v.b; 24 } 25 int main(int argc, char const *argv[]) 26 { 27  #ifndef ONLINE_JUDGE 28         freopen("/home/wzy/in.txt", "r", stdin); 29         freopen("/home/wzy/out.txt", "w", stdout); 30         srand((unsigned int)time(NULL)); 31     #endif
32     ios::sync_with_stdio(false); 33     cin.tie(0); 34     int n; 35     cin>>n; 36     for(int i=1;i<=n;i++) 37         cin>>p[i].a>>p[i].b>>p[i].h; 38     sort(p+1,p+1+n,cmp); 39     ll ans=1LL*p[1].h; 40     ll sum=1LL*p[1].h; 41     stack<wzy>st; 42     st.push(p[1]); 43     for(int i=2;i<=n;i++) 44  { 45         while(!st.empty()&&(st.top().a>=p[i].b||st.top().b<p[i].b)) 46  { 47             sum-=1LL*st.top().h; 48  st.pop(); 49  } 50         sum+=1LL*p[i].h; 51  st.push(p[i]); 52         ans=max(ans,sum); 53  } 54     cout<<ans<<endl; 55  #ifndef ONLINE_JUDGE 56         cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl; 57     #endif
58     return 0; 59 }
View Code

K題 Cloud of Hashtags

 

 http://codeforces.com/contest/777/problem/D

n個字符串,要求不改變位置,刪除最少的字符串的後綴,使這些字符串按照字典序非遞減的順序排列

暴力便可

題解連接:http://www.javashuo.com/article/p-skykstax-ht.html

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 #define ull unsigned long long
 4 #define ms(a,b) memset(a,b,sizeof(a))
 5 const int inf=0x3f3f3f3f;  6 const ll INF=0x3f3f3f3f3f3f3f3f;  7 const int maxn=1e6+10;  8 const int mod=1e9+7;  9 const int maxm=1e3+10; 10 using namespace std; 11 vector<string>ve; 12 vector<string>ans; 13 int get_place(string s1,string s2) 14 { 15     int l1=s1.length(); 16     int l2=s2.length(); 17     int i; 18     for(i=0;i<min(l2,l1);i++) 19  { 20         if(s1[i]<s2[i]) 21             return l1; 22         if(s1[i]==s2[i]) 23             continue; 24         if(s1[i]>s2[i]) 25             return i; 26  } 27     if(i==l2) 28  { 29         if(l1>l2) 30  { 31             if(s1[i-1]==s2[i-1]) 32                 return l2; 33             else
34                 return l1; 35  } 36  } 37     return l1; 38 } 39 int main(int argc, char const *argv[]) 40 { 41  #ifndef ONLINE_JUDGE 42         freopen("in.txt", "r", stdin); 43         freopen("out.txt", "w", stdout); 44         srand((unsigned int)time(NULL)); 45     #endif
46     ios::sync_with_stdio(false); 47     cin.tie(0); 48     int n; 49     cin>>n; 50     string s; 51     for(int i=0;i<n;i++) 52  { 53         cin>>s; 54  ve.push_back(s); 55  } 56     string s1,s2; 57     ans.push_back(ve[n-1]); 58     for(int i=n-2;i>=0;i--) 59  { 60         s1=ve[i]; 61         s2=ans[n-(i+2)]; 62         int pos=get_place(s1,s2); 63         string ss; 64         ss=s1.substr(0,pos); 65  ans.push_back(ss); 66  } 67     for(int i=n-1;i>0;i--) 68         cout<<ans[i]<<endl; 69     cout<<ve[n-1]<<endl; 70  #ifndef ONLINE_JUDGE 71         cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl; 72     #endif
73     return 0; 74 }
View Code

L題 Game of Credit Cards

 

http://codeforces.com/contest/777/problem/B 

Sherlock和Moriarty有n張卡片,每一個卡片上有一個數字,如今有Sherlock和Moriarty 兩我的在比較這些卡片上的數字大小,小的數字須要接受懲罰,Sherlock的卡片順序是固定的,Moriarty的卡片順序能夠隨意變更,求Moriarty的最小接受懲罰次數是多少,Sherlock最大懲罰對方的次數是多少

排序比較便可

題解連接:http://www.javashuo.com/article/p-fuuowqjl-hx.html

 

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 #define ull unsigned long long
 4 #define ms(a,b) memset(a,b,sizeof(a))
 5 const int inf=0x3f3f3f3f;  6 const ll INF=0x3f3f3f3f3f3f3f3f;  7 const int maxn=1e6+10;  8 const int mod=1e9+7;  9 const int maxm=1e3+10; 10 using namespace std; 11 int s[maxn],m[maxn]; 12 int nums[100],numm[100]; 13 int main(int argc, char const *argv[]) 14 { 15  #ifndef ONLINE_JUDGE 16         freopen("in.txt", "r", stdin); 17         freopen("out.txt", "w", stdout); 18         srand((unsigned int)time(NULL)); 19     #endif
20     ios::sync_with_stdio(false); 21     cin.tie(0); 22     int n; 23     string s1,s2; 24     cin>>n; 25     cin>>s1>>s2; 26     for(int i=0;i<n;i++) 27         s[i]=s1[i]-'0',m[i]=s2[i]-'0'; 28     sort(s,s+n); 29     sort(m,m+n); 30     int pos1=0; 31     int pos2=0; 32     int res1=0; 33     int res2=0; 34     for(int i=0;i<n;i++) 35  { 36         if(pos1>=n&&pos2>=n) 37             break; 38         while(pos1<n&&m[pos1]<s[i]) 39             pos1++; 40         while(pos2<n&&m[pos2]<=s[i]) 41             pos2++; 42         if(pos1<n) 43             res1++,pos1++; 44         if(pos2<n) 45             res2++,pos2++; 46  } 47     cout<<n-res1<<endl; 48     cout<<res2<<endl; 49  #ifndef ONLINE_JUDGE 50         cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl; 51     #endif
52     return 0; 53 }
View Code
本站公眾號
   歡迎關注本站公眾號,獲取更多信息