Codeforces Round #568 (Div. 2) 题解(A,B,C)

Codeforces Round #568 (Div. 2) 题解

熬夜打比赛真有趣

竟然上分了真有趣

Your room: 233

有可能更D

A.

题意:有三个人分别在数轴上\(a,b,c\)的位置,每秒能使其中一个移动1个单位长度(可以移动到负数位置上),求他们两两之间距离至少为\(d\)的时间。

\(1 \le a,b,c,d \le 10^9\)

分析:直接分类讨论,显然挪左右两个才是最好的

#include<bits/stdc++.h>
using namespace std;
int a[10],d;
int main()
{
	cin>>a[1]>>a[2]>>a[3]>>d;
	sort(a+1,a+4);
	if (a[2]-a[1]<d)
	{
		if (a[3]-a[2]<d) 
		{
			cout<<d-(a[3]-a[2]) + d-(a[2]-a[1]);
		}
		else
		cout<<d-(a[2]-a[1]);
	}
	else
	{
		if (a[3]-a[2]<d)
		{
			cout<<d-(a[3]-a[2]);
		}
		else cout<<0;
	}
	return 0;
}

B

题意:有两个字符串。求一个字符串删掉某些重复字母后是否成为另一个字符串。

分析:直接模拟即可,注意重复字母的情况。

#include<bits/stdc++.h>
using namespace std;
int n,len,len1;string st,st1;
bool check()
{
	int js=0,ks=0;
	for (int j=0,k=0;j<len||k<len1;j++)
	{
			js=0;ks=0;
			if (st[j]!=st1[k]||(j==len&&k!=len1)||(j!=len&&k==len1))
			{
				return false;
			}
			while (st1[k]==st1[k+1]&&k<len1-1) k++,ks++;
			while (st[j]==st[j+1]&&j<len-1) j++,js++;
			if (js>ks) return false;
			k++;
	}
	return true;
}
int main()
{
	cin>>n;
	for (int i=1;i<=n;i++)
	{
		cin>>st;
		cin>>st1;
		len=st.size(),len1=st1.size();
		if (check()) cout<<"YES\n";else cout<<"NO\n";
	}
	return 0;
}

C

题意:有\(n​\)个人考试,考试时间\(m \min​\) ,每个人可以放弃考试或用\(t_i​\)分钟完成考试,求钦定第\(i(1\le i\le n)​\)个人完成考试时,需要多少人放弃考试。

请注意:假设某一个人在第\(j(1\le j < i)\)个人的方案中放弃了,并不代表在第\(i​\)个人的方案中必须放弃

\(t_i \le m​\)

C1

\(1 \le n,m \le 100\)

考虑贪心,对于\(a_i>a_j\),显然删\(a_i\)更优,因为这空出的时间会多一些,使得答案不会更劣

sol 0

用堆维护\(t_i\),当时间不足以让第\(i​\)个人考试时,删除最大的。

然后\(\ wa \ on \ pretest \ 13\)

原因:没有考虑上面加粗字体

sol 1

处理每一个人的情况时,只需使前面的排好序,再从大往小删除就行了。

时间复杂度大约\(O(N*NlogN)\)

#include<bits/stdc++.h>
using namespace std;
int a[300],cnt,n,m,s,sum;
int main()
{
	cin>>n>>m;
	for (int i=1;i<=n;i++)
	{
		cin>>a[i];
		cnt=0;sum=s;
		sort(a+1,a+i);
		for (int j=i-1;j>0;j--)
		{
			if (sum+a[i]<=m) break;
		    sum-=a[j];
		    cnt++;
		}
		s+=a[i];
		cout<<cnt<<" ";
	}
	return 0;
}

C2

\(1 \le n \le 2 \cdot 10^5,1 \le m \le 2 \cdot 10^7\)

\(1 \le t_i \le 100\)

用C1的方法是行不通的。

但如此显眼的\(t_i\)很容易让人想到数组计数。(大概也能叫桶排?)

按上面贪心方法,从\(100\)\(1\)枚举删除即可。

代码

#include<bits/stdc++.h>
using namespace std;
int n,m,x,a[300],s,cnt,sum;
int main()
{
	cin>>n>>m;
	for (int i=1;i<=n;i++)
	{
		cin>>x;
		int s=sum;cnt=0;
		if (s+x<=m){sum+=x;a[x]++;cout<<"0 ";continue;}
		for (int j=100;j>0;j--)
		{
			if (s-a[j]*j+x<=m)
			{
				cnt+=(s+x-m)/j + (int(bool((s+x-m)%j)));
				break;
			}
			else 
			{
				cnt+=a[j];
				s-=a[j]*j;
			}
		}
		sum+=x;
		a[x]++;
		cout<<cnt<<" ";
	}
	return 0;
}
posted @ 2019-06-20 23:42  fmj_123  阅读(361)  评论(0编辑  收藏  举报