暑假算法训练Day6(排序、二分、前缀和、差分)

115 篇文章 3 订阅

在这里插入图片描述

A - 纪念品分组

在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n,w;
const int N=3e4+10;
int a[N];
bool check(int x)
{
	int l=1,r=n;
	int sum=0;
	while(l<r)
	{
		if(a[l]+a[r]<=w)sum++,l++,r--;
		else r--;
	}
	return sum+(n-sum*2)<=x;
}
void solve()
{
	cin>>w>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	sort(a+1,a+1+n);
	int l=1,r=1e9;
	while(l<r)
	{
		int mid=l+r>>1;
		if(check(mid))r=mid;
		else l=mid+1;
	}
	cout<<r<<endl;
}
signed main()
{
    io;
 	//cin>>_; 
 	//while(_--)
    solve();
    return 0;
}

B - 眼红的Medusa

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n,m;
const int N=1e5+10;
int a[N];
void solve()
{
	cin>>n>>m;
	unordered_set<int>s;
	for(int i=1;i<=n;i++)cin>>a[i];
	vector<int>v;
	for(int i=1;i<=m;i++)
	{
		int x;
		cin>>x;
		s.insert(x);
	}
	for(int i=1;i<=n;i++)
	{
		if(s.count(a[i]))v.pb(a[i]);
	}
	for(auto x:v)cout<<x<<' ';
}
signed main()
{
    io;
 	//cin>>_; 
 	//while(_--)
    solve();
    return 0;
}

C - 烦恼的高考志愿

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n,m;
const int N=1e5+10;
int a[N],b[N];
void solve()
{
	cin>>m>>n;
	for(int i=1;i<=m;i++)cin>>a[i];
	for(int i=1;i<=n;i++)cin>>b[i];
	sort(a+1,a+1+m);
	int sum=0;
	for(int i=1;i<=n;i++)
	{
		int l=1,r=m;
		while(l<r)
		{
			int mid=l+r+1>>1;
			if(a[mid]<=b[i])l=mid;
			else r=mid-1;
		}
		int idx=r;
		l=1,r=m;
		while(l<r)
		{
			int mid=l+r>>1;
			if(a[mid]>=b[i])r=mid;
			else l=mid+1;
		}
		int res=min(abs(a[idx]-b[i]),abs(a[l]-b[i]));
		sum+=res;
	}
	cout<<sum<<endl;
}
signed main()
{
    io;
 	//cin>>_; 
 	//while(_--)
    solve();
    return 0;
}

D - 一元三次方程求解 (分类讨论)

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
double a,b,c,d;
const int N=1e5+10;
double f(double x)
{
	return a*x*x*x+b*x*x+c*x+d;
}
void solve()
{
	scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
	int cnt=0;
	for(int i=-100;i<100;i++)
	{
		double l=i,r=i+1;
		double x1=f(l),x2=f(r);
		if(!x1)
		{
			printf("%.2f ",l);
			cnt++;
		}
		if(x1*x2<0)
		{
			while(r-l>1e-7)
			{
				double mid=(r+l)/2;
				if(f(mid)*f(r)<=0)
				{
					l=mid;
				}
				else r=mid;
			}
			printf("%.2f ",r);
			cnt++;
		}
		if(cnt==3)break;
	}
}
signed main()
{
    //io;
 	//cin>>_; 
 	//while(_--)
    solve();
    return 0;
}

E - 数的划分 (TLE)

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n,k;
int res;
void dfs(int last,int sum,int cur)
{
	if(cur==k)
	{
		if(sum==n)res++;
		return;
	}
	for(int i=last;sum+i*(k-cur)<=n;i++)
		dfs(i,sum+i,cur+1);
}
void solve()
{
	cin>>n>>k;
	dfs(1,0,0);	
	cout<<res<<endl;
}
signed main()
{
    io;
 	//cin>>_; 
 	//while(_--)
    solve();
    return 0;
}

F - Weight of the System of Nested Segments

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n,m;
const int N=2e5+10;
struct node
{
	int x,w;
	int idx;
}a[N];
bool cmp1(node a,node b)
{
	return a.w<b.w;
}
bool cmp2(node a,node b)
{
	return a.x<b.x;
}
void solve()
{
	cin>>n>>m;
	for(int i=1;i<=m;i++)cin>>a[i].x>>a[i].w,a[i].idx=i;
	int sum=0;
	sort(a+1,a+1+m,cmp1);
	for(int i=1;i<=2*n;i++)sum+=a[i].w;
	sort(a+1,a+1+2*n,cmp2);
	cout<<sum<<endl;
	for(int i=1;i<=n;i++)
	cout<<a[i].idx<<' '<<a[2*n+1-i].idx<<endl;
}
signed main()
{
    io;
 	cin>>_; 
 	while(_--)
    solve();
    return 0;
}

G - LJUBOMORA(check)

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n,m;
const int N=3e5+10;
int a[N];
bool check(int x)
{
	int sum=0;
	for(int i=1;i<=m;i++)
	{
		sum+=ceil(double(a[i])/x);
	}
	return sum<=n;
}
void solve()
{
	cin>>n>>m;
	int maxv=0;
	for(int i=1;i<=m;i++)cin>>a[i],maxv=max(maxv,a[i]);
	int l=1,r=maxv;
	while(l<r)
	{
		int mid=l+r>>1;
		if(check(mid))r=mid;
		else l=mid+1;
	}
	cout<<l<<endl;
}
signed main()
{
    io;
 	//cin>>_; 
 //	while(_--)
    solve();
    return 0;
}

H - 插入排序(大模拟 TLE)

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n,m;
const int N=3e5+10;
int a[N];
bool check(int x)
{
	int sum=0;
	for(int i=1;i<=m;i++)
	{
		sum+=ceil(double(a[i])/x);
	}
	return sum<=n;
}
void solve()
{
	cin>>n>>m;
	int maxv=0;
	for(int i=1;i<=m;i++)cin>>a[i],maxv=max(maxv,a[i]);
	int l=1,r=maxv;
	while(l<r)
	{
		int mid=l+r>>1;
		if(check(mid))r=mid;
		else l=mid+1;
	}
	cout<<l<<endl;
}
signed main()
{
    io;
 	//cin>>_; 
 //	while(_--)
    solve();
    return 0;
}

K - Black and White Stripe

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n,m;
const int N=2e5+10;
int sum[N];
void solve()
{
	cin>>n>>m;
	for(int i=0;i<=n;i++)sum[i]=0;
	string s;
	cin>>s;
	for(int i=0;i<s.size();i++)
	{
		if(s[i]=='B')sum[i+1]=1;
		else sum[i+1]=0;
	}
	for(int i=1;i<=n;i++)sum[i]=sum[i-1]+sum[i];
	int maxv=0;
	for(int i=1;i+m-1<=n;i++)
	{
		int l=i,r=i+m-1;
		maxv=max(maxv,sum[r]-sum[l-1]);
	}
	cout<<m-maxv<<endl;
}
signed main()
{
    io;
 	cin>>_; 
 	while(_--)
    solve();
    return 0;
}

L - Balanced Stone Heaps(greedy+bs)

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n,m;
const int N=2e5+10;
int a[N];
int b[N];
bool check(int x)
{
	for(int i=1;i<=n;i++)b[i]=a[i];
	for(int i=n;i>2;i--)
	{
		if(b[i]<x)return false;
		else
		{
			int d=min(a[i],b[i]-x)/3;
			b[i-1]+=d;
			b[i-2]+=2*d;
		}
	}
	return b[1]>=x&&b[2]>=x;
}
void solve()
{
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	int l=0,r=1e9;
	while(l<r)
	{
		int mid=l+r+1>>1;
		if(check(mid))l=mid;
		else r=mid-1;
	}
	cout<<l<<endl;
}
signed main()
{
    io;
 	cin>>_; 
 	while(_--)
    solve();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

leimingzeOuO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值