跳到正文

题解

CSP-J题解

weberzcm约 11 分钟
原文
文章信息与版本

文章信息

发布
2024年11月28日
收录
2025年12月12日
更新
2026年11月19日
篇幅
约 11 分钟
获赞
2

版本

  1. v1当前

前言

CSP-J考挂了。。。

T1赛时50(T_T),二刷100
T2赛时40,二刷100
T3赛时0(明明想到正解思路了),二刷100
T4赛时0,二刷0(。。。)

那么这次小w就先写T1~T3的题解吧(T4也会说说我的思路)

T1 扑克牌

(洛谷P11227)

题目分析

首先看到题目,发现大概就是补充几张卡片,使得你的手里拥有全部种类52张扑克牌。

那可以怎么做呢?

做法

很简单,可以写一个变种的桶排序,注意看桶排序的这行代码。

m[a[i]]++;

为了去重,可以把它换成:

m[a[i]]=1;

这样就实现了简单去重(妈妈再也不怕我忘了怎么用set辣)

具体代码实现很简单,还有一个点,开个string数组存下52张牌的“样子”会更方便哦!

代码

C++
#include<bits/stdc++.h>
#define int long long
#define N 105
#define M 100005
using namespace std;
int n,ans,vis[N];
string s[N],t[N]={"DA","D2","D3","D4","D5","D6","D7","D8","D9","DT","DJ","DQ","DK","CA","C2","C3","C4","C5","C6","C7","C8","C9","CT","CJ","CQ","CK","HA","H2","H3","H4","H5","H6","H7","H8","H9","HT","HJ","HQ","HK","SA","S2","S3","S4","S5","S6","S7","S8","S9","ST","SJ","SQ","SK"};
signed main()
{
  freopen("poker.in","r",stdin);
  freopen("poker.out","w",stdout);
  cin>>n;
  for(int i=1;i<=n;i++)
  {
  	cin>>s[i];
      for(int j=0;j<52;j++)
      {
          if(s[i]==t[j]&&vis[j]==0)
          {
              vis[j]=1;
              ans++;
          }
      }
	}
	cout<<52-ans;
  return 0;
}

T2 地图探险

(洛谷P11228)

题目分析

首先,先来分析一下题目大意:

一个地图,你有一个初始方向,遇到障碍就会向右转

问你经过k次操作后经过了几个位置

那可以怎么做呢?

做法

大模拟!

模拟k步操作,记录经过的位置,注意不要重复,可以加个“标记字符”,比如,走过一步就把那个格点的字符换成!或者别的字符。

不要标记为障碍的符号(#)!!!

还有一点,最开始那个点也要标记上,我比赛时的失分点就在这里T_T

代码很简单,细节不简单。

代码

C++
#include<bits/stdc++.h>
#define int long long
#define N 1005
#define M 100005
using namespace std;
int n,m,k,x,y,d,dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
char a[N][N];
signed main()
{
  freopen("explore.in","r",stdin);
  freopen("explore.out","w",stdout);
  int T;
  cin>>T;
  while(T--)
  {
  	cin>>n>>m>>k>>x>>y>>d;
  	for(int i=1;i<=n;i++)
  	{
  		for(int j=1;j<=m;j++)
  		{
  			cin>>a[i][j];
			}
		}
		int ans=1;
      	a[x][y]='!';
		for(int i=1;i<=k;i++)
		{
			int xx=x+dx[d],yy=y+dy[d];
			if(xx<1||xx>n||yy<1||yy>m||a[xx][yy]=='x')
			{
				d=(d+1)%4;
			}
			else
			{
				x=xx,y=yy;
				if(a[x][y]!='!')ans++;
				a[x][y]='!';
			}
		}
		cout<<ans<<"\n";
	}
  return 0;
}

T3 小木棍

PS:很巧妙的题。 (洛谷P11229)

题目分析

这道题可不是什么玄学dp,而是——

做法

特判!

首先来看看数据范围带给我们的提示:

image

是不是想到了点什么?

再看看每一个数字所对应的火柴棒数:

image

让我们重温一下小学一年级学过的数字比大小:

image

我们发现,当位数最小时,不管数字是啥,他都最小(是和位数多的进行比较,别搞错了)。

而有因为用的木棍最多,剩下的越少,数位就越少,所以看到用木棍最多的8,用了7个木棍,所以肯定是按照除以七的余数考虑,对吧?

当 n mod 7==0n \bmod 7==0 时,肯定全选8最优;

当 n mod 7==1n \bmod 7==1 时,前2个选10最优(2+6==8,8%7==1,最优,不信你试试);

当 n mod 7==2n \bmod 7==2 时,前1个选1最优;

当 n mod 7==3n \bmod 7==3 时,前3个选200最优(不要想成22888888888……会挂……)

当 n mod 7==4n \bmod 7==4 时,前2个选20最优;

当 n mod 7==5n \bmod 7==5 时,前1个选2最优(2最小);

当 n mod 7==6n \bmod 7==6 时,前2个选6最优。

注意:还有一些特殊情况,在这里不予说明。我会放在代码里。

代码

C++
#include<bits/stdc++.h>
#define int long long
#define N 15
#define M 15
using namespace std;
int n;
signed main()
{
  freopen("sticks.in","r",stdin);
  freopen("sticks.out","w",stdout);
  int T;
  cin>>T;
  while(T--)
  {
  	cin>>n;
  	if(n==1)
  	{
  		cout<<-1<<"\n";
  		continue;
		}
		if(n==2)
		{
			cout<<1<<"\n";
			continue;
		}
		if(n==3)
		{
			cout<<7<<"\n";
			continue;
		}
		if(n==4)
		{
			cout<<4<<"\n";
			continue;
		}
		if(n==5)
		{
			cout<<2<<"\n";
			continue;
		}
		if(n==6)
		{
			cout<<6<<"\n";
			continue;
		}
		if(n==8)
		{
			cout<<10<<"\n";
			continue;
		}
		if(n==10)
		{
			cout<<22<<"\n";
			continue;
		}
      if(n==17)
      {
          cout<<200<<"\n";
          continue;
      }
  	if(n%7==0)
  	{
  		for(int i=1;i<=n/7;i++)
  		{
  			cout<<8;
			}
			cout<<"\n";
		}
		else if(n%7==1)
		{
			cout<<10;
			for(int i=1;i<=n/7-1;i++)
			{
				cout<<8;
			}
			cout<<"\n";
		}
		else if(n%7==2)
		{
			cout<<1;
			for(int i=1;i<=n/7;i++)
			{
				cout<<8;
			}
			cout<<"\n";
		}
		else if(n%7==3)
		{
			cout<<200;
			for(int i=1;i<=n/7-2;i++)
			{
				cout<<8;
			}
			cout<<"\n";
		}
		else if(n%7==4)
		{
			cout<<20;
			for(int i=1;i<=n/7-1;i++)
			{
				cout<<8;
			}
			cout<<"\n";
		}
		else if(n%7==5)
		{
			cout<<2;
			for(int i=1;i<=n/7;i++)
			{
				cout<<8;
			}
			cout<<"\n";
		}
		else if(n%7==6)
		{
			cout<<6;
			for(int i=1;i<=n/7;i++)
			{
				cout<<8;
			}
			cout<<"\n";
		}
	}
  return 0;
}

T4 接龙(有思路,0分)

(洛谷P11230)

题目分析

这道题目是一道模拟!

有的人就说了,啊小w小w,题解大佬都用的图论dp,你这种思路肯定不行。

这么说吧,我教练用这种思路A了……

首先,预处理出接1~q之间接龙的各种状态。放心,数组能存的下。

把每一局看成一层,根据上一层的转换后台进行操作。

具体实现放代码里了。有大佬可以帮忙调一下吗?

C++
#include<bits/stdc++.h>
#define int long long
#define N 200005
#define M 105
using namespace std;
int n,k,q,l[N],a[M][N],r,c;
vector<int>s[N];
signed main()
{
	int T;
	cin>>T;
	while(T--)
	{
		cin>>n>>k>>q;
		for(int i=1;i<=n;i++)
		{
			cin>>l[i];
			s[i].clear();
			s[i].push_back(374972349);
			for(int j=1;j<=l[i];j++)
			{
				int shshs;
				cin>>shshs;
				s[i].push_back(shshs);//存入数据
			}
		}
		a[0][1]=-1;
		for(int i=1;i<=100;i++)
		{
			for(int j=1;j<=n;j++)
			{
				int le=0;
				//cout<<"It's j is "<<j<<endl;
				for(int kk=1;kk<=l[j];kk++)
				{
					int t_t=s[j][kk];
					if(le)
					{
						le--;
						if(a[i][t_t]==0)
						{
							a[i][t_t]=j;//接龙
						}
						else if(a[i][t_t]!=j)
						{
							a[i][t_t]=-1;//变成一个可以接很多次的“完美数段”
						}
					}
					if(a[i-1][t_t]!=0)
					{
						if(a[i][t_t]==j)continue;
					  le=k-1;
					}
				 	//cout<<a[i][t_t]<<" ";
				}
				//cout<<endl;
			}
		}
		for(int i=1;i<=q;i++)//按顺序输出
		{
			cin>>r>>c;
			if(a[r][c]!=0)
			{
				cout<<1<<endl;
			}
			else
			{
				cout<<0<<endl;
			}
		}
	}
	return 0;	
}
原文
存档评论1 条抓取于