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 }

原文地址:https://www.cnblogs.com/Fighting-sh/p/10356389.html

时间: 2024-12-14 10:00:59

Codeforces Beta Round #22 (Div. 2 Only)的相关文章

暴力/DP Codeforces Beta Round #22 (Div. 2 Only) B. Bargaining Table

题目传送门 1 /* 2 题意:求最大矩形(全0)的面积 3 暴力/dp:每对一个0查看它左下的最大矩形面积,更新ans 4 注意:是字符串,没用空格,好事多磨,WA了多少次才发现:( 5 详细解释:http://www.cnblogs.com/cszlg/p/3217478.html 6 */ 7 #include <cstdio> 8 #include <algorithm> 9 #include <cstring> 10 #include <cmath>

Codeforces Beta Round #91 (Div. 1 Only) E. Lucky Array

E. Lucky Array Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467are not. Petya has an arra

图论/暴力 Codeforces Beta Round #94 (Div. 2 Only) B. Students and Shoelaces

题目传送门 1 /* 2 图论/暴力:这是个连通的问题,每一次把所有度数为1的砍掉,把连接的点再砍掉,总之很神奇,不懂:) 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <algorithm> 7 #include <cmath> 8 using namespace std; 9 10 const int MAXN = 1e2 + 10; 11 const int INF = 0x3f3f3

BFS Codeforces Beta Round #94 (Div. 2 Only) C. Statues

题目传送门 1 /* 2 BFS:三维BFS,坐标再加上步数,能走一个点当这个地方在步数内不能落到.因为雕像最多8步就会全部下落, 3 只要撑过这个时间就能win,否则lose 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <queue> 8 #include <vector> 9 #include <cstring> 10 using namespace std; 11 1

水题 Codeforces Beta Round #70 (Div. 2) A. Haiku

题目传送门 1 /* 2 水题:三个字符串判断每个是否有相应的元音字母,YES/NO 3 下午网速巨慢:( 4 */ 5 #include <cstdio> 6 #include <cstring> 7 #include <string> 8 #include <iostream> 9 #include <algorithm> 10 #include <cmath> 11 using namespace std; 12 13 cons

Codeforces Beta Round #6 (Div. 2 Only)

Codeforces Beta Round #6 (Div. 2 Only) 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 1000010 7 typedef long long ll; 8 /*#ifndef

Codeforces Beta Round #9 (Div. 2 Only)

Codeforces Beta Round #9 (Div. 2 Only) http://codeforces.com/contest/9 A gcd水题 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 1000010 7

Codeforces Beta Round #12 (Div 2 Only)

Codeforces Beta Round #12 (Div 2 Only) http://codeforces.com/contest/12 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 1000010 7 t

Codeforces Beta Round #14 (Div. 2)

Codeforces Beta Round #14 (Div. 2) http://codeforces.com/contest/14 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