【补题】Codeforces 1065: Educational Round 52 (Rated for Div. 2) ABCD

本文分享了竞赛编程中常见的问题解决技巧,包括认真读题、数学问题解析、图论构造、贪心算法应用、状态编码与动态规划等,通过具体题目讲解如何高效解题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我是智障吗?2e5的数据范围看成1e9然后卡了一个半小时???

总结

  1. 认真读题,做题有困难时再读一遍题,注意各个常数是否读错。

A. Vasya and Chocolate 数学

饼干单价c元,买a赠b,现在有n元,问最多可以获得多少块饼干。

先降维(减少变量数),最多买p=n/c块饼干,所以最多获得p/a*(a+b)+p%a块。

B. Vasya and Isolated Vertices 图论,构造

定义孤点为度数为0的点,现在给定点数n与边数m,要求构造一个简单无向图,求最多与最少的孤点数。

如果要求孤点最少,把每条边都连接两个孤点。答案是max(n-m*2,0)
如果要求孤点最多,尽量去连完全图。答案是n-k,其中k表示m条边最少使用的点数。

ll n = read(), m=read();
ll k=0; for(;k*(k-1)/2<m;++k); //m条边最少使用的点数
printf("%I64d %I64d\n",max(n-2*m,0ll),max(0,n-k) );

C. Make It Equal 贪心

题意不想说了,难受。

记录原位置,逐层试探。

/* LittleFall : Hello! */
#include <bits/stdc++.h>
using namespace std; using ll = long long; inline int read();
const int M = 500016, MOD = 1000000007;

int save[M];
int main(void)
{
	#ifdef _LITTLEFALL_
	freopen("in.txt","r",stdin);
    #endif

	int n = read(), k = read();
	int low = INT_MAX, hei = INT_MIN;
	for(int i=1;i<=n;++i)
	{
		int v = read();
		++save[v];
		low = min(low,v);
		hei = max(hei,v);
	}
	int ans = 0;
	int pp = 0; //已被切的数量
	while(hei!=low)
	{
		int nh = hei;
		int s=0;
		while(nh > low && s+(pp+save[nh])<=k)
			s+=pp+=save[nh--];
		//slice
		++ans;
		hei = nh;
	}
	printf("%d\n",ans );

    return 0;
}


inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

D. Three Pieces 建图,编码

有一个n*n(10)的棋盘,每个格子都标着一个数字,这些数字是1到n*n的排列。你有三个国际象棋棋子:车、马、象,初始时选择一个棋子放在标1的格子里,要依次经过2,3,4,…n*n,每个格子可以走若干次。每走完一步可以更换棋子,更换棋子也算作一步。输出最少的步数以及在最少步数下最少的更换棋子次数。

一道编码题(?)
因为最多100个格子,3种棋子,只有300个状态,完全可以把任意两个状态之间需要的步数与更换次数预处理出来。
状态编码st=(x−1)∗3∗n+(y−1)∗3+rolst = (x-1)*3*n+(y-1)*3+rolst=(x1)3n+(y1)3+rol,距离编码step∗1000+changestep*1000+changestep1000+change
然后floyd即可。

全部预处理出来后使用dp即可求出最优解。

写这个题的时候心态爆炸,变量名写错愣是没查出来。

/* LittleFall : Hello! */
#include <bits/stdc++.h>
using namespace std; using ll = long long; inline int read();
const int M = 500016, MOD = 1000000007;

int n;
const int hgo[8][2] = {{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1},{-2,1},{-1,2}};
ll dis[324][324]; 
//状态编码:(x-1)*3*n+(y-1)*3+rol, rol 0车1马2象
//距离编码:step*1000+change 
inline int ms(int x,int y,int rol)
{
	return (x-1)*3*n+(y-1)*3+rol;
}
ll dp[128][3];
int ch[128];
int main(void)
{
	#ifdef _LITTLEFALL_
	freopen("in.txt","r",stdin);
    #endif

	memset(dis,0x3f,sizeof(dis));
	n = read();
	int ast = n*n*3; //总点数
	//建图
	for(int pnt=0;pnt<ast;++pnt)
	{
		int rol = pnt%3, x = pnt/3/n+1, y = pnt/3%n+1;
		if(rol==0)
		{
			for(int nx=1;nx<=n;++nx)
				dis[pnt][ms(nx,y,rol)] = 1000;
			for(int ny=1;ny<=n;++ny)
				dis[pnt][ms(x,ny,rol)] = 1000;
		}
		else if(rol==1)
		{
			for(int k=0;k<8;++k)
			{
				int nx = x + hgo[k][0], ny = y + hgo[k][1];
				if(nx>=1&&nx<=n&&ny>=1&&ny<=n)
					dis[pnt][ms(nx,ny,rol)] = 1000;
			}
		}
		else if(rol==2)
		{
			int sum = x+y, sub = x-y;
			for(int nx=1;nx<=n;++nx)
			{
				int ny = sum-nx;
				if(ny>=1&&ny<=n)
					dis[pnt][ms(nx,ny,rol)] = 1000;
				ny = nx-sub;
				if(ny>=1&&ny<=n)
					dis[pnt][ms(nx,ny,rol)] = 1000;
			}
		}
		for(int nr=0;nr<3;++nr)
			dis[pnt][ms(x,y,nr)] = 1001;
		dis[pnt][pnt] = 0;
	}
	//floyd
	for(int k=0;k<ast;++k)
		for(int i=0;i<ast;++i)
			for(int j=0;j<ast;++j)
				dis[i][j] = min(dis[i][j],dis[i][k]+dis[k][j]);

	for(int i=1;i<=n;++i)
		for(int j=1;j<=n;++j)
			ch[read()] = (i-1)*n+j-1;

	memset(dp,0x3f,sizeof(dp));
	dp[ch[1]][0]=dp[ch[1]][1]=dp[ch[1]][2]=0;

	ll ans = LLONG_MAX;
	for(int i=2;i<=n*n;++i)
	{
		for(int rol=0;rol<3;++rol)
		{
			for(int lr=0;lr<3;++lr)
			{
				dp[ch[i]][rol] = min(dp[ch[i]][rol],dp[ch[i-1]][lr]+dis[ch[i]*3+rol][ch[i-1]*3+lr]);
				if(i==n*n)
					ans = min(ans,dp[ch[i]][rol]);
			}
		}
	}
	printf("%I64d %I64d\n",ans/1000,ans%1000 );
    return 0;
}


inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值