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
本站公众号
   欢迎关注本站公众号,获取更多信息