6 solutions
-
2
二分答案
#include<iostream> using namespace std; const int N=1e6+7; int a[N],n,k; int main(){ cin>>n>>k; for(int i=1;i<=n;i++) cin>>a[i]; int l=1,r=N,mid=0,ans; while(l<=r){ mid=(r+l)/2; int tmp=0; for(int i=1;i<=n;i++) tmp+=(a[i]/mid); if(tmp<k) r=mid-1; else l=mid+1,ans=mid; } cout<<ans<<endl; return 0; }
-
1
#include<bits/stdc++.h> using namespace std; const int N=1e7+5; int a[N],n,k,sum=0,l=0,r,ans; bool check(int x){ int g=0; for(int i=1;i<=n;i++){ g+=a[i]/x; } return g>=k; } int main(){ cin>>n>>k; for(int i=1;i<=n;i++){ cin>>a[i]; sum+=a[i]; } sort(a+1,a+n+1); r=sum/k; if(a[1]<k){ cout<<0; return 0; } if(a[1]==k){ cout<<a[1]; return 0; } while(l<=r){ int mid=(l+r)>>1; if(check(mid)){ l=mid+1; } else{ r=mid-1; } } cout<<l-1; return 0; }
-
0
二分
#include<iostream> using namespace std; #define ll long long const int N = 1e7 + 5; ll n, k, Max; ll a[N]; bool check(ll x) { ll cnt = 0; for (ll i = 1; i <= n; ++i) { cnt += a[i] / x; } return cnt >= k; } int main() { scanf("%lld%lld", &n, &k); for (ll i = 1; i <= n; ++i) { scanf("%lld", &a[i]); if (a[i] > Max)Max = a[i]; } ll l = 0, r = Max, mid; while (l < r) { mid = (l + r) >> 1; if (check(mid))l = mid + 1; else r = mid; } printf("%lld\n", r - 1); return 0; }
-
0
#include<iostream> using namespace std; typedef long long LL; const int N=1e8+10; int n,k; LL a[N]; bool check(int x){ int m=0; for(int i=0;i<n;i++){ m+=a[i]/x;//a[i]/x表示的是当前木棍能切出的长度为x的块数,把所有木棍的对应值加起来就是当前x的最大切出来的块数 } if(m>=k){ return true; } return false; } int main() { cin>>n>>k; for(int i=0;i<n;i++) cin>>a[i]; int l=0,r=N; while(l<r){ int mid=(l+r+1)/2; if(check(mid)) l=mid;//如果当前的最大值满足大于等于k,说明l还有可能更大,所以往右边找 else r=mid-1; } cout<<l<<endl; return 0; }
-
0
#include<bits/stdc++.h>
using namespace std;
long long n, k;
long long a[1000005];
bool check(long long x) {
long long cnt = 0; for (int i = 0; i < n; i++) { cnt += a[i] / x; } return cnt >= k;
}
int main() {
cin >> n >> k; for (int i = 0; i < n; i++) cin >> a[i]; long long l = 0, r = 100000001; while (l < r) { long long mid = (l + r+1)>>1; if (check(mid)) l = mid; else r = mid-1; } cout << l << endl; return 0;
}
-
0
嗯,经典二分,找到条件就可以了
#include<iostream> #include<algorithm> using namespace std; const int N=1e6+7; int n,m,ans,a[N]; bool check(int x){ int k=0; for(int i=0;i<n;i++){ k+=a[i]/x; if(k>=m)return true; } return false ; } int find(int l,int r){ int mid; while(l<=r){ mid=(l+r)>>1; if(cheak(mid))l=mid+1; else r=mid-1; } return l-1; } int main(){ cin>>n>>m; for(int i=0;i<n;i++){ cin>>a[i]; } sort(a,a+n); ans=find(0,10000007); cout<<ans; return 0; }
- 1
Information
- ID
- 281
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 3
- Tags
- # Submissions
- 141
- Accepted
- 73
- Uploaded By