Codeforces Beta Round #22 (Div. 2 Only)
Codeforces Beta Round #22 (Div. 2 Only)
http://codeforces.com/contest/22
A
水题
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define maxn 500005 7 typedef long long ll; 8 /*#ifndef ONLINE_JUDGE 9 freopen("1.txt","r",stdin); 10 #endif */ 11 12 int a; 13 vector<int>ve; 14 15 int main(){ 16 #ifndef ONLINE_JUDGE 17 // freopen("1.txt","r",stdin); 18 #endif 19 std::ios::sync_with_stdio(false); 20 int n; 21 cin>>n; 22 for(int i=0;i<n;i++){ 23 cin>>a; 24 ve.push_back(a); 25 } 26 sort(ve.begin(),ve.end()); 27 ve.erase(unique(ve.begin(),ve.end()),ve.end()); 28 if(ve.size()==1) cout<<"NO"<<endl; 29 else cout<<ve[1]<<endl; 30 31 }
B
DP,有点像二维差分
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define maxn 500005 7 typedef long long ll; 8 /*#ifndef ONLINE_JUDGE 9 freopen("1.txt","r",stdin); 10 #endif */ 11 12 int n,m; 13 char str[35][35]; 14 int dp[35][35]; 15 16 int main(){ 17 #ifndef ONLINE_JUDGE 18 freopen("1.txt","r",stdin); 19 #endif 20 std::ios::sync_with_stdio(false); 21 scanf("%d %d",&n,&m); 22 int ans=0; 23 for(int i=1;i<=n;i++) scanf("%s%*c",str[i]+1); 24 for(int i=1;i<=n;i++){ 25 for(int j=1;j<=m;j++){ 26 if(str[i][j]=='1'){ 27 dp[i][j]++; 28 } 29 dp[i][j]+=dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]; 30 } 31 } 32 for(int i=1;i<=n;i++){ 33 for(int j=1;j<=m;j++){ 34 if(str[i][j]=='0'){ 35 for(int k=i-1;k>=0;k--){ 36 for(int l=j-1;l>=0;l--){ 37 int tmp=dp[i][j]-dp[k][j]-dp[i][l]+dp[k][l]; 38 if(!tmp){ 39 ans=max(ans,2*(i-k+j-l)); 40 } 41 if(str[i][l]=='1') break; 42 } 43 if(str[k][j]=='1') break; 44 } 45 } 46 } 47 } 48 49 50 cout<<ans<<endl; 51 }
C
构造题
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define maxn 500005 7 typedef long long ll; 8 /*#ifndef ONLINE_JUDGE 9 freopen("1.txt","r",stdin); 10 #endif */ 11 12 int n,m,v; 13 14 int main(){ 15 #ifndef ONLINE_JUDGE 16 freopen("1.txt","r",stdin); 17 #endif 18 std::ios::sync_with_stdio(false); 19 cin>>n>>m>>v; 20 if(m<n-1||m>((n-2)*(n-3))/2+n-1) cout<<-1<<endl; 21 else if(n<3) cout<<"1 2"<<endl; 22 else{ 23 int u=v-1; 24 if(v==1) u=2; 25 for(int i = 1; i <= n; i++){ 26 if(i != v) 27 cout<<i<<" "<<v<<endl; 28 } 29 m -= (n - 1); 30 for(int i = 1; i <= n && m; i++){ 31 if(i == v || i == u) continue; 32 for(int j = i + 1; j <= n && m; j++){ 33 if(j == v || j == u) continue; 34 cout<<i<<" "<<j<<endl; 35 m--; 36 } 37 } 38 } 39 40 }
D
贪心
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define maxn 500005 7 typedef long long ll; 8 /*#ifndef ONLINE_JUDGE 9 freopen("1.txt","r",stdin); 10 #endif */ 11 12 int n; 13 vector<pair<int,int> >ve; 14 vector<int>V; 15 16 int main(){ 17 #ifndef ONLINE_JUDGE 18 // freopen("1.txt","r",stdin); 19 #endif 20 std::ios::sync_with_stdio(false); 21 cin>>n; 22 int a,b; 23 for(int i=1;i<=n;i++){ 24 cin>>a>>b; 25 if(a>b) swap(a,b); 26 ve.push_back(make_pair(a,b)); 27 } 28 sort(ve.begin(),ve.end()); 29 int ans=1; 30 int r=ve[0].second; 31 for(int i=1;i<ve.size();i++){ 32 if(ve[i].first>r){ 33 ans++; 34 V.push_back(r); 35 r=ve[i].second; 36 } 37 else{ 38 r=min(r,ve[i].second); 39 } 40 } 41 V.push_back(r); 42 cout<<ans<<endl; 43 for(int i=0;i<V.size();i++){ 44 cout<<V[i]<<' '; 45 } 46 }
E
构造强连通分量,先找到出度为0的点,跑dfs找出链或环上的头尾节点,然后把这些节点相连即可
注意,可能存在自环,所以要判断一下
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define maxn 500005 7 typedef long long ll; 8 /*#ifndef ONLINE_JUDGE 9 freopen("1.txt","r",stdin); 10 #endif */ 11 12 vector<int>ve[100005],head,last; 13 int d[100005]; 14 int vis[100005]; 15 16 int dfs(int pos){ 17 vis[pos]=1; 18 if(!vis[ve[pos][0]]){ 19 return vis[pos]=dfs(ve[pos][0]); 20 } 21 return vis[pos]=pos; 22 } 23 24 int main(){ 25 #ifndef ONLINE_JUDGE 26 // freopen("1.txt","r",stdin); 27 #endif 28 std::ios::sync_with_stdio(false); 29 int n; 30 cin>>n; 31 int a; 32 for(int i=1;i<=n;i++){ 33 cin>>a; 34 ve[i].push_back(a); 35 d[a]++; 36 } 37 int k=0; 38 for(int i=1;i<=n;i++){ 39 if(!d[i]){ 40 k++; 41 head.push_back(i); 42 last.push_back(dfs(i)); 43 } 44 } 45 int kk=k; 46 for(int i=1;i<=n;i++){ 47 if(!vis[i]){ 48 k++; 49 head.push_back(i); 50 last.push_back(dfs(i)); 51 } 52 } 53 if(k==1&&!kk) k=0; 54 cout<<k<<endl; 55 for(int i=0;i<k;i++){ 56 cout<<last[i]<<" "<<head[(i+1)%k]<<endl; 57 } 58 }
posted on 2019-02-08 17:02 Fighting_sh 阅读(141) 评论(0) 编辑 收藏 举报