跳到正文

个人记录

小蒟蒻-训练课-第六次——唐果

Har10约 5 分钟
原文
文章信息与版本

文章信息

发布
2026年10月24日
收录
2026年11月4日
更新
2026年11月15日
篇幅
约 5 分钟

版本

  1. v1当前

CF1073D Berland Fair

呜呜呜,差一步第二。
首先,建议加强数据,因为我隔壁的snowane纯暴力过了,我后面也试了,直接模拟就能过,分享下水AC代码(差一点水过)

水
C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n,t;
int a[200005];
signed main()
{
	cin >> n >> t;
	int sum=0,res=0;
	for(int i=1;i<=n;i++)
	{
		cin >> a[i];
		sum+=a[i];
	}
	while(1)
	{
		int cnt=0;//我赛时位置定到外面了,就错了,要是写这里妥妥第二
		for(int i=1;i<=n;i++)
		{
			if(t>=a[i])
			{
				t-=a[i];
				res++;
				cnt++;
			}
		}
		if(!cnt) break;
	} 
	cout << res;
	return 0;
} 

一开始,我就想,这么大的数据肯定有类似循环节的做法,不出所料,我写了,但是有点不对。我们发现,每次出现新增不能买的物品时,就会有一种新的购买方案,而这个购买方案会循环执行几次,知道再次出现新增不能买的物品时,又会有新的方案,那么,按这个数据,每有一种新的方案,我们就记录一次要多少钱,一次买了多少个,在用现在剩的钱数除以多少钱,就能算出这个方案要执行多少次,减去钱数,加上购买的物品数即可。(思路代码都很简单)

C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n,t;
int a[200005];
signed main()
{
	cin >> n >> t;
	int res=0;
	for(int i=1;i<=n;i++)
	{
		cin >> a[i];
	}
	while(1)
	{
		int cnt=0,sum=0,tmp=t;
		for(int i=1;i<=n;i++)
		{
			if(tmp>=a[i])
			{
				tmp-=a[i];
				sum+=a[i];
				cnt++;
			}
		}
		if(cnt==0) break;
		int k=t/sum;
		res+=cnt*k;
		t-=k*sum;
	} 
	cout << res;
	return 0;
} 

时间复杂度:O(n⋅logT)O(n·logT):
设当前方案买一圈的钱数为costcost。

  • 情况1:cost>T/2cost>T/2,一圈直接减半
  • 情况2:cost<=T/2cost<=T/2,则剩余钱数TT%cost<cost,也不超过一半。

CF63C Bulls and Cows

哎,神秘的枚举每一位,然后检查nn条消息是否符合,如果有多个答案,就是数据不够,没有答案就是数据错误,否则输出那个答案。 纯考验代码能力

C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n;
string s[15];
int x[15],y[15];
char a[5];
bool cc(string x,string y,int w,int v)
{
	int t[10]={0};
	for(int i=0;i<4;i++)
	{
		t[y[i]-'0']++;
	}
	int cntw=0,cntv=0;
	for(int i=0;i<4;i++)
	{
		if(x[i]==y[i]) cntw++;
		else if(t[x[i]-'0']) cntv++;
	}
	return cntw==w&&cntv==v;
}
bool check(string xx)
{
	for(int i=1;i<=n;i++)
	{
		if(!cc(xx,s[i],x[i],y[i])) return 0;
	}
	return 1;
}
signed main()
{
	cin >> n;
	for(int i=1;i<=n;i++)
	{
		cin >> s[i] >> x[i] >> y[i];
	}
	int cnt=0;
	string res="";
	for(int s1=0;s1<=9;s1++)
	{
		for(int s2=0;s2<=9;s2++)
		{
			if(s2==s1) continue;
			for(int s3=0;s3<=9;s3++)
			{
				if(s3==s2) continue;
				for(int s4=0;s4<=9;s4++)
				{
					if(s3==s4) continue;
					string xx="";
					xx+=char(s1+'0');
					xx+=char(s2+'0');
					xx+=char(s3+'0');
					xx+=char(s4+'0');
					if(check(xx))
					{
						cnt++;
						res=xx;
					}
				}
			}
		}
	}
	if(cnt==1) cout << res;
	if(cnt==0) cout << "Incorrect data";
	if(cnt>1) cout << "Need more data";
	return 0;
} 
原文