Educational Codeforces Round 32
http://codeforces.com/contest/888
A?Local Extrema【水】
【題意】:計算極值點個數
【分析】:除了第一個最后一個外,遇到極值點ans++,包括極大和極小
【代碼】:
#include<bits/stdc++.h>using namespace std;int main() {int n,a[1000+10];int maxn,minn;maxn=minn=0;cin>>n;for(int i=1;i<=n;i++){cin>>a[i];}for(int i=2;i<=n-1;i++){if(a[i]>a[i+1]&&a[i]>a[i-1]) maxn++;if(a[i]<a[i+1]&&a[i]<a[i-1]) minn++;}cout<<minn+maxn<<endl;return 0; } 暴力?
B?Buggy Robot【模擬】
【題意】:機器人從(0,0)出發,有UDLR四種走法,分別代表上下左右的方向,最后回到(0,0),給你一段包含這些走法的序列,求最多有幾個走法正確(即可以刪去最少的操作使其能夠回到原點)。
【分析】:記錄下四個方向執行了幾次,然后取相反方向的最小值乘2。我是這么想的,還特判了。最后被hack了···
【代碼】:
#include<bits/stdc++.h>using namespace std;int main() {int n,min1,min2;int l,d,u,r;l=d=u=r=min1=min2=0;char a[110];cin>>n;for(int i=0;i<n;i++){cin>>a[i];if(a[i]=='L') l++;else if(a[i]=='D') d++;else if(a[i]=='U') u++;else r++;}if(l&&(!r)){printf("0\n");return 0;}if(r&&(!l)){printf("0\n");return 0;}if(u&&(!d)){printf("0\n");return 0;}if(d&&(!u)){printf("0\n");return 0;}if(l&&r) min1=2*min(l,r);if(u&&d) min2=2*min(u,d);printf("%d\n",min1+min2);return 0; } 被hack代碼?
#include <bits/stdc++.h> using namespace std; int main(){ios_base::sync_with_stdio(false);cin.tie(NULL);int N,l=0,r=0,u=0,d=0;cin>>N;string s;cin>>s;for(int i=0;i<s.length();i++){if(s[i]=='L')l++;if(s[i]=='R')r++;if(s[i]=='U')u++;if(s[i]=='D')d++;}cout<<2*(min(l,r)+min(u,d))<<endl; } AC?
C?K-Dominant Character【貪心】
【題意】:給一小寫字母組成的字符串,找個長度最短的k。使得這些長度為k的子串必須包含了某同一個字符。
【分析】:記錄下兩個相同字符的最長距離,然后取這些距離的最小值。最小化最大值。
一個數組記錄該字母上一次出現的位置
一個數組記錄該字母相鄰最大距離
如果沒有重復出現的字符呢
設定在-1和len兩個位置上和所以字母相同
比如abbbabba串,對于字母a,有距離4,3,那么數組里存的就是4?(金牌講師010):對于aab
有a這個位置的是
1 2
字符串長度是3
間距分別是
0-1 1
1-2 1
2-4 2
0和4是字符串的邊界外
所以對于字符a就是2是最大間距
b只有3這個位置出現
0-3 3
3-4 1
所以b的最大間距是3
這題答案就是2
字符串的邊界外也算進去?
因為
___x___x____x__
兩邊還有__
x是出現的位置
【代碼】:
#include<bits/stdc++.h>using namespace std; const int inf = 0x3f3f3f3f; const long long INF = 0x3f3f3f3f3f3f3f3fLL; const double pi = acos(-1.0); const int maxn = 100000+10; const int mod = 1e9+7; char a[maxn]; int main() {//freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);int n;scanf("%s",a+1);int len=strlen(a+1),ans=len;for (int i=0;i<128;i++){int tmp=0;int pos=0;for (int j=1;j<=len;j++)if (a[j]==i)tmp=max(tmp,j-pos),pos=j;tmp=max(tmp,len+1-pos);ans=min(ans,tmp);}printf("%d\n",ans);return 0; } View Code?
D. Almost Identity Permutations【排列組合】 time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard outputA permutation?p?of size?n?is an array such that every integer from?1?to?n?occurs exactly once in this array.
Let's call a permutation an?almost identity permutation?iff there exist at least?n?-?k?indices?i?(1?≤?i?≤?n) such that?pi?=?i.
Your task is to count the number of?almost identity?permutations for given numbers?n?and?k.
InputThe first line contains two integers?n?and?k?(4?≤?n?≤?1000,?1?≤?k?≤?4).
OutputPrint the number of?almost identity?permutations for given?n?and?k.
Examples input 4 1 output 1 input 4 2 output 7 input 5 3 output 31 input 5 4 output 76【題意】:一個長為n的排列,其中pi=i的位置大于等于n-k的排列方案數, 比如n=5 k=3讓你求有多少個1 2 3 4 5的排列使得至少5-3=2個數字在“原位”
【分析】:公式是C(n,i)*D(i) (0<=i<=k), 錯排只用弄到4, 組合數也只用弄到1000,4? 【代碼】: By RoniKing, contest: Educational Codeforces Round 32, problem: (D) Almost Identity Permutations, Accepted, ##include <bits/stdc++.h> #define LL long long using namespace std; //const int mod=1e9+7; const int maxn=1e3+100; LL D[maxn], C[maxn][1005]; int p[1000+100]; void init() {D[0]=1;D[1]=0;for(int i=1;i<maxn;i++)//錯排D[i]=((i-1)*(D[i-1]+D[i-2]));int i;//for (p[0]=i=1;i<=200000;i++) p[i]=1ll*p[i-1]*i%mod;for (int i = 0; i < maxn; i++)//排列組合 {for (int j = 0; j <= min(105, i); j++){if (j == 0) C[i][j] = 1;elseC[i][j] = (C[i-1][j] + C[i-1][j-1]) ;}} } int main() {init();int t;int n,k;cin>>n>>k;LL ans=1;for(LL i=1;i<=n;i++)ans = ans * i ;LL val=1;for(int i=1;i<=k;i++)val=(val + D[i]*C[n][i]) ;// cout<<ans<<endl;cout<<val<<endl;//cout<<(ans-val)<<endl; } 我的丑代碼
?
#include<bits/stdc++.h>using namespace std;int main() {long long n,k;cin>>n>>k;long long ans=1;if(k>=2) ans+=n*(n-1)/2;if(k>=3) ans+=n*(n-1)*(n-2)/3;if(k>=4) ans+=n*(n-1)*(n-2)*(n-3)*3/8;cout<<ans;return 0; } 簡♂短?
轉載于:https://www.cnblogs.com/Roni-i/p/7813487.html
總結
以上是生活随笔為你收集整理的Educational Codeforces Round 32的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IMEX
- 下一篇: Some notes for CLFS2