队内排位赛(三)

题目传送门

A.Wormhole Sort(二分+并查集)

题目描述

有n头牛,编号为1~n,初始时它们在某个位置上,现在有m个虫洞,宽度为wi,连接ai与bi两个点,求如若把每头牛传送送到其编号节点的位置上,需要虫洞宽度的最大的最小值为多少?

算法分析

首先我们对m条边按w从大到小排序,然后我们可以用二分的方法来枚举需要用到前多少条边。对于是否符合题意,对于每条边可以使其对应的两个点做并查集合并操作,所以如果第i头牛初始位置与i是在同一集合中的话,那么就符合题意。

代码

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const int N = 1e5 +10;
int p[N],n,m,a[N];
struct Edge
{
	int a,b,w;
}edge[N];
bool cmp(Edge x, Edge y)
{
	return x.w > y.w;
}
int find(int x)
{
	if(x!=p[x])p[x] = find(p[x]);
	return p[x];
}
void add(int x)
{
	int a = edge[x].a, b =edge[x].b;
	p[find(a)]=find(b);
}
bool check(int x)
{
	for(int i = 1; i <= n; i ++)p[i] = i;
	for(int i = 1; i <= x; i ++)add(i);
	for(int i = 1; i <= n; i ++)if(find(i)!=find(a[i]))
		return false;
	return true;
}
int main()
{
	cin>> n>>m;
	for(int i = 1; i <= n; i ++)
	scanf("%d",&a[i]);

	for(int i = 1; i <= m; i ++)
		scanf("%d%d%d",&edge[i].a,&edge[i].b,&edge[i].w);
	sort(edge+1,edge+1+m,cmp);
	LL l = 0, r = m;
	while(l < r)
	{
		LL mid = (l+r)/2;
		if(check(mid))
			r = mid;
		else l = mid+1;
	}
	if(l)cout << edge[l].w;
	else cout << -1;
	return 0;
 } 

B. Loan Repayment(二分)

题目大意

John欠Bessien加仑牛奶,John需要在k天内归还,请你确定最大整数x,使得John每日归还max((n-g)/ x(下取整,g为已归还的牛奶),m)加仑牛奶可以满足题意

题目解析

首先对于x可以分为满足题意与不满足题意两个区间,所以可以二分x找到最大值。对于check函数如果只是简单的模拟的话,因为数据给得很大,所以会超时,事实上,存在一些连续的天,每日归还的牛奶不变,所以我们可以算出这样连续的天数,并合并为一次操作。

代码

cpp#include <iostream>

using namespace std;
typedef long long LL;
LL n,m,k;
bool check(LL x)
{
	LL day = 0, r = n, cnt = 0;
	LL t = r/x;
	while(t > m)
	{
		t = r/x;
		day = (double)r/(double)t - x;
		day ++;
		r -= day*t;
		cnt += day;
	}
	cnt += r/m;
	if(r%m)cnt++;
	return cnt <= k;
} 
int main()
{
	cin >> n >> k >>m;
	LL l = 1, r = n;
	while(l < r)
	{
		LL mid = (l+r+1)/2;
		if(check(mid))l = mid;
		else r = mid - 1;
	}
	cout << l;
	return 0;
 }

D. Race(二分)

题目大意

Bessie要跑完一个长度为k的跑道,她初始位置与速度都为0,每秒她可以加速1个单位/s或者减速1个单位/s或者保持当前速度,并且她到达终点的速度不能超过x,问Bessie最少需要多少秒跑完

题目解析

通过贪心容易得到:最高速度越快,方案所需时间越小,所以可以通过二分来寻找最大的最快速度

代码

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
LL m,n,k,q,s[1000000];
bool check(int x)
{
	LL x1 = ((1+x)*x) / 2, x2 = (n+x-1)*(x-n)/2;
	return x1+x2 <= k;
		
}
int main()
{
	cin >> k >> q;
	LL maxn;
	for(int i = 1; ;i ++)
	{
		s[i] = s[i-1] + i;
		if(s[i]>=k)
		{
			maxn = i;
			break;
		}
	}
	while(q--)
	{
		cin >> n;
		if(n>=maxn)
		{
			cout << maxn<<endl;
			continue;
		}
		LL l = n, r = maxn;
		while(l < r)
		{
			LL mid = (l+r+1)/2;
			if(check(mid))l = mid;
			else r = mid-1;
		}
		LL t1 = l-1,t3 = l-n+1;
		LL x1 = s[l-1],x3 = s[l]-s[n-1];
		LL x2 = k-x1-x3;
		LL t2 = x2/l;
		if(x2%l!=0)t1++;
		cout << t1+t2+t3<<endl;
	}
	return 0;
 } 

E. Word Processor(签到题)

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const int N = 110;
char a[N][30];

int main()
{
   int n,k;cin>>n>>k;
   for(int i = 1, t = 0; i <= n; i ++)
   {
   	cin >> a[i];
   	t += strlen(a[i]);
   	if(t > k)
   	{
   		cout << endl;
   		t = strlen(a[i]);
   	}
   	else if(i!=1) cout << ' ';
   	cout << a[i];
   }
   return 0;
} 

F. Time is Mooney(DP)

题目大意

有n个点,m条边,Bessie初始是在1号点,每个点(除1号点)有一定的月光值,
来到这个点一次就会增加月光值,但是如果走了k条边就会减少c×k2的月光值,求Bessie能获得月光值的最大值

题目解析

设f[i][j]为走过j条边,最终在i号点的月光值的最大值(不算减少的),通过dp很容易得到转移方程f[i][j] = f[t]j-1,最后枚举答案就可以了

代码

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
typedef long long LL;
const int N = 1e3 +10,INF = 0x3f3f3f3f;
vector<int> g[N];
int f[1200][N],c,m,n,w[N];
int main()
{
	cin >> n >> m >> c;
	for(int i = 1; i <= n; i ++)
	cin >> w[i];
	for(int i = 1; i <= m; i ++)
	{
		int x,y;cin >> x>>y;
		g[x].push_back(y);
	}
	memset(f,-1,sizeof f);
	f[0][1] = 0;
	for(int i = 1; i <= 1100; i ++)
	{
		for(int j = 1; j <= n; j ++)
		if(f[i-1][j]!=-1)
		for(int t = 0; t < g[j].size(); t++)
		{
			int u = g[j][t];
			f[i][u] = max(f[i][u],f[i-1][j]+w[u]);  
		}
	}
	int ans = -1;
	for(int i = 0; i <= N; i ++)
		ans = max(ans,f[i][1]-i*i*c);
	cout<<ans;
	return 0;
 } 

H. Photoshoot

题目大意

给你b1、b2……bn-1,求字典序最小数列a1,a2……an,使得对于任意bi,有 bi=ai+ai+1,且a数列中每个数为1~n中的每个不同数。

题目分析

只要确定了a1,那么a数列就确定了,所以只要枚举a1即可

代码

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const int N = 1e3 +10;
int a[N],b[N],ans[N];
bool st[N];
int main()
{
	int n;cin >> n;
	for(int i = 1; i < n; i ++)
	cin >> b[i];
	for(int i = 1; i <= n; i ++)
	{
		memset(ans,0,sizeof ans);
		memset(st,0,sizeof st);
		ans[1] = i,st[i] = true;
		bool flag = true;
		for(int j = 2; j <= n; j ++)
		{
			int x = b[j-1]-ans[j-1];
			if(x<=0||st[x])
			{
				flag = false;
				break;
			}
			st[x] = true;
			ans[j] = x; 
		}
		if(flag)
		break;
	}
	for(int i = 1; i <= n; i ++)
	cout << ans[i] << ' ';
	return 0;
 } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值