ybtoj:二分算法

A:数列分段

点击查看代码
#include<bits/stdc++.h>
using namespace std;
int m,n;
int a[100002];
int l,r;

bool check(int limit){
	int cnt=1,sum=0;
	for(int i=1;i<=n;i++)
	{
		if(sum+a[i]>limit){
			cnt++;
			sum=a[i];
		}
		else{
			sum+=a[i];
			
		}
	}
	return cnt<=m;
	
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		r+=a[i];
		if(a[i]>l)
		{
			l=a[i];
		}
	}
	
	while(l<r)
	{
		int mid=(l+r)>>1;
		if(check(mid)==1)
		{
			r=mid;
		}
		else{
			l=mid+1;
		}
	}
	cout<<l;
	
	return 0;
} 

B:防具布置

点击查看代码
#include<bits/stdc++.h>
using namespace std;

#define int long long
const int N=2e5+15;
int t;
int n;
int s[N], e[N], d[N];

int num(int k) {
	int ans = 0;
	for (int ii = 1; ii <= n; ii++) {
		if (s[ii] <= k)ans += (min(k, e[ii]) - s[ii] / d[ii] + 1);

	}
	return ans;

}

void work() {
	scanf("%lld",&n);
	
	for (int i = 1; i <= n; i++) {
		scanf("%lld%lld%lld", &s[i], &e[i], &d[i]);

	}
	
	//cout<<r<<endl;
	if (num(2147483649) % 2 == 0) {
		printf("There's no weakness.\n");
		
		//cout << "" << endl;
		return;

	}
	int l = 0, r = 2147483647;
	while (l < r) {
		int mid = (long long)(l + r) >> 1;
		if (num(mid) & 1) {
			r = mid;

		} else {
			l = mid + 1;
		}

	}
	printf("%lld %lld\n",l,num(l)-num(l-1));
	
	//cout << l << " " << num(l) - num(l - 1) << endl;
}
signed main() {
	cin >> t;
	while (t--) {
		work();
	}
	return 0;
}

C:最大均值

点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int N=100005;

int n,l;
double a[N],qzh[N],b[N];
const double e=1e-5;


bool check(double num){
	for(int i=1;i<=n;i++) b[i]=a[i]-num;
	for(int i=1;i<=n;i++) qzh[i]=qzh[i-1]+b[i];
	double minn=1e8;
	double ans=-1e8;
	
	
	for(int i=l;i<=n;i++){
		minn=min(minn,qzh[i-l]);
		ans=max(ans,qzh[i]-minn);
	}
	return ans>=0;
}
int main(){
	scanf("%d%d",&n,&l);
	for(int i=1;i<=n;i++){
		scanf("%lf",&a[i]);
	}
	double l=-1e6,r=1e6;
	while(l+e<r){
		double mid=(l+r)/2;
		if(check(mid)){
			l=mid;
		}
		else {
			r=mid;
		}
		
	}
	cout<<int(r*1000)<<endl;
	
	return 0;
	
}

D:喂养宠物

点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int N=55;
int hun[N],grd[N];

int n,tot;

bool rabbit(int x){
	int re[N];
	for(int i=1;i<=n;i++)re[i]=hun[i]+(x-1)*grd[i];
	sort(re+1,re+n+1);
	int ans=0;
	for(int i=1;i<=x;i++){
		ans+=re[i];
		
	}
	return ans<=tot;
}
int main(){
	scanf("%d%d",&n,&tot);
	for(int i=1;i<=n;i++)scanf("%d",&hun[i]);
	for(int j=1;j<=n;j++)scanf("%d",&grd[j]);
	int l=1,r=n;
	while(l<r){
		int mid=(l+r)/2;
		
		if(rabbit(mid)) l=mid+1;
		else r=mid;
		
	}
	cout<<l-1<<endl;
	return 0;
}

E:最小时间

点击查看代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const int N=1e6+10;

int n,m;
ll s;
int k[N],b[N];
ll val[N];

bool check(int x){
	for(int i=1;i<=n;i++){
		val[i]=1ll*k[i]*x + b[i];
	}
	nth_element(val+1,val+m,val+n+1,greater<ll>());
	ll sum=0;
	for(int i=1;i<=m;i++){
		if(val[i]>0 && (sum+=val[i])>=s){
			return true;
		}
	}
	return sum>=s;
	
}
int main(){
	scanf("%d%d%lld",&n,&m,&s);
	for(int i=1;i<=n;i++){
		scanf("%d%d",&k[i],&b[i]);
	}
	if(check(0)){
		cout<<0;
		return 0;
	}
	int l=1,r=1e9+1;
	while(l<r){
		int mid=(l+r)/2;
		if(check(mid)) r=mid;
		else l=mid+1;
	}
	printf("%d\n",l);
	
	return 0;
}

F:攻击法坛

点击查看代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,R,G;
const int N=2024;
int a[N],p[N],q[N],dp[N][N];
int ans;

inline bool check(int l){
	memset(dp,0,sizeof(dp));
	memset(p,0,sizeof(p));
	memset(q,0,sizeof(q));
	for(int i=1;i<=n;i++){
		for(int j=i;j<=n;j++){
			if(a[j]-a[i]+1<=l){
				p[i]=j;
			}
			if(a[j]-a[i]+1<=2*l){
				q[i]=j;
			}
		}
	}
	p[n+1]=q[n+1]=n;
	for(int i=0;i<=R;i++){
		for(int j=0;j<=G;j++){
			if(i>0){
				dp[i][j]=max(dp[i][j],p[dp[i-1][j]+1]);
				
			}
			if(j>0){
				dp[i][j]=max(dp[i][j],q[dp[i][j-1]+1]);
			}
		}
	}
	return dp[R][G]==n;
}

signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	cin>>n>>R>>G;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	sort(a+1,a+n+1);
	int l=1,r=a[n]-a[1]+1;
	if(R+G>=n){
		cout<<"1\n";
		return 0;
	}
	
	while(l<=r){
		int mid=l+r>>1;
		if(check(mid)){
			r=mid-1;ans=mid;
		}
		else {
			l=mid+1;
		}
	}
	cout<<ans<<"\n";
	
	return 0;
}

G:跳石头

点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int N=5e4+10;

int l,n,m;
int dis[N];

inline bool nb(int x){
	int sum=0,cnt=0;
	for(int i=1;i<=n;i++){
		if(dis[i]-sum<x) cnt++;
		else sum=dis[i];
	}
	if(l-sum<x) cnt++;
	return cnt<=m;
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	cin>>l>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>dis[i];
	}
	
	int ll=0,rr=l;
	while(ll<rr){
		int mid=(ll+rr+1)/2;
		if(nb(mid))ll=mid;
		else rr=mid-1;
	}
	cout<<ll;
	return 0;
}

H:飞离地球

点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int N=114514,M=1919810;
int n,m,cnt,t;
int l=-1e5,r=1e5;
long long ans;

struct edge{
	int w,to,nxt;
}e[M*2];
int u[N],v[N],w[N],head[N],dis[N],cis[N];
bool vis[N],arrive[N];

inline int read(){
	int cxk=0,f=1;
	char c=getchar();
	while(c<'0' || c>'9'){
		if(c=='-') f=-f;
		c=getchar();
	}
	while(c>='0' && c<='9'){
		cxk=(cxk<<3)+(cxk<<1)+c-'0';
		c=getchar();
	}
	return cxk*f;
}

inline void add(int u,int v,int w){
	e[++cnt].w=w;
	e[cnt].to=v;
	e[cnt].nxt=head[u];
	head[u]=cnt;
	
}

inline void dfs(int u){
	arrive[u]=1;
	for(int i=head[u];i;i=e[i].nxt){
		int t=e[i].to;
		if(!arrive[t]) dfs(t);
	}
}

inline int spfa(int x){
	queue<int> q;
	memset(dis,0x3f3f3f3f,sizeof(dis));
	memset(vis,0,sizeof(vis));
	memset(cis,0,sizeof(cis));
	q.push(1);
	vis[1]=1,dis[1]=0,cis[1]=1;
	while(!q.empty()){
		int k=q.front();
		q.pop();
		vis[k]=0;
		for(int i=head[k];i;i=e[i].nxt){
			int t=e[i].to;
			if(!arrive[t]) continue;
			if(dis[k]+e[i].w+x<dis[t]){
				dis[t]=dis[k]+e[i].w+x;
				if(!vis[t]){
					vis[t]=1;
					cis[t]=cis[k]+1;
					if(cis[t]>n) return -1;
					q.push(t);
				}
			}
		}
	}
	return dis[n];
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	t=read();
	while(t--){
		memset(arrive,0,sizeof(arrive));
		memset(head,0,sizeof(head));
		cnt=0;
		n=read(),m=read();
		for(int i=1;i<=m;i++){
			u[i]=read(),v[i]=read(),w[i]=read();
			add(v[i],u[i],w[i]);
			
		}
		dfs(n);
		if(!arrive[1]){
			cout<<"-1\n";
			continue;
		}
		memset(head,0,sizeof(head));
		cnt=0;
		for(int i=1;i<=m;i++) add(u[i],v[i],w[i]);
		l=-1e5,r=1e5;
		while(l<r){
			int mid=l+r>>1;
			long long val=spfa(mid);
			if(val>=0){
				r=mid;
				ans=val;
			}
			else l=mid+1;
		}
		cout<<ans<<"\n";
	}
	return 0;
}

//抄袭绝非良策,理解才是正道

posted @ 2024-11-18 09:36  cathyzro  阅读(23)  评论(0)    收藏  举报