2020年牛客算法入门课练习赛1题解
A.第k小数
题意:
题解:
AC代码
/* Author:zzugzx Lang:C++ Blog:blog.csdn.net/qq_43756519 */ #include<bits/stdc++.h> using namespace std; #define fi first #define se second #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define endl '\n' typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; //const int mod=1e9+7; const int mod=998244353; const double eps = 1e-10; const double pi=acos(-1.0); const int maxn=4e6+10; const ll inf=0x3f3f3f3f; const int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}}; int a[maxn]; int main() { ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int _; while(cin>>_){ while(_--){ int n,k; cin>>n>>k; for(int i=1;i<=n;i++)cin>>a[i]; sort(a+1,a+1+n); cout<<a[k]<<endl; } } return 0; }
B.不平行的直线
题意:
题解:
AC代码
/* Author:zzugzx Lang:C++ Blog:blog.csdn.net/qq_43756519 */ #include<bits/stdc++.h> using namespace std; #define fi first #define se second #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define endl '\n' typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; //const int mod=1e9+7; const int mod=998244353; const double eps = 1e-10; const double pi=acos(-1.0); const int maxn=1e6+10; const ll inf=0x3f3f3f3f; const int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}}; double x[210],y[210]; int main() { ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int n; cin>>n; for(int i=1;i<=n;i++)cin>>x[i]>>y[i]; set<double> s; int f=0; for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++){ if(!(y[i]-y[j])){f=1;continue;} s.insert((x[i]-x[j])/(y[i]-y[j])); } cout<<s.size()+f; return 0; }
C. 丢手绢
题意:
题解:
AC代码
/* Author:zzugzx Lang:C++ Blog:blog.csdn.net/qq_43756519 */ #include<bits/stdc++.h> using namespace std; #define fi first #define se second #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define endl '\n' typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; //const int mod=1e9+7; const int mod=998244353; const double eps = 1e-10; const double pi=acos(-1.0); const int maxn=1e6+10; const ll inf=0x3f3f3f3f; const int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}}; ll i,a[maxn]; ll s[maxn],sum; ll check(ll x){ ll y=s[x]-s[i-1]; return min(y,sum-y); } int main() { ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int n;cin>>n; for(i=1;i<=n;i++) cin>>a[i],sum+=a[i]; for(i=1;i<=2*n;i++){ if(i<=n)s[i]=s[i-1]+a[i]; else s[i]=s[i-1]+a[i-n]; } ll ans=0; for(i=1;i<=n;i++){ ll l=i,r=i+n; while(l+10<r){ ll lm=l+(r-l)/3,rm=r-(r-l)/3; if(check(lm)>=check(rm))r=rm; else l=lm; } for(ll j=l;j<=r;j++) ans=max(ans,check(j)); } cout<<ans; return 0; }
D. 二分
题意:
题解:
AC代码
/* Author:zzugzx Lang:C++ Blog:blog.csdn.net/qq_43756519 */ #include<bits/stdc++.h> using namespace std; #define fi first #define se second #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define endl '\n' typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; //const int mod=1e9+7; const int mod=998244353; const double eps = 1e-10; const double pi=acos(-1.0); const int maxn=1e6+10; const ll inf=0x3f3f3f3f; const int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}}; int n; ll x[maxn],a[maxn],c[maxn]; char y[maxn]; int main() { ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); cin>>n; int len=0; for(int i=1;i<=n;i++){ cin>>x[i]>>y[i]; c[++len]=x[i]; c[++len]=x[i]-1; c[++len]=x[i]+1; } sort(c+1,c+1+len); len=unique(c+1,c+1+len)-c-1; for(int i=1;i<=n;i++){ int p=lower_bound(c+1,c+1+len,x[i])-c; if(y[i]=='.')a[p]++,a[p+1]--; if(y[i]=='-')a[p+1]++,a[len+10]--; if(y[i]=='+')a[1]++,a[p]--; } ll ans=0; for(int i=1;i<=len+10;i++) a[i]+=a[i-1],ans=max(ans,a[i]); cout<<ans; return 0; }
E. 交换
题意:
题解:
AC代码
/* Author:zzugzx Lang:C++ Blog:blog.csdn.net/qq_43756519 */ #include<bits/stdc++.h> using namespace std; #define fi first #define se second #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define endl '\n' typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; //const int mod=1e9+7; const int mod=998244353; const double eps = 1e-10; const double pi=acos(-1.0); const int maxn=1e6+10; const ll inf=0x3f3f3f3f; const int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}}; bool flag[maxn]; int main() { ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int n;cin>>n; vector<int> v; for(int i=1,x;i<=n;i++){ cin>>x; v.pb(x); } map<int,int> m; vector<int> v1=v; sort(all(v1)); for(int i=0;i<n;i++)m[v1[i]]=i; int ans=0; for(int i=0;i<n;i++){ if(!flag[i]){ int j=i; while(!flag[j]){ flag[j]=1; j=m[v[j]]; } ans++; } } cout<<n-ans; return 0; }