連接:https://codeforces.com/contest/670html
AC代碼:ios
#include<bits/stdc++.h> using namespace std; int n; int main() { cin>>n; int q=n/7, r=n%7; int least=q*2; int a,b; if(r==0) a=b=0; if(r==1) a=0, b=1; if(2<=r && r<=5) a=0, b=2; if(r==6) a=1, b=2; printf("%d %d\n",least+a,least+b); }
AC代碼:c++
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=1e5+10; int n; ll k; int id[maxn]; int main() { cin>>n>>k; for(int i=1;i<=n;i++) scanf("%d",&id[i]); ll p; for(p=1;p<=n;p++) if((p-1)*p/2<k && k<=p*(p+1)/2) break; k-=(p-1)*p/2; cout<<id[k]<<endl; }
AC代碼:spa
#include<bits/stdc++.h> using namespace std; const int maxn=2e5+10; const int maxm=2e5+10; int n,m; int a[maxn],cnt[maxn+2*maxm]; vector<int> v; inline int getID(int x){return lower_bound(v.begin(),v.end(),x)-v.begin()+1;} inline int getVal(int id){return v.at(id-1);} struct Mo{ int id; int x,y; }mo[maxm]; bool cmp(const Mo& a,const Mo& b) { if(cnt[a.x]==cnt[b.x]) return cnt[a.y]>cnt[b.y]; else return cnt[a.x]>cnt[b.x]; } int main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin>>n; for(int i=1;i<=n;i++) cin>>a[i], v.push_back(a[i]); cin>>m; for(int i=1;i<=m;i++) cin>>mo[i].x, v.push_back(mo[i].x); for(int i=1;i<=m;i++) cin>>mo[i].y, v.push_back(mo[i].y); sort(v.begin(),v.end()); v.erase(unique(v.begin(),v.end()),v.end()); for(int i=1;i<=n;i++) { a[i]=getID(a[i]); cnt[a[i]]++; } for(int i=1;i<=m;i++) { mo[i].id=i; mo[i].x=getID(mo[i].x); mo[i].y=getID(mo[i].y); } sort(mo+1,mo+m+1,cmp); cout<<mo[1].id<<endl; }
題解:code
二分能生產的餅乾數目,對於一個餅乾數目 $x$ 咱們能夠 $O(n)$ 判斷出是否可行。htm
AC代碼:blog
#include<bits/stdc++.h> using namespace std; typedef long long ll; const ll INF=0x3f3f3f3f3f3f3f3f; const int maxn=1e5+10; int n; ll k,a[maxn],b[maxn]; bool check(ll x) { ll need=0; for(int i=1;i<=n;i++) { if(x*a[i]>b[i]) need+=x*a[i]-b[i]; if(need>k) return 0; } if(need>k) return 0; else return 1; } int main() { cin>>n>>k; for(int i=1;i<=n;i++) scanf("%I64d",&a[i]); for(int i=1;i<=n;i++) scanf("%I64d",&b[i]); ll up=INF; for(int i=1;i<=n;i++) up=min(up,(b[i]+k)/a[i]); ll l=0, r=up; while(l<r) { ll mid=(l+r+1LL)/2; if(check(mid)) l=mid; else r=mid-1; } cout<<l<<endl; }
一、線段樹作法;排序
二、鏈表作法;ci
三、對頂棧作法。字符串