- Codeforces Beta Round #34 (Div. 2)
- http://codeforces.com/contest/34
- A
- #include<bits/stdc++.h>
- using namespace std;
- #define lson l,mid,rt<<1
- #define rson mid+1,r,rt<<1|1
- #define sqr(x) ((x)*(x))
- #define maxn 500005
- typedef long long ll;
- /*#ifndef ONLINE_JUDGE
- freopen("1.txt","r",stdin);
- #endif */
- int n;
- int a[105];
- map<int,int>mp;
- int main(){
- #ifndef ONLINE_JUDGE
- // freopen("1.txt","r",stdin);
- #endif
- std::iOS::sync_with_stdio(false);
- int n;
- cin>>n;
- for(int i=1;i<=n;i++) cin>>a[i];
- a[n+1]=a[1];
- int pos1,pos2,Min=0x3f3f3f3f;
- for(int i=1;i<=n;i++){
- if(abs(a[i]-a[i+1])<Min){
- Min=abs(a[i]-a[i+1]);
- pos1=i;
- pos2=i+1;
- }
- }
- if(pos2==n+1) pos2=1;
- cout<<pos1<<" "<<pos2<<endl;
- }
- View Code
- B
- #include<bits/stdc++.h>
- using namespace std;
- #define lson l,mid,rt<<1
- #define rson mid+1,r,rt<<1|1
- #define sqr(x) ((x)*(x))
- #define maxn 500005
- typedef long long ll;
- /*#ifndef ONLINE_JUDGE
- freopen("1.txt","r",stdin);
- #endif */
- int n;
- int a[105];
- map<int,int>mp;
- int main(){
- #ifndef ONLINE_JUDGE
- // freopen("1.txt","r",stdin);
- #endif
- std::iOS::sync_with_stdio(false);
- int n;
- int m;
- cin>>n>>m;
- for(int i=1;i<=n;i++) cin>>a[i];
- sort(a+1,a+n+1);
- int ans=0;
- for(int i=1;i<=n&&m;i++){
- if(a[i]<0){
- ans-=a[i];
- m--;
- }
- }
- cout<<ans<<endl;
- }
- View Code
- C
- map+vector
- #include<bits/stdc++.h>
- using namespace std;
- #define lson l,mid,rt<<1
- #define rson mid+1,r,rt<<1|1
- #define sqr(x) ((x)*(x))
- #define maxn 500005
- typedef long long ll;
- /*#ifndef ONLINE_JUDGE
- freopen("1.txt","r",stdin);
- #endif */
- map<int,int>mp;
- map<int,int>::iterator it;
- vector<int>ve;
- int main(){
- #ifndef ONLINE_JUDGE
- freopen("1.txt","r",stdin);
- #endif
- std::iOS::sync_with_stdio(false);
- string str;
- cin>>str;
- int co=0;
- for(int i=0;i<str.length();i++){
- if(str[i]!=','){
- co=co*10+str[i]-'0';
- }
- else{
- mp[co]=1;
- co=0;
- }
- }
- mp[co]=1;
- for(it=mp.begin();it!=mp.end();it++){
- ve.push_back(it->first);
- }
- ve.push_back(0x3f3f3f3f);
- sort(ve.begin(),ve.end());
- int pre=ve[0];
- int flag=0;
- for(int i=0;i<ve.size()-1;i++){
- if(ve[i+1]-ve[i]!=1){
- //cout<<endl<<ve[i]<<" "<<ve[i+1]<<endl;
- if(flag) cout<<',';
- if(pre!=ve[i])
- cout<<pre<<'-'<<ve[i];
- else cout<<pre;
- pre=ve[i+1];
- if(!flag){
- flag=1;
- }
- }
- }
- }
- View Code
- D
题意不好理解, 是要求树换一个根结点后的前驱
- #include<bits/stdc++.h>
- using namespace std;
- #define lson l,mid,rt<<1
- #define rson mid+1,r,rt<<1|1
- #define sqr(x) ((x)*(x))
- #define maxn 500005
- typedef long long ll;
- /*#ifndef ONLINE_JUDGE
- freopen("1.txt","r",stdin);
- #endif */
- vector<int>ve[50005];
- int r1,r2;
- int n;
- int ans[50005];
- int vis[50005];
- void dfs(int pos){
- vis[pos]=1;
- for(int i=0;i<ve[pos].size();i++){
- if(!vis[ve[pos][i]]){
- ans[ve[pos][i]]=pos;
- dfs(ve[pos][i]);
- }
- }
- }
- int main(){
- #ifndef ONLINE_JUDGE
- // freopen("1.txt","r",stdin);
- #endif
- std::iOS::sync_with_stdio(false);
- cin>>n>>r1>>r2;
- int a;
- for(int i=1;i<=n;i++){
- if(i!=r1){
- cin>>a;
- ve[i].push_back(a);
- ve[a].push_back(i);
- }
- }
- dfs(r2);
- for(int i=1;i<=n;i++){
- if(i!=r2){
- cout<<ans[i]<<" ";
- }
- }
- }
- View Code
来源: http://www.bubuko.com/infodetail-2948914.html