Codeforces Beta Round #34 (Div. 2)

Codeforces Beta Round #34 (Div. 2)

http://codeforces.com/contest/34

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 n;
13 int a[105];
14 map<int,int>mp;
15 
16 int main(){
17     #ifndef ONLINE_JUDGE
18   //      freopen("1.txt","r",stdin);
19     #endif
20     std::ios::sync_with_stdio(false);
21     int n;
22     cin>>n;
23     for(int i=1;i<=n;i++) cin>>a[i];
24     a[n+1]=a[1];
25     int pos1,pos2,Min=0x3f3f3f3f;
26     for(int i=1;i<=n;i++){
27         if(abs(a[i]-a[i+1])<Min){
28             Min=abs(a[i]-a[i+1]);
29             pos1=i;
30             pos2=i+1;
31         }
32     }
33     if(pos2==n+1) pos2=1;
34     cout<<pos1<<" "<<pos2<<endl;
35 }
View Code

 

B

 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 int a[105];
14 map<int,int>mp;
15 
16 int main(){
17     #ifndef ONLINE_JUDGE
18   //      freopen("1.txt","r",stdin);
19     #endif
20     std::ios::sync_with_stdio(false);
21     int n;
22     int m;
23     cin>>n>>m;
24     for(int i=1;i<=n;i++) cin>>a[i];
25     sort(a+1,a+n+1);
26     int ans=0;
27     for(int i=1;i<=n&&m;i++){
28         if(a[i]<0){
29             ans-=a[i];
30             m--;
31         }
32     }
33     cout<<ans<<endl;
34 }
View Code

 

C

map+vector

 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 map<int,int>mp;
13 map<int,int>::iterator it;
14 vector<int>ve;
15 
16 int main(){
17     #ifndef ONLINE_JUDGE
18         freopen("1.txt","r",stdin);
19     #endif
20     std::ios::sync_with_stdio(false);
21     string str;
22     cin>>str;
23     int co=0;
24     for(int i=0;i<str.length();i++){
25         if(str[i]!=','){
26             co=co*10+str[i]-'0';
27         }
28         else{
29             mp[co]=1;
30             co=0;
31         }
32     }
33     mp[co]=1;
34     for(it=mp.begin();it!=mp.end();it++){
35         ve.push_back(it->first);
36     }
37     ve.push_back(0x3f3f3f3f);
38     sort(ve.begin(),ve.end());
39     int pre=ve[0];
40     int flag=0;
41     for(int i=0;i<ve.size()-1;i++){
42         if(ve[i+1]-ve[i]!=1){
43             //cout<<endl<<ve[i]<<" "<<ve[i+1]<<endl;
44             if(flag) cout<<',';
45             if(pre!=ve[i])
46                 cout<<pre<<'-'<<ve[i];
47             else cout<<pre;
48             pre=ve[i+1];
49             if(!flag){
50                 flag=1;
51             }
52         }
53     }
54 }
View Code

 

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 vector<int>ve[50005];
13 int r1,r2;
14 int n;
15 int ans[50005];
16 int vis[50005];
17 
18 void dfs(int pos){
19     vis[pos]=1;
20     for(int i=0;i<ve[pos].size();i++){
21         if(!vis[ve[pos][i]]){
22             ans[ve[pos][i]]=pos;
23             dfs(ve[pos][i]);
24         }
25     }
26 }
27 
28 int main(){
29     #ifndef ONLINE_JUDGE
30        // freopen("1.txt","r",stdin);
31     #endif
32     std::ios::sync_with_stdio(false);
33     cin>>n>>r1>>r2;
34     int a;
35     for(int i=1;i<=n;i++){
36         if(i!=r1){
37             cin>>a;
38             ve[i].push_back(a);
39             ve[a].push_back(i);
40         }
41     }
42     dfs(r2);
43     for(int i=1;i<=n;i++){
44         if(i!=r2){
45             cout<<ans[i]<<" ";
46         }
47     }
48 }
View Code

 

 

posted on 2019-02-09 23:08  Fighting_sh  阅读(130)  评论(0编辑  收藏  举报

导航