Codeforces Round #797 (Div. 3)

A

Print a Pedestal (Codeforces logo?)

题意:给定n,让你输出h1,h2,h3,其中h1+h2+h3=n且0<h3<h2<h1,输出h1最小的三元组。

思路:让h1,h2,h3三个数尽量接近,根据n%3的情况分类讨论即可。

#include <bits/stdc++.h>
#define lowbit(x) x&(-x)
#define ios std::ios::sync_with_stdio(false);cin.tie(0),cout.tie(0)
#define PII pair<int,int>
typedef long long ll;
const int N=1e6+10;
const int inf=0x3f3f3f3f;

using namespace std;
int n;
/*
0
h h+1 h-1
1
h h+2 h-1;
2
h+1 h+2 h-1
*/
void solve()
{
	cin>>n;
	int h=n/3;
	if(n%3==0)
	{
		cout<<h<<' '<<h+1<<' '<<h-1<<'\n';
	}
	else if(n%3==1)
	{
		cout<<h<<' '<<h+2<<' '<<h-1<<'\n';
	}
	else cout<<h+1<<' '<<h+2<<' '<<h-1<<'\n';
}
int main()
{
	//ios;
	int _t=1;
	cin>>_t;
	while(_t--) solve();
	system("pause");
	return 0;
}

 B

Array Decrements

题意:给定长度为n的数组a,b,每次操作可以将数组a中非0元素全部减一,问能否经过操作使得a数组等于b数组。 

思路:遍历数组,找到max(a[i]-b[i]),对a数组每个数都进行这么多次操作,再看a,b是否相等即可。

#include <bits/stdc++.h>
#define lowbit(x) x&(-x)
#define ios std::ios::sync_with_stdio(false);cin.tie(0),cout.tie(0)
#define PII pair<int,int>
typedef long long ll;
const int N=5e4+10;
const int inf=0x3f3f3f3f;

using namespace std;
int n;
int a[N],b[N];
void solve()
{
	cin>>n;
	for(int i=0;i<n;i++) cin>>a[i];
	for(int i=0;i<n;i++) cin>>b[i];

	int ma=-inf,t=0;
	for(int i=0;i<n;i++)
	{
		if(a[i]>ma) 
		{
			ma=a[i];
			t=a[i]-b[i];
		}
	}
	if(t<0) 
	{
		puts("NO");
		return ;
	}
	for(int i=0;i<n;i++)
	{
		a[i]=max(a[i]-t,0);
	}
	bool flag=1;
	for(int i=0;i<n;i++)
	{
		if(a[i]!=b[i])
		{
			puts("NO");
			return ;
		}
	}
	puts("YES");
}
int main()
{
	ios;
	int _t=1;
	cin>>_t;
	while(_t--) solve();
	system("pause");
	return 0;
}

C

Restoring the Duration of Tasks

题意:给定n个进程的到达时间和离开时间,问每个进程运行了多长时间。一个进程可以运行当且仅当它之前的进程都已离开。

思路: 模拟

#include <bits/stdc++.h>
#define lowbit(x) x&(-x)
#define ios std::ios::sync_with_stdio(false);cin.tie(0),cout.tie(0)
#define PII pair<int,int>
typedef long long ll;
const int N=2e5+10;
const int inf=0x3f3f3f3f;

using namespace std;
int n;
int s[N],f[N];
int ans[N];
void solve()
{
	cin>>n;
	for(int i=1;i<=n;i++) cin>>s[i];
	for(int j=1;j<=n;j++) cin>>f[j];

	for(int i=1;i<=n;i++)
	{
		ans[i]=f[i]-max(s[i],f[i-1]);
	}
	for(int i=1;i<=n;i++) cout<<ans[i]<<" \n"[i==n];
}
int main()
{
	ios;
	int _t=1;
	cin>>_t;
	while(_t--) solve();
	system("pause");
	return 0;
}

D

Black and White Stripe

题意:给定长度为n的字符串s,其中只含W和B,每次操作可以将W变成B,问长度为k的子串全为B最少需要多少次操作。 

思路:前缀和,sum[i]表示[1,i]中有多少B。枚举子串起点,统计最小值即可

#include <bits/stdc++.h>
#define lowbit(x) x&(-x)
#define ios std::ios::sync_with_stdio(false);cin.tie(0),cout.tie(0)
#define PII pair<int,int>
typedef long long ll;
const int N=2e5+10;
const int inf=0x3f3f3f3f;

using namespace std;
int n,k;
char s[N];
int sum[N];
void solve()
{
	cin>>n>>k;
	scanf("%s",s+1);
	for(int i=1;i<=n;i++)
	{
		sum[i]=sum[i-1]+(s[i]=='B');
	}
	int ans=inf;
	for(int i=1;i+k-1<=n;i++)
	{
		ans=min(ans,k-(sum[i+k-1]-sum[i-1]));
	}
	cout<<ans<<'\n';
}
int main()
{
	//ios;
	int _t=1;
	cin>>_t;
	while(_t--) solve();
	system("pause");
	return 0;
}

E

Price Maximization

题意:给定长度为n的数组和一个k,其中n为偶数,将数组分为n/2份,每份两个元素,贡献为(ai+aj)/k,求最大贡献。

思路:对于每个数a[i]对答案贡献至少为a[i]/k,再令a[i]=a[i]%k,这样我们得到一个元素大小在[1,k-1]之间的数组,从大到小排序,贪心找到a[i]+a[j]>=k,使用双指针,统计对答案的贡献。

#include <bits/stdc++.h>
#define lowbit(x) x&(-x)
#define ios std::ios::sync_with_stdio(false);cin.tie(0),cout.tie(0)
#define PII pair<int,int>
typedef long long ll;
const int N=2e5+10;
const int inf=0x3f3f3f3f;

using namespace std;
int n,k;
int a[N];
void solve()
{
	cin>>n>>k;
	ll ans=0;
	for(int i=1;i<=n;i++)
	{
		int x;
		cin>>x;
		ans+=x/k;
		a[i]=x%k;
	}
	sort(a+1,a+n+1,[](int x,int y){
		return x>y;
	});
	for(int i=1,j=n;i<j;i++,j--)
	{
		while(a[i]+a[j]<k&&i<j) j--;
		if(i==j) break;
		ans++;
	}
	cout<<ans<<'\n';
}
int main()
{
	//ios;
	int _t=1;
	cin>>_t;
	while(_t--) solve();
	system("pause");
	return 0;
}

F

Shifting String

题意:给定长度为n的字符串和数组a,每次操作将当前字符串第a[i]位上的字符放到第i位置上。问最少需要多少次操作还原字符串。

思路:每个位置上可变换成的字符构成一个环,找到所有环长度的最小公倍数即可。 

#include <bits/stdc++.h>
#define lowbit(x) x&(-x)
#define ios std::ios::sync_with_stdio(false);cin.tie(0),cout.tie(0)
#define PII pair<int,int>
typedef long long ll;
const int N=210;
const int inf=0x3f3f3f3f;

using namespace std;
int n;
char s[N];
int a[N];
bool vis[N];
ll fun(string s)
{
	int len=s.length();
	for(int i=1;i<=len;i++)
	{
		bool flag=1;
		for(int j=0;j<len;j++)
		{
			if(s[j]!=s[(j+i)%len])
			{
				flag=0;
				break;
			}
		}
		if(flag) return i;
	}
}
void solve()
{
	cin>>n;
	scanf("%s",s+1);
	map<int,int>mp;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		vis[i]=false;
		mp[a[i]]=i;
	}
	ll ans=1;
	for(int i=1;i<=n;i++)
	{
		if(vis[i]) continue;
		vis[i]=true;
		string tem;
		tem+=s[i];
		int idx=i;
		while(mp[idx]!=i)
		{
			idx=mp[idx];
			tem+=s[idx];
			vis[idx]=1;
		}
		ll t=fun(tem);
		ans=lcm(ans,t);
	}
	cout<<ans<<'\n';
}
int main()
{
	//ios;
	int _t=1;
	cin>>_t;
	while(_t--) solve();
	system("pause");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值