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 100005int 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;}新闻热点
疑难解答