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[];
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=;i<=n;i++) cin>>a[i];
a[n+]=a[];
int pos1,pos2,Min=0x3f3f3f3f;
for(int i=;i<=n;i++){
if(abs(a[i]-a[i+])<Min){
Min=abs(a[i]-a[i+]);
pos1=i;
pos2=i+;
}
}
if(pos2==n+) pos2=;
cout<<pos1<<" "<<pos2<<endl;
}
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[];
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=;i<=n;i++) cin>>a[i];
sort(a+,a+n+);
int ans=;
for(int i=;i<=n&&m;i++){
if(a[i]<){
ans-=a[i];
m--;
}
}
cout<<ans<<endl;
}
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=;
for(int i=;i<str.length();i++){
if(str[i]!=','){
co=co*+str[i]-'';
}
else{
mp[co]=;
co=;
}
}
mp[co]=;
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[];
int flag=;
for(int i=;i<ve.size()-;i++){
if(ve[i+]-ve[i]!=){
//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+];
if(!flag){
flag=;
}
}
}
}
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[];
int r1,r2;
int n;
int ans[];
int vis[]; void dfs(int pos){
vis[pos]=;
for(int i=;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=;i<=n;i++){
if(i!=r1){
cin>>a;
ve[i].push_back(a);
ve[a].push_back(i);
}
}
dfs(r2);
for(int i=;i<=n;i++){
if(i!=r2){
cout<<ans[i]<<" ";
}
}
}