Problem A :
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define F(i,a,b) for(int i=a;i<=b;i++)
#define maxn 100005
int main(){
int n,num[2*maxn];
scanf("%d",&n);
F(i,1,2*n) scanf("%d",&num[i]);
int count=0,res=0,vis[maxn];
memset(vis, 0, sizeof(vis));
F(i,1,2*n){
if(vis[num[i]]==0){
vis[num[i]]=1;
count++;
}else{
count--;
}
res = max(res, count);
}
cout<<res<<endl;
return 0;
}
Problem B :
这题刚开始没有什么思路,后来想通了是用二分查找,注意在STL库里面max 和 min比较double,尾数越多比较的时间越长,选择 1e-7 才不容易TLE
#include <iostream>
#include <cstdio>
using namespace std;
const double eps=1e-7;
#define maxn 60005
#define F(i,a,b) for(int i=a;i<=b;i++)
int n,x[maxn],v[maxn],sum;
bool check(double m){
double b=-2e9,t=2e9;
F(i,1,n){
b = max(b,x[i]-v[i]*m);
t = min(t,x[i]+v[i]*m);
}
return b<=t;
}
int main(){
sum =0;
scanf("%d",&n);
F(i,1,n) {scanf("%d",&x[i]);sum+=x[i];}
F(i,1,n) scanf("%d",&v[i]);
double L=0,R=1e9,M;
while((R-L)>=eps){
M = (L+R)/2.0;
if(check(M)) R=M;
else L=M;
}
printf("%.9lf\n",(L+R)/2.0);
return 0;
}
Problem C :
#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 2e5+5;
int n,color[maxn],vis[maxn],res=0;
vector<int> v[maxn];
void dfs(int d){
int now = 1;
vector<int>::iterator it;
for(it=v[d].begin();it!=v[d].end();it++){
if(*it==vis[d]) continue;
vis[*it]=d;
while (color[d]==now || color[vis[d]]==now) now++;
color[*it]=now;
now++;
}
res=max(now-1,res);
for(it=v[d].begin();it!=v[d].end();it++){
if(*it!=vis[d])
dfs(*it);
}
}
int main(){
scanf("%d",&n);
for(int i=0,x,y;i<n-1;i++){
scanf("%d%d",&x,&y);
v[x].push_back(y);
v[y].push_back(x);
}
color[1] = 1;
dfs(1);
cout<<res<<endl;
for(int i=1;i<n;i++)
printf("%d ",color[i]);
cout<<color[n]<<endl;
return 0;
}