跳到正文

个人记录

2025 国庆集训 DAY3

_Sorasaki_Hina_约 28 分钟
原文

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

文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 28 分钟

版本

  1. v3当前
  2. v2
  3. v1

T1 等腰三角形(isosceles,1s,512MB)

题目描述

小明有两个等腰三角形,边长分别为 a1a_{1},b1b_{1},c1c_{1} 和 a2a_{2},b2b_{2},c2c_{2},中保证 a1=b1a_{1}=b_{1},2a1>c12 a_{1}>c_{1} 和 a2=b2a_{2}=b_{2},2a2>c22 a_{2}>c_{2}。
小明想知道,在底边平行的前提下,是否可以将其中一个等腰三角形完全的放在另一个等腰三角形的内部,如果可以,输出YesYes,否则输出 NoNo。

输入格式

第一行一个整数 TT 表示数据组数。
对于每组数据,输入一行六个正整数 a1a_{1},b1b_{1},c1c_{1},a2a_{2},b2b_{2},c2c_{2}。

输出格式

对于每组数据,输出 YesYes 或者 NoNo。

输入输出样例#1

  • 输入#1
代码
10
1 1 1 1 1 1
1 1 1 2 2 2
2 2 3 2 2 2
3 3 5 4 4 5
5 5 8 3 3 1
5 5 8 4 4 1
20 20 32 15 15 18
20 20 32 15 15 19
20 20 32 15 15 17
100 100 180 150 150 1
  • 输出#1
代码
Yes
Yes
No
Yes
Yes
NO
Yes
Yes
NO
NO

说明/提示

  • 对于 30%30\% 的数据,a1=b1=c1a_{1}=b_{1}=c_{1},a2=b2=c2a_{2}=b_{2}=c_{2}。
  • 对于另外 30%30\% 的数据,a1>c1a_{1}>c_{1},a2>c2a_{2}>c_{2}。
  • 对于全部数据 1≤T≤101 ≤T ≤10,1≤a11 ≤a_{1},b1b1,c1c_{1},a2a_{2},b2b2,c2≤106c_{2} ≤10^{6},并且保证 a1=b1a_{1}=b_{1},2a1>c12 a_{1}>c_{1} 和 a2=b2a_{2}=b_{2},2a2>c22 a_{2}>c_{2}。

题解

两个等腰三角形 pp,qq,不妨设 cc 较小的一个是 pp。
设 pp 的 c1c_1 的高为 h1h_1,qq 的 c2c_2 的高为 h2h_2。

如果 h1≤h2h_1≤h_2 即为 YesYes,否则 NoNo。

推一下:

h1≤h2⇒a12−(c12)2≤a22−(c22)2⇒4a12−c12≤4a22−c22⇒4a12+c22≤4a22+c12h_1≤h_2 \Rightarrow a_1^2-\Big(\dfrac{c_1}{2}\Big)^2≤a_2^2-\Big(\dfrac{c_2}{2}\Big)^2 \Rightarrow 4a_1^2-c_1^2≤4a_2^2-c_2^2\Rightarrow 4a_1^2+c_2^2≤4a_2^2+c_1^2

最后只需要检查 4a12+c22≤4a22+c124a_1^2+c_2^2≤4a_2^2+c_1^2 就行了,避免了精度问题。

代码

自己的:

C++
#include<cstdio>
#include<iostream>
#include<cmath>
#define ll __int128
#define re register
#define endl '\n'
using namespace std ;
template<class T>inline void read(T &x){x=0;T f=1;char c=getchar();while(c<'0'||c>'9'){if (c=='-'){f=-1;}c=getchar();}while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+(c^48);c=getchar();}x*=f;}
template<class T>inline void nout(T x){if(x<0){x=-x;putchar('-');}if(x>9){nout(x/10);}putchar(x%10+'0');}
template<class T>inline void write(T x,char c){nout(x),putchar(c);}
int T ;
ll a1,b1,c1,a2,b2,c2 ;
bool check_h(ll a,ll b,ll c,ll x,ll y,ll z) {
	ll p = a * a * 4 + z * z ;
	ll q = x * x * 4 + c * c ;
	return p <= q ;
}
int main () {
	//freopen("isosceles.in","r",stdin) ;
	//freopen("isosceles.out","w",stdout) ;
	read(T) ;
	while (T--) {
		read(a1),read(b1),read(c1),read(a2),read(b2),read(c2) ;
		if (c1 > c2) swap(a1,a2),swap(b1,b2),swap(c1,c2) ;//底边小的是第一个三角形
		if (check_h(a1,b1,c1,a2,b2,c2)) printf("Yes\n") ;
		else printf("No\n") ; 
	} 
	return 0 ;
}

std:

C++
#include <bits/stdc++.h>
#define int long long
using namespace std ;
const int N = 1.01e6 ;
signed main(){
    //freopen("isosceles.in","r",stdin);
    //freopen("isosceles.out","w",stdout);
    int T ;
    cin>>T ;
    while (T--) {
        int a,b,c,d ;
        cin>>a ;cin>>a ;cin>>b ;
        cin>>c ;cin>>c ;cin>>d ;
        if (d == b) {
            printf("Yes\n") ;
            continue ;
        }
        if (d > b) swap(d,b),swap(c,a) ;
        a *= 2,c *= 2 ;
        int x = a * a - b * b,y = c * c - d * d ;
        if (x >= y) printf("Yes\n") ;
        else printf("No\n") ;
    }
    //fclose(stdin);
    //fclose(stdout);
    return 0;
}

T2 修复括号串 (repair,1s,512MB)

题目描述

给定一个长度为 nn(nn 为偶数),由左右括号组成的字符串 SS。
每次操作可以修改 SS 中的某个字符,求出最少的操作次数,使 SS 变成一个括号串。
我们定义满足以下条件的字符串为括号串:

  1. 空串是括号串。
  2. 如果 AA 是括号串,(A)(A) 是括号串。
  3. 如果 AA,BB 都是括号串,ABA B 是括号串。

输入格式

第一行输入一个整数 nn。
第二行输入一个长度为 nn 的字符串 SS,SS 中每个字符为左括号或者右括号。

输出格式

输出一行一个整数,表示最少的操作次数。

输入输出样例 #1

  • 输入 #1
代码
4
())(
  • 输出 #1
代码
2

输入输出样例 #2

  • 输入 #2
代码
2
((
  • 输出 #2
代码
1

说明/提示

  • 对于 30%30\% 的数据,n≤10n ≤10。
  • 对于另外 30%30\% 的数据,n≤103n ≤10^{3}。
  • 对于另外 20%20\% 的数据, S 中仅包含左括号。
  • 对于全部数据,1≤n≤1061 ≤n ≤10^{6},nn 是偶数,SS 长度为 nn,而且每个字符为左括号或者右括号。

题解

栈的板子,遇到 ( 就入栈,) 弹栈。

  • 如果全是 ( 或全是),直接输出 n2\dfrac{n}{2} 即可。
  • 当遇到 ) 时如果已经空栈了,说明 ( 少,增加答案,并将一个 ( 入栈即可(其实是将这个 ) 变成了 ()。
  • 当遍历完一遍后栈里还有元素,说明 ( 多,需要将一半的数量变成 ) 来配对,答案增加(栈里剩下的元素个数/2)

代码

自己的:

C++
#include<cstdio>
#include<iostream>
#include<string>
#include<stack>
#define ll long long
#define re register
#define endl '\n'
using namespace std ;
template<class T>inline void read(T &x){x=0;T f=1;char c=getchar();while(c<'0'||c>'9'){if (c=='-'){f=-1;}c=getchar();}while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+(c^48);c=getchar();}x*=f;}
template<class T>inline void nout(T x){if(x<0){x=-x;putchar('-');}if(x>9){nout(x/10);}putchar(x%10+'0');}
template<class T>inline void write(T x,char c){nout(x),putchar(c);}
stack<char>st ;
string s ;
int n ;
ll ans ;
int main () {
	//freopen("repair.in","r",stdin) ;
	//freopen("repair.out","w",stdout) ;
	read(n) ;
	cin>>s ;
	if (s.find(')') == -1 || s.find('(') == -1)) {//点1
		nout(n >> 1) ;
		return 0 ;
	}
	for (re int i = 0 ; i < n ; ++i) {
		if (s[i] == '(') st.push('(') ;
		else {
			if (st.size()) st.pop() ;
			else ++ans,st.push('(') ;//点2
		}
	}
	if (st.size()) ans += (st.size() >> 1) ;//点3
	nout(ans) ;
	return 0 ;
}

std(二分):

C++
#include <bits/stdc++.h>
using namespace std ;
const int N = 1.01e6 ;
int n,x,y,ans1 ;
char s[N],_s[N] ; 
bool ck(int t){
    for(int i = 1 ; i <= n ; i++) _s[i] = s[i] ;
    s[0] = '(',s[n + 1] = ')' ;
    int l = 1,r = n ;
    while (l <= r && t > 0) {
        while (s[l ]== '(') ++l ;
        while (s[r] == ')') --r ;
        if (r < l) break ;
        swap(s[l],s[r]),--t ;
    }
    for(int i = 1,p = 0 ; i <= n ; i++) {
        if (s[i] == '(') ++p ;
        else --p ;
        if (p < 0) return false ;
    }
    for (int i = 1 ; i <=n ; i++) s[i] = _s[i] ;
    return true ;
}

signed main(){
    //freopen("repair.in","r",stdin) ;
    //freopen("repair.out","w",stdout) ;
    
    cin>>n ;
    scanf("%s",s + 1) ;
    for (int i = 1 ;i <= n ; i++) {
        if (s[i] == '(') ++x ;
        else ++y ;
    }
    for (int i = 1 ; i <= n ; i++) {
        if (s[i] == ')' && x < y) s[i] = '(',++x,--y,++ans1 ;
    }
    for(int i = n ; i >= 1 ; i--) {
        if (s[i] == '(' && x > y) s[i] = ')',--x,++y,++ans1 ;
    }
    int L = 0,R = n / 2,ans = n / 2 ;
    while (L <= R) {
        int M = (L + R) / 2 ;
        if (ck(M)) ans = M,R = M - 1 ;
        else L = M + 1 ;
    }
    printf("%d\n",ans1 + ans * 2) ;
    //fclose(stdin) ;
    //fclose(stdout) ;
    return 0 ;
}

T3 括号路径 (bracketpath,1s,512MB)

题目描述

有一个 n 个点 m 条边的无向图,每条边上标有左括号 "((" ,或者右括号 "))" 。
我们定义满足以下条件的字符串为括号串:

  1. 空串是括号串。
  2. 如果 AA 是括号串,(A)(A)是括号串。
  3. 如果 AA,BB 都是括号串,ABA B 是括号串。

我们定义一条括号路径满足以下条件:从起点到终点经过的所有边上标的括号按经过的先后顺序写下来,构成的字符串是括号串。

有 qq 组询问,每次询问起点为 xx,终点为 yy 的最短括号路径的长度,如果不存在合法路径则输出 −1-1。

输入格式

第一行输入三个整数 nn,mm,qq。
接下来 mm 行,每行两个整数和一个字符: uiu_{i},viv_{i},cic_{i},表示一条连接节点 uiu_{i} 和 viv_{i} 的无向边(ui≠viu_{i} ≠v_{i}),上面标记的括号为 ci∈{"(",")"}c_{i} \in\{ "(", ")" \}。
接下来 q 行,每行两个整数 xx,yy,表示一组询问的起点和终点。

输出格式

输出 q 行每行一个整数表示对应询问的答案。

输入输出样例 #1

  • 输入 #1
代码
4 5 5
1 2 (
2 3 )
3 4 (
4 1 )
1 3 (
1 1
1 3
1 4
1 2
2 1
  • 输出 #1
代码
0
2
4
2
6

输入输出样例 #2

  • 输入 #2
代码
2 1 1
1 2 (
1 2
  • 输出 #2
代码
-1

说明/提示

  • 对于 20%20\% 的数据,1≤n≤51≤n≤5,1≤m≤101 ≤m ≤10。
  • 对于另外 30%30\% 的数据,1≤n≤201 ≤n ≤20,1≤m≤501 ≤m ≤50。
  • 对于另外 20%20\% 的数据,q≤5q ≤5。
  • 对于全部数据,1≤n≤4001 ≤n ≤400,1≤m≤5001 ≤m ≤500,1≤ui1 ≤u_{i},vi≤nv_{i} ≤n,ui≠viu_{i} ≠v_{i},1≤q≤1051 ≤q ≤10^{5}。

题解

一些东西:

  1. 平衡度:起点到某一点的路径中,左括号比右括号多的数量。
  2. dis(s,i,j)dis_{(s,i,j)}:起点 ss 到点 ii,平衡度为 jj 的最短路径长度。
  3. checkcheck,pqpq:打标记用的数组,优先队列(存<0>当前路径长度,<1>当前节点,<2>当前平衡度)。

DijkstraDijkstra 的实现:

  1. 算平衡度:是 ( 就 +1+1,否则 −1-1。
  2. 查越界:平衡度保持非负,且不能超过上界。
  3. 转移(松弛):当新路径长度小于当前记录值时,更新状态。

路径的合法条件:最后平衡值为 00,且中途任意时刻平衡值不能为负(其实就是避免 )( 的出现)

时间复杂度:O(Nn2log⁡n)O\big(Nn^2\log n\big)(NN 为平衡值上界)(?)(nn 和 mm 差不多我就不管了)

代码

自己的:

C++
#include<cstdio>
#include<iostream>
#include<queue>
#include<vector>
#include<tuple>
#include<cstring>
#define ll long long
#define re register
#define endl '\n'
using namespace std ;
template<class T>inline void read(T &x){x=0;T f=1;char c=getchar();while(c<'0'||c>'9'){if (c=='-'){f=-1;}c=getchar();}while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+(c^48);c=getchar();}x*=f;}
template<class T>inline void nout(T x){if(x<0){x=-x;putchar('-');}if(x>9){nout(x/10);}putchar(x%10+'0');}
template<class T>inline void write(T x,char c){nout(x),putchar(c);}
const int N = 402 ;  // 平衡度上界,数组大小
struct edge {
	int v,w ;  // w: 括号类型,'('为1,')'为-1
	edge() {}
	edge(int _v,int _w) {v = _v,w = _w ;}
};
int n,m,q ;
priority_queue<tuple<int,int,int>,vector<tuple<int,int,int>>,greater<tuple<int,int,int>>>pq ;  // 堆:(路径长度, 当前节点, 括号平衡值)
vector<edge> g[N] ;  // 邻接表
bool check[N][N] ;   // 标记数组
int dis[N][N][N] ;   // dis[s][i][j]: 从起点s到节点i,平衡度为j的最短路

void dijkstra(int s) {
	memset(check,0,sizeof(check)) ;  // 初始化标记数组
	for (re int i = 1 ; i <= n ; ++i) {
		for (re int j = 0 ; j < N ; ++j) {
			dis[s][i][j] = 0x3f3f3f3f ;  // 初始化所有距离为无穷大
		}
	}
	dis[s][s][0] = 0 ;  // 起点到自身的平衡值为0,路径长度为0
	pq.push(make_tuple(0,s,0)) ;  // 初始状态入队
	while (!pq.empty()) {
		tuple<int,int,int> nd = pq.top() ;
		pq.pop() ;
		int minn = get<0>(nd),minx = get<1>(nd),num = get<2>(nd) ;  // 当前路径长度、当前节点、当前括号平衡值
		if (check[minx][num]) continue ;  // 如果该状态已处理过,跳过
		check[minx][num] = 1 ;  // 标记该状态已处理
		for (re edge e : g[minx]) {  // 遍历当前点所有邻边
			int v = e.v,w = e.w ;  // v:邻接节点,w:括号类型
			int now = num + w ;    // 更新后的括号平衡度
			if (now < 0 || now >= N) continue ;  // 平衡值必须非负且小于N,否则越界
			if (dis[s][v][now] > minn + 1) {  // 如果找到更短路
				dis[s][v][now] = minn + 1 ;   // 更新最短路
                pq.push(make_tuple(dis[s][v][now],v,now)) ;  // 新状态入队
			}
		}
	}
}

int main () {
	//freopen("bracketpath.in","r",stdin) ;
	//freopen("bracketpath.out","w",stdout) ;
	read(n),read(m),read(q) ;
	for (re int i = 1,x,y ; i <= m ; ++i) {
		re char c ;
		read(x),read(y) ;
		cin>>c ;
		// 括号换为数值:'('->1,')'->-1,
		g[x].emplace_back(y,c == '(' ? 1 : -1) ;
		g[y].emplace_back(x,c == '(' ? 1 : -1) ;
	}
	// 对每个起点Dij,算出所有点的所有平衡值的最短路
	for (re int s = 1 ; s <= n ; ++s) dijkstra(s) ;
	for (re int i = 1,x,y ; i <= q ; ++i) {
		read(x),read(y) ;
		// 起点x,终点y且平衡值为0的最短路
		if (dis[x][y][0] == 0x3f3f3f3f) write(-1,endl) ;  // 不存在是-1
		else write(dis[x][y][0],endl) ;
	}
	return 0 ;
}

std(SPFA):

C++
#include <bits/stdc++.h>
using namespace std;
const int N = 410,mod = 998244353,inf = 1.01e9 ;
int n,m,q ;
struct node{
	int x,y ;
	node(int x = 0,int y = 0) : x(x),y(y) {}
};
vector<node> g[N] ;
int d[N][N][N],vis[N][N] ;
queue<node> Q ;

void dij(int S) {
	for (int i = 1 ; i <= n ; i++) {
		for (int j = 0 ; j < n ; j++) d[S][i][j] = inf,vis[i][j] = 0 ;
	}
	d[S][S][0] = 0 ;
	Q.push(node(S,0)) ;
	while (!Q.empty()) {
		node u = Q.front() ;
		Q.pop() ;
		if (vis[u.x][u.y]++) continue ;
		for(unsigned i = 0 ; i < g[u.x].size() ; i++) {
			int v = g[u.x][i].x,t = u.y + g[u.x][i].y ;
			if (t < 0) continue ;
			if (d[S][u.x][u.y] + 1 < d[S][v][t]) {
				d[S][v][t] = d[S][u.x][u.y] + 1 ;
				Q.push(node(v,t)) ;
			}
		}
	}
	return ;
}

signed main () {
    //freopen("bracketpath.in","r",stdin) ;
    //freopen("bracketpath.out","w",stdout) ;
    
	cin>>n>>m>>q ;
	for (int i = 1,u,v ; i <= m ; i++) {
		char c ;
		cin>>u>>v>>c ;
		g[u].push_back(node(v,c == '(' ? 1 : -1)) ;
		g[v].push_back(node(u,c == '(' ? 1 : -1)) ;
	}
	for(int i = 1 ; i <= n ; i++) dij(i) ;
	while (q--) {
		int x,y ;
		cin>>x>>y ;
		if (d[x][y][0] == inf) printf("-1\n") ;
		else printf("%d\n",d[x][y][0]) ;
	}
    return 0 ;
}

T4 彩色棋盘 (colorful,1s,512MB)

题目描述

有一个 n 行 n 列的网格。
有 m 次操作,每次操作给定 lxlx,rxrx,lyly,ryry c,表示对于所有第 xx 行,第 yy 列的格点,如果满足 lx≤x≤rxl x ≤x ≤r x,ly≤y≤ryl y ≤y ≤r y,则在这个位置上放入一枚染色为 cc 的棋子(同一个格子可以放入多枚棋子)。

小 Z 想知道 mm 次操作后,有多少个格子内部有全部 CC 种颜色的棋子。

输入格式

第一行三个整数 nn,mm,CC 表示棋盘大小,操作次数,颜色种类数。
接下来 mm 行,每行五个整数 lxlx,rxrx,lyly,ryr y,cc 表示一次操作 ( 1≤c≤C1 ≤c ≤C )。

输出格式

输出一行一个整数,表示内部有全部 CC 种颜色的棋子的格点数量。

输入输出样例 #1

  • 输入 #1
代码
6 3 3
1 4 1 4 1
2 5 2 5 2
3 6 3 6 3
  • 输出 #1
代码
4

说明/提示

  • 对于 20%20\% 的数据,n,m≤500n,m ≤500。
  • 对于另外 20%20\% 的数据,n≤2000n ≤2000,m≤105m ≤10^{5}。
  • 对于另外 20%20\% 的数据,C=1C=1。
  • 对于另外 20%20\% 的数据,C=2C=2。
  • 对于另外 10%10\% 的数据,C=3C=3。
  • 对于全部数据,1≤n1≤n,m≤105m≤10^{5},1≤C≤51 ≤C ≤5,1≤lx≤rx≤n1 ≤l x ≤r x ≤n,1≤ly≤ry≤n1 ≤l y ≤r y ≤n,1≤c≤C1 ≤c ≤C。

题解

代码

自己的:

std:

C++
#include <bits/stdc++.h>
using namespace std ;
const int N = 1.01e5,M = 5 ;
int n,m,C,A ;
struct node {
	int l,r,col,tag ;
	node(int l = 0,int r = 0,int col = 0,int tag = 0) : l(l),r(r),col(col),tag(tag) {}
};
vector<node> upt[N] ;
struct Node{
	int cnt[M],s[1 << M] ; 
}t[N << 2] ;
#define Lid (id << 1)
#define Rid (Lid | 1)
#define M ((L + R) / 2)

void build (int id,int L,int R) {
	t[id].s[0] = R - L + 1 ;
	if (L != R) build(Lid,L,M),build(Rid,M+1,R) ;
	return ;
}

void update (int id,int L,int R,int l,int r,int col,int tag) {
	if (r < L || R < l) return ;
	if (l <= L && r >=R ) {
		t[id].cnt[col] += tag ;
	}
	else{
		update(Lid,L,M,l,r,col,tag) ;
		update(Rid,M+1,R,l,r,col,tag) ;
	}
	int z=0 ;
	for (int i = 0 ; i < C ; i++) if (t[id].cnt[i] > 0) z |= (1 << i) ;
	for (int i = 0 ; i < A ; i++) t[id].s[i] = 0 ;
	if (L == R) t[id].s[z] = 1 ;
	else{
		for (int i = 0 ; i < A ; i++) t[id].s[i | z] += t[Lid].s[i] + t[Rid].s[i] ;
	}
	return ;
}

//int bf[1024][1024][3] ; 

signed main(){
    //freopen("colorful.in","r",stdin) ;
    //freopen("colorful.out","w",stdout) ;
    
    scanf("%d %d %d",&n,&m,&C) ;
    A = 1 << C ;
    build(1,1,n) ; 
    for (int i = 1,lx,rx,ly,ry,c ; i <= m ; i++) {
    	scanf("%d %d %d %d %d",&lx,&rx,&ly,&ry,&c) ;
    	--c ;
//    	for(int x = lx ; x <= rx ; x++) {
//    		for(int y = ly ; y <= ry ; y++) {
//    			bf[x][y][c]++ ;
//			}
//		}
    	upt[lx].push_back(node(ly,ry,c,1)) ;
    	upt[rx+1].push_back(node(ly,ry,c,-1)) ;
	}
//	long long ans1 = 0;
//	for (int i = 1 ; i <= n ; i++) {
//		for (int j = 1 ; j <= n ; j++) {
//			if (bf[i][j][0 % C] && bf[i][j][1 % C] && bf[i][j][2 % C]) ++ans1 ;
//		}
//	}
	long long ans = 0 ;
	for (int i = 1 ; i <= n ; i++) {
		for (unsigned j = 0 ; j < upt[i].size() ; j++) {
			update(1,1,n,upt[i][j].l,upt[i][j].r,upt[i][j].col,upt[i][j].tag) ;
		}
		ans += t[1].s[A - 1] ;
	}
    printf("%lld\n",ans) ;
//    printf("%lld\n",ans1) ;
    
    //fclose(stdin);
    //fclose(stdout);
    return 0 ;
}