跳到正文

题解

Test47普转提考试总结

Claire0630约 29 分钟
原文

这是 2025年11月5日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2025年11月3日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 29 分钟

版本

  1. v2当前
  2. v1

Test47普转提考试总结

Data

Score:190 = 100 + 0 + 60 + 10 + 20

Rank: (Data damaged and lost)

Preface

好吧好吧,状态挺差的

我也没啥好说的

T1直接过

T2贪心我是真贪不动啊(感觉需要求救了,我的贪心烂成一坨了,给我100块钱我都不要的感觉)

T3基本是原题,但还是没A掉

T4太猎奇的大模拟DP了

T5是真的看不出来图论

好吧好吧

Statements

A. 十字斩

题目描述

小 Z 是某大学新生,在迎新晚会上,老师向大家介绍了十字斩活动。

活动场地可以视为一个具有坐标轴的无限平面直角坐标系。坐标系中有许多气球,气球的坐标可以用两整数对刻画,而学生们的目标是用十字斩来破坏最多的气球。

十字斩可以形象的刻画为相互垂直的两条直线,并且每条直线与坐标轴的夹角均为 45°。使用十字斩后,两条直线上的气球均被破坏。

小 Z 想要赢得这场活动的胜利,走向大学人生巅峰。于是他选了若干个位置,想问你如果他站在这些位置释放十字斩,他能破坏多少气球。

输入格式

第一行两个正整数 nn 和 mm,代表气球数量和询问数量。

接下来 nn 行,每行两个整数,代表气球坐标,保证坐标两两不同。

接下来 mm 行,每行两个整数,代表询问坐标。

输出格式

mm 行,每行一个整数,代表可以破坏的气球数量。

输入输出样例

输入:

代码
4 2  
0 0  
2 2  
0 2  
2 0  
0 1  
1 1  

输出:

代码
4  
0  

Solution

我们观察两条对角线的性质,发现有y+x=ky + x = k或y−x=ky - x = k

然后我们就用map存一下就好了

Code:

C++
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef __int128 SLL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
template <typename T> inline void read(T &x){
	x = 0;
	LL f = 1;
	char c = getchar();
	for(; !isdigit(c); c=getchar()) if(c == '-') f = -1;
	for(; isdigit(c); c=getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
	x *= f;
}
template <typename T> inline void print(T x){
	if(x < 0) x = -x,putchar('-');
	if(x > 9) print(x / 10);
	putchar(x % 10 + 48);
}
const int N = 1e5 + 11;
LL n,m;
map<LL,LL> mp_up,mp_down;
map<PLL,bool> est;
int main(){
//	freopen("cross.in","r",stdin);
//	freopen("cross.out","w",stdout);
	read(n);read(m);
	for(LL i=1;i<=n;i++){
		LL x,y;
		read(x);read(y);
		mp_up[y - x] ++;
		mp_down[y + x] ++;
		est[{x,y}] = 1;
	}
	while(m --){
		LL x,y;
		read(x);read(y);
		print(mp_up[y - x] + mp_down[y + x] - (est[{x,y}] == 1 ? 1 : 0));
		putchar('\n');
	}
	return 0;
}

B. 大富翁

题目描述

新生们为了促进感情,准备去玩桌游,他们选择了大富翁游戏。大富翁的胜利目标是为了收取更多的租金,获得更多的钱。

地图上有 kk 栋房屋,一开始并不会产出租金。每栋房屋都可以被“购买”一次,也可以被“装修”一次(需要在购买之后)。如果一栋房屋被“购买”,则这栋房屋每天可以产生一个金币的租金,如果一栋房屋被“购买”且“装修”,则这栋房屋每天可以产生两个金币的租金。

购买房屋和装修房屋都需要时间,第 ii 栋房屋被购买需要 tit_i 时间,被装修需要 rir_i 时间。一个人只能在同一时间进行一个操作。

输入格式

第一行两个整数 kk 和 TT,代表房屋数量和时间总长度。

接下来 kk 行,每行两个整数 tit_i 和 rir_i,代表第 ii 房屋的购买和装修时间。

输出格式

输出一行一个整数,代表小 Z 能获得的最大金币数。

输入输出样例

输入:

代码
2 10000000000  
2 1  
1 2  

输出:

代码
399999999861  

Solution

我们使用贪心思想

我们发现,其实对于每个房屋都只有两种选择,一种是建完之后立马装修,或者是建完之后就摆在那里先不装修

我们又需要在更少的时间内将金币数达到最大才可取得最大收益

因此我们整个流程的时间顺序一定是从小到大的

我们使用优先队列动态处理这个问题

定义优先级为priority=min(ai,0×2,ai,0+ai,1)priority = min(a_{i,0} \times 2,a_{i,0} + a_{i,1})

前者表示先买房但不着急装修,后者表示买完房立刻装修,考虑这两段时间的最小值,才是我们上面需要的最大收益

然后按优先级从小到大处理,在中途计算贡献即可

C++
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
template <typename T> inline void read(T &x){
	x = 0;
	LL f = 1;
	char c = getchar();
	for(; !isdigit(c); c=getchar()) if(c == '-') f = -1;
	for(; isdigit(c); c=getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
	x *= f;
}
template <typename T> inline void print(T x){
	if(x < 0) x = -x,putchar('-');
	if(x > 9) print(x / 10);
	putchar(x % 10 + 48);
}
inline void sprint(const string s){
	for(auto x : s) putchar(x);
	return;
}
const int N = 6e5 + 11,M = 20;
LL n,m,cur,tot,ans;
__attribute__((always_inline)) void add(LL tim){
	ans += tim * tot;
	cur += tim;
	tot ++;
	return;
}
int main(){
    //	freopen("rich.in","r",stdin);
	//	freopen("rich.out","w",stdout);
	auto cmp = [](const PLL &x,const PLL &y){
		LL tmp1 = min(x.first * 2,x.first + x.second);
		LL tmp2 = min(y.first * 2,y.first + y.second);
		if(tmp1 > tmp2) return 1;
		return 0;
	};
	priority_queue<PLL,vector<PLL>,decltype(cmp)> pq(cmp);
	read(n);read(m);
    for(LL i=1;i<=n;i++){
    	LL x,y;
    	read(x);read(y);
    	pq.push((PLL){x,y});
	}
	while(pq.size()){
		PLL t = pq.top();
		pq.pop();
		add(t.first);
		if(t.second != 1e18) pq.push((PLL){t.second,1e18});
	}
	if(cur < m) ans += (m - cur) * tot;
    print(ans);
    return 0;
}

C. 联谊舞会

题目描述

一年一度的联谊舞会开始了,每位学生都能交到自己心仪的朋友。

为了吸引同学参加,主办方在今年想出了交友的新办法:主办方在舞厅中间画出了一个分界线,分界线的上方和下方都有一些从左往右的“站点”,每位同学都需要从一个分界线上方的“站点”跑到一个分界线下方的“站点”,如果两个同学的跑步路线相交,那么他们便交友成功。

输入格式

第一行一个正整数 nn,代表同学个数。

接下来一行 nn 个数,第 ii 个数为 aia_i,代表第 ii 个出发站点所对应的同学。

接下来一行 nn 个数,第 ii 个数为 bib_i,代表第 ii 个结束站点所对应的同学。

输出格式

输出一个整数,代表最大朋友圈的大小。

输入输出样例

代码
3  
1 2 3  
3 2 1  

输出:

代码
3  

Solution

我们发现这道题其实和远古时期的CWOI上一道题很像

其实确实差不多

只不过那个是尽量不交叉,这个是尽量多交叉

注意,此题需要你在O(n×log2n)O(n\times log_2n)的时间复杂度内通过

O(n2)O(n^2)并不可

我们简单回顾一下

设dpidp_i表示长度为ii的最长上升子序列的最小结尾值

这步状态定义用到了贪心,因为只有结尾值尽可能小才会尽可能让最长上升子序列尽可能长

然后枚举数组aa,如果aia_i比你当前的dplendp_{len}小,就直接接到结尾dplen+1=aidp_{len + 1} = a_i

否则我们二分查找到小于lenlen的最大的合适的lenlen,将aia_i接到末尾dpnlen+1=aidp_{nlen + 1} = a_i

复杂度为O(n×log2n)O(n \times log_2n)

Code:

C++
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef __int128 SLL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
template <typename T> inline void read(T &x){
	x = 0;
	LL f = 1;
	char c = getchar();
	for(; !isdigit(c); c=getchar()) if(c == '-') f = -1;
	for(; isdigit(c); c=getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
	x *= f;
}
template <typename T> inline void print(T x){
	if(x < 0) x = -x,putchar('-');
	if(x > 9) print(x / 10);
	putchar(x % 10 + 48);
}
const int N = 1e5 + 11;
struct node{
	int s,t,id;
}g[N];
int dp[N];
int n;
unordered_map<int,int> mp;
int main(){
    //	freopen("dance.in","r",stdin);
	//	freopen("dance.out","w",stdout);
	read(n);
	for(int i=1;i<=n;i++){
		read(g[i].s);
		mp[g[i].s] = i;
	}
	for(int i=1;i<=n;i++){
		read(g[i].t);
		g[i].id = mp[g[i].t];
	}
	int cur = 0;
	dp[0] = 0x3f3f3f3f;
	for(int i=1;i<=n;i++){
		if(g[i].id < dp[cur]){
			cur ++;
			dp[cur] = g[i].id;
		}else{
			int l = 0,r = n,ans = 0;
			while(l <= r){
				int mid = l + r >> 1;
				if(dp[mid] > g[i].id){
					ans = mid;
					l = mid + 1;
				}else r = mid - 1;
			}		
			dp[ans + 1] = g[i].id;
		}
	}
	print(cur);
}

D. 唬牌游戏

题目描述

近些天,一款名为唬牌的游戏风靡全校,游戏规则如下:

所需游戏道具是若干张扑克牌,此时每张牌都具有一个花色。

一共两名玩家参赛,游戏共两局。在第一个回合中,玩家 A 将若干张相同花色的扑克牌扣置(此时玩家 B 不知道玩家 A 扣置的是什么花色),并声称一种花色,玩家 B 则需要猜测玩家 A 所扣置的牌是否与其声称花色相同,猜对则 B 得一分。

输入格式

第一行四个整数,代表扑克牌中方片、草花、红桃、黑桃牌的张数。

第二行输入一个整数 qq,表示查询数量。

接下来 qq 行,每行一个字符,表示声称的花色;一个数字,表示扣置牌数;一个字符 T 或 F,表示扣置牌与小 A 声称牌花色是否一致。

输出格式

输出 qq 行,每行一个字符串,表示如果小 A 这么声称,你的回答是什么。一定正确输出 T,一定错误输出 F,你也不清楚就输出 NG。特别的,如果小 A 根本不可能进行这样的声称,输出 ERROR。

输入输出样例1

输入:

代码
1 1 1 1  
2  
D 1 T  
D 1 F  

输出:

代码
F  
NG  
ERROR  

Solution

其实可以直接大模拟一波

只要你愿意

但其实我们是需要一个dp数组来表示当前状态是否可用

我们设dpi,j,k,l,qdp_{i,j,k,l,q}表示在前ii轮,有j,k,l,qj,k,l,q张方片、草花、红桃、黑桃牌是否成立

我们在进行前nn轮回合时直接标记

  • 如果当前情况为T

    • 如果是xx张方片,我们直接

      • dpi,j,k,l,q∣=dpi−1,j−x,k,l,qdp_{i,j,k,l,q} |= dp_{i - 1,j - x,k,l,q}
    • 以此类推…

  • 如果当前情况为F

    • 如果是xx张方片,我们直接

      • dpi,j,k,l,q∣=dpi−1,j,k−x,l,qdp_{i,j,k,l,q} |= dp_{i - 1,j,k - x,l,q}
      • dpi,j,k,l,q∣=dpi−1,j,k,l−x,qdp_{i,j,k,l,q} |= dp_{i - 1,j,k,l - x,q}
      • dpi,j,k,l,q∣=dpi−1,j,k,l,q−xdp_{i,j,k,l,q} |= dp_{i - 1,j,k,l,q - x}
    • 以此类推…

然后我们就根据qq个询问查询dp数组进行回复

我们在查询时枚举每一类牌还剩余的张数,在保证这种情况合法的前提下(如果一直不合法就是ERROR)

我们尝试给出这xx张牌,如果给出之后还剩余就不可能是F

在确定只有这一种牌给出之后还有剩余才是T

不然就是NG

我们就分讨完成

Tips:

1.注意内存不够,我们需要滚动数组优化

2.在枚举时不需要枚举四种牌,三种就够了,因为我们枚举给出的牌数量是固定的,可以枚举后计算最后一种牌的数量

Code:

C++
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef __int128 SLL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
template <typename T> inline void read(T &x){
	x = 0;
	LL f = 1;
	char c = getchar();
	for(; !isdigit(c); c=getchar()) if(c == '-') f = -1;
	for(; isdigit(c); c=getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
	x *= f;
}
template <typename T> inline void print(T x){
	if(x < 0) x = -x,putchar('-');
	if(x > 9) print(x / 10);
	putchar(x % 10 + 48);
}
inline void sprint(const string s){
	for(auto x : s) putchar(x);
}
const int N = 85;
int dmx,cmx,hmx,smx,n,m,sum;
int dp[2][N][N][N][N];
int main(){
//	freopen("game.in","r",stdin);
//	freopen("game.out","w",stdout);
	read(dmx);read(cmx);read(hmx);read(smx);
	read(n);
	dp[0][0][0][0][0] = 1;
	for(int i=1;i<=n;i++){
		char c,idn;
		int x;
		cin>>c;
		read(x);
		cin>>idn;
		sum += x;
		for(int j=0;j<=dmx;j++){
	        for(int k=0;k<=cmx;k++){
	            for(int l=0;l<=hmx;l++){
	                int q = sum - j - k - l;
	                if(q < 0 || q > smx) continue;
	                dp[i & 1][j][k][l][q] = 0; 
	            }
	        }
	    }
		for(int j=0;j<=dmx;j++){
			for(int k=0;k<=cmx;k++){
				for(int l=0;l<=hmx;l++){
					int q = sum - j - k - l;
					if(q < 0 || q > smx) continue;
					if(((idn == 'T' && c == 'D') || (idn == 'F' && c != 'D')) && j >= x) dp[i & 1][j][k][l][q] |= dp[(i & 1) ^ 1][j - x][k][l][q];
					if(((idn == 'T' && c == 'C') || (idn == 'F' && c != 'C')) && k >= x) dp[i & 1][j][k][l][q] |= dp[(i & 1) ^ 1][j][k - x][l][q];
					if(((idn == 'T' && c == 'H') || (idn == 'F' && c != 'H')) && l >= x) dp[i & 1][j][k][l][q] |= dp[(i & 1) ^ 1][j][k][l - x][q];
					if(((idn == 'T' && c == 'S') || (idn == 'F' && c != 'S')) && q >= x) dp[i & 1][j][k][l][q] |= dp[(i & 1) ^ 1][j][k][l][q - x];
				}
			}
		}
	}
	read(m);
	while(m --){
	    char c;
	    int x;
	    cin>>c;
	    read(x);
	    bool malf = 1,fls = 1,tru = 1,ucr = 0;
	    for(int i=0;i<=dmx;i++){
	        for(int j=0;j<=cmx;j++){
	            for(int k=0;k<=hmx;k++){
	                int l = sum - i - j - k;
	                if(l < 0 || l > smx) continue;
	                if((dmx - x) < i && (cmx - x) < j && (hmx - x) < k && (smx - x) < l) continue;
	                if(dp[n & 1][i][j][k][l] == 1){
	                    malf = 0;
	                    if(c == 'D'){
	                        if((dmx - x) >= i) fls = 0;  
	                        if((dmx - x) >= i && (cmx - x) < j && (hmx - x) < k && (smx - x) < l) ucr = 1;
	                        else tru = 0;
	                    }
	                    else if(c == 'C'){
	                        if((cmx - x) >= j) fls = 0;
	                        if((dmx - x) < i && (cmx - x) >= j && (hmx - x) < k && (smx - x) < l) ucr = 1;
	                        else tru = 0;
	                    }
	                    else if(c == 'H'){
	                        if((hmx - x) >= k) fls = 0;
	                        if((dmx - x) < i && (cmx - x) < j && (hmx - x) >= k && (smx - x) < l) ucr = 1;
	                        else tru = 0;
	                    }
	                    else if(c == 'S'){
	                        if((smx - x) >= l) fls = 0;
	                        if((dmx - x) < i && (cmx - x) < j && (hmx - x) < k && (smx - x) >= l) ucr = 1;
	                        else tru = 0;
	                    }
	                }
	            }
	        }
	    }
	    if(malf == 1) sprint("ERROR");
	    else if(fls == 1) sprint("F");
	    else if(tru == 1) sprint("T");
	    else sprint("NG");
	    putchar('\n');
	}
	return 0;
}

E. 序列

题目描述

小Z有一个长度为 nn 的序列 aa,有以下函数:

C++
void update(int u){ 
  int w = 0; 
  for(int i=1;i<=n;i++) w ^= a[i]; 
  a[u] = w; 
}

小Z可以执行这个函数若干次,其中参数 uu 由你指定,请问将序列 aa 修改为序列 bb 的最小调用次数是多少?

输入格式

输入第一行,包含一个正整数 nn,表示序列的长度。

第二行给定 nn 个正整数,表示序列 aa。

第三行给定 nn 个正整数,表示目标序列 bb。

输出格式

输出一行,包含一个整数,表示最小调用次数。

输入输出样例

输入:

代码
3  
0 1 2  
3 1 0  

输出:

代码
2  

Solution

这里首先鸣谢GCX大佬和宝<((・))>><<<《大佬协助的证明和纠错

我们先关注这个函数有什么用

发现它将整个aa数组的值全部异或起来在赋值给某一个位置

设初始数组为a={a1,a2,a3,a4,…,an}a = \{ a_1,a_2,a_3,a_4,\dots,a_n\}

初始所有值异或起来是xx

我们有x=a1⊕a2⊕a3⊕⋯⊕anx = a_1 \oplus a_2 \oplus a_3 \oplus \dots \oplus a_{n}

我们第一次操作将aia_i赋值为xx

我们的aa数组变为a={a1,a2,a3,a4,…,ai−1,x,ai+1,…,an}a = \{ a_1,a_2,a_3,a_4,\dots,a_{i - 1},x,a_{i + 1},\dots ,a_n\}

此时所有值异或起来是yy

我们发现y=a1⊕a2⊕a3⊕…ai−1⊕x⊕ai+1⊕⋯⊕any = a_1 \oplus a_2 \oplus a_3 \oplus \dots a_{i - 1} \oplus x \oplus a_{i + 1} \oplus \dots \oplus a_{n}

我们尝试将xx展开带入上式

因为p⊕p=0p \oplus p = 0

所以我们将上式化简为y=aiy = a_i

第二次操作将aja_j赋值为yy

我们实际将aja_j赋值为aia_i

我们发现了一些规律

我们的update操作,实际上就是把aa数组中的任意一个元素与xx进行交换,再用交换出来的aia_i与新的aa数组进行交换,如此往复,使其最后成为bb数组

其实实际上整个aa数组只会有n+1n + 1个元素,nn为aa的元素个数

我们把上面的操作看做从xx出发,每次替换一个元素视作走了一步

因为我们最后一定会用bib_i替换aia_i,我们直接从bib_i到aia_i连边(题目保证有解,所以bib_i肯定在aa数组中出现过)

我们就顺利的将这道题转化为图论问题

我们的答案就是边的数量加上联通块的数量

每一条边对应一次操作

我们实际上就是想在整张图上找到最短的经过边数的路径去走遍所有的点,因为我们的操作的最小次数就是边数的最小值

我们可以更改一下题面,设有一个长度为n+1n+1的aa数组和长度为n+1n+1的bb数组

aa数组n+1n+1号位置是前nn个位置的异或值

我们从最后一个位置,即n+1n+1号位置开始,与前面的位置进行交换,最后使得前nn位与bb数组的前nn位相同

由于题面保证一定有解,我们就会发现对于每一条有向边bi=>aib_i => a_i,一定还会有一条边从aia_i连出,还会有一条边从bib_i连入,换句话说,整张图必定是多个联通块组成,而每个联通块有性质:入度一定等于出度,不然不会满足一一对应的关系,自然也就无解

我们发现这个性质可以说明我们一定可以不重不漏的一遍走完联通块内的点,因为这个性质满足欧拉图的性质

于是我们假设整张图长这样

00为起点,我们发现Part1是一个标准的欧拉图,从00出发到11结束,我们还发现为了让整个图的每个点和边都被经过一次,我们需要从Part1向Part2连边,幸好保证了全部的联通块都是欧拉路或欧拉回路,所以我们一定可以找到两个联通块之间的连接点,所以每个联通块之间都要联通,所以我们要多走联通块数量−1-1条边,因而我们需要走过的总边数是:

原图边数 + 联通块数 - 1

不要被TJ误导了,答案就是这个式子,相信我,你只需要数原图的联通块数量即可

TJ的意思是:

如果图不连通,或 xx不在连通块内( xx是孤立点),则答案就是边数再加上连通块数再减去11 (如果 xx是孤立点就不用减 )。

TJ上的解释是为了方便写代码,我们单独看看xx存不存在于联通块中,这样方便统计

但实际上xx如果不属于联通块中也相当于是多了一个联通块,只是我们不方便建边统计所以特判一波

实际上减不减11就只是看一下xx与其他联通块的关系而已

最后注意此题需要离散化,不然vector建边存不下

Code:

C++
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef __int128 SLL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
template <typename T> inline void read(T &x){
	x = 0;
	LL f = 1;
	char c = getchar();
	for(; !isdigit(c); c=getchar()) if(c == '-') f = -1;
	for(; isdigit(c); c=getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
	x *= f;
}
template <typename T> inline void print(T x){
	if(x < 0) x = -x,putchar('-');
	if(x > 9) print(x / 10);
	putchar(x % 10 + 48);
}
const int N = 2e5 + 11;
int n,org,nid;
int a[N],b[N];
bool fl[N],vis[N];
vector<int> g[N]; 
map<int,int> mp;
void dfs(int u){
	vis[u] = 1;
	for(auto x : g[u]){
		if(vis[x] == 0) dfs(x);
	}
	return;
} 
int main(){
    //	freopen("xor.in","r",stdin);
	//	freopen("xor.out","w",stdout);
	read(n);
    for(int i=1;i<=n;i++){
    	read(a[i]);
    	org ^= a[i];
	}
	for(int i=1;i<=n;i++) read(b[i]);
    for(int i=1;i<=n;i++){
        if(mp.find(a[i]) == mp.end()) mp[a[i]] = ++ nid;
        if(mp.find(b[i]) == mp.end()) mp[b[i]] = ++ nid;
    }
    int m = 0,loops = 0;
    for(int i=1;i<=n;i++){
    	if(a[i] == b[i]) continue;
        g[mp[b[i]]].push_back(mp[a[i]]);
        g[mp[a[i]]].push_back(mp[b[i]]);
        fl[mp[b[i]]] = fl[mp[a[i]]] = 1;
        m ++; 
    }
    for(int i=1;i<=nid;i++){
    	if(fl[i] == 1 && vis[i] == 0){
    		loops ++;
    		dfs(i);
		}
	}
    int ans = m + loops;
    if(mp.find(org) != mp.end()){
        if(fl[mp[org]] == 1) ans --;
    }
    print(ans);
    return 0;
}