2025 国庆集训 DAY3
_Sorasaki_Hina_ · · 个人记录
T1 等腰三角形(isosceles,1s,512MB)
题目描述
小明有两个等腰三角形,边长分别为
小明想知道,在底边平行的前提下,是否可以将其中一个等腰三角形完全的放在另一个等腰三角形的内部,如果可以,输出
输入格式
第一行一个整数
对于每组数据,输入一行六个正整数
输出格式
对于每组数据,输出
输入输出样例#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\% 的数据,a_{1}=b_{1}=c_{1} ,a_{2}=b_{2}=c_{2} 。 - 对于另外
30\% 的数据,a_{1}>c_{1} ,a_{2}>c_{2} 。 -
对于全部数据
1 ≤T ≤10 ,1 ≤a_{1} ,b1 ,c_{1} ,a_{2} ,b2 ,c_{2} ≤10^{6} ,并且保证a_{1}=b_{1} ,2 a_{1}>c_{1} 和a_{2}=b_{2} ,2 a_{2}>c_{2} 。题解
两个等腰三角形
p ,q ,不妨设c 较小的一个是p 。
设p 的c_1 的高为h_1 ,q 的c_2 的高为h_2 。
如果
推一下:
最后只需要检查
代码
自己的:
#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:
#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)
题目描述
给定一个长度为
每次操作可以修改
我们定义满足以下条件的字符串为括号串:
- 空串是括号串。
- 如果
A 是括号串,(A) 是括号串。 - 如果
A ,B 都是括号串,A B 是括号串。
输入格式
第一行输入一个整数
第二行输入一个长度为
输出格式
输出一行一个整数,表示最少的操作次数。
输入输出样例 #1
- 输入 #1
4 ())( - 输出 #1
2
输入输出样例 #2
- 输入 #2
2 (( - 输出 #2
1
说明/提示
-
对于
30\% 的数据,n ≤10 。 -
对于另外
30\% 的数据,n ≤10^{3} 。 -
对于另外
20\% 的数据, S 中仅包含左括号。 -
对于全部数据,
1 ≤n ≤10^{6} ,n 是偶数,S 长度为n ,而且每个字符为左括号或者右括号。题解
栈的板子,遇到
(就入栈,)弹栈。- 如果全是
(或全是),直接输出\dfrac{n}{2} 即可。 - 当遇到
)时如果已经空栈了,说明(少,增加答案,并将一个(入栈即可(其实是将这个)变成了()。 - 当遍历完一遍后栈里还有元素,说明
(多,需要将一半的数量变成)来配对,答案增加(栈里剩下的元素个数/2)代码
自己的:
#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(二分):
#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. 如果 $A$ 是括号串,$(A)$是括号串。
3. 如果 $A$,$B$ 都是括号串,$A B$ 是括号串。
我们定义一条括号路径满足以下条件:从起点到终点经过的所有边上标的括号按经过的先后顺序写下来,构成的字符串是括号串。
有 $q$ 组询问,每次询问起点为 $x$,终点为 $y$ 的最短括号路径的长度,如果不存在合法路径则输出 $-1$。
#### 输入格式
第一行输入三个整数 $n$,$m$,$q$。
接下来 $m$ 行,每行两个整数和一个字符: $u_{i}$,$v_{i}$,$c_{i}$,表示一条连接节点 $u_{i}$ 和 $v_{i}$ 的无向边($u_{i} ≠v_{i}$),上面标记的括号为 $c_{i} \in\{ "(", ")" \}$。
接下来 q 行,每行两个整数 $x$,$y$,表示一组询问的起点和终点。
#### 输出格式
输出 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\%$ 的数据,$1≤n≤5$,$1 ≤m ≤10$。
- 对于另外 $30\%$ 的数据,$1 ≤n ≤20$,$1 ≤m ≤50$。
- 对于另外 $20\%$ 的数据,$q ≤5$。
- 对于全部数据,$1 ≤n ≤400$,$1 ≤m ≤500$,$1 ≤u_{i}$,$v_{i} ≤n$,$u_{i} ≠v_{i}$,$1 ≤q ≤10^{5}$。
---
### 题解
一些东西:
1. 平衡度:起点到某一点的路径中,左括号比右括号多的数量。
2. $dis_{(s,i,j)}$:起点 $s$ 到点 $i$,平衡度为 $j$ 的**最短路径长度**。
3. $check$,$pq$:打标记用的数组,优先队列(存<0>当前路径长度,<1>当前节点,<2>当前平衡度)。
$Dijkstra$ 的实现:
1. 算平衡度:是 `(` 就 $+1$,否则 $-1$。
2. 查越界:平衡度保持非负,且不能超过上界。
3. 转移(松弛):当新路径长度小于当前记录值时,更新状态。
路径的合法条件:最后平衡值为 $0$,且中途任意时刻平衡值不能为负(其实就是避免 `)(` 的出现)
时间复杂度:$O\big(Nn^2\log n\big)$($N$ 为平衡值上界)(?)($n$ 和 $m$ 差不多我就不管了)
### 代码
#### 自己的:
```cpp
#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):
#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)
题目描述
有一个
有
小 Z 想知道
输入格式
第一行三个整数
接下来
输出格式
输出一行一个整数,表示内部有全部
输入输出样例 #1
- 输入 #1
6 3 3 1 4 1 4 1 2 5 2 5 2 3 6 3 6 3 - 输出 #1
4
说明/提示
- 对于
20\% 的数据,n,m ≤500 。 - 对于另外
20\% 的数据,n ≤2000 ,m ≤10^{5} 。 - 对于另外
20\% 的数据,C=1 。 - 对于另外
20\% 的数据,C=2 。 - 对于另外
10\% 的数据,C=3 。 -
对于全部数据,
1≤n ,m≤10^{5} ,1 ≤C ≤5 ,1 ≤l x ≤r x ≤n ,1 ≤l y ≤r y ≤n ,1 ≤c ≤C 。题解
~(写不动了用AI吧)~
题目思路梳理
该问题要求计算一个
n×n 网格中,有多少个格子包含全部C 种颜色的棋子。每个操作在给定的子矩形区域内添加一种颜色的棋子(同一个格子可以有多个相同颜色的棋子)。关键在于C 较小(C ≤ 5 ),允许使用状态压缩和扫描线优化。
核心思路
- 扫描线降维:将二维网格问题转化为一维问题。按行扫描(从第
1 行到第n 行),对每一行维护所有列的颜色覆盖状态。 - 事件处理:每个操作 (
lx ,rx ,ly ,ry ,c ) 被拆分为两个事件:
- 添加事件:在行
lx添加,表示在列区间[ly, ry] 上颜色c 的覆盖次数+1 。 - 移除事件:在行
rx+1添加,表示在列区间[ly, ry] 上颜色c 的覆盖次数-1 。
- 线段树维护列状态:使用线段树维护当前行所有列的颜色覆盖状态。线段树每个节点存储:
cnt[M]:记录该节点对应区间内,每种颜色被完全覆盖的次数(M 为颜色数 C)。s[A]:数组(A = 2^C ),s[mask]表示该节点区间内,颜色覆盖状态恰好为mask的列数(mask的二进制位表示颜色是否存在)。
- 状态合并:在线段树更新过程中,每个节点的覆盖状态
z(由其cnt计算)会被应用到子节点的状态上,实现状态合并。 - 查询答案:对每一行,处理完该行的事件后,查询线段树根节点中状态为全
1 (即s[A-1])的列数,并累加到答案中。这表示当前行有多少列包含全部颜色。
算法流程
- 读入数据:网格大小
n 、操作数m 、颜色数C 。 - 初始化线段树:构建区间
[1, n] 的线段树,初始状态无颜色覆盖(所有位置掩码为0 )。 - 预处理事件:对每个操作,在
upt[lx]添加添加事件,在upt[rx+1]添加移除事件。 - 扫描每一行:
- 处理当前行
i的所有事件:调用线段树更新函数,更新列区间上的颜色覆盖。 - 查询当前行有多少列满足全颜色覆盖(即
t.s[A-1]),累加到答案。
- 输出答案:累计值即为所有满足条件的格子数量。
线段树更新原理
- 更新操作:当更新区间
[l, r] 的颜色c 覆盖次数(增加或减少)时: - 若当前节点区间
[L, R] 完全包含在[l, r] 内,更新当前节点的cnt[c]。 - 否则递归更新左右子树。
- 状态合并:更新后重新计算当前节点的状态数组
s:
- 计算当前节点的覆盖掩码
z:遍历每种颜色,若cnt[i] > 0则设置掩码第i 位。 - 初始化
s数组为0 。 - 叶子节点:直接置
s[z] = 1(只有一个列,状态为z)。 - 非叶子节点:将左右子树的状态数组与掩码
z合并:for (int i = 0; i < A; i++) t[id].s[i | z] += t[Lid].s[i] + t[Rid].s[i];
- 左右子树的状态
i与当前掩码z取或,得到新状态i | z。 - 当前节点的
s[i | z]累加左右子树状态为i的列数。 - 复杂度:每次更新访问
O(\log n) 个节点,每节点处理O(2^C) 的状态(C ≤ 5 ,故为常数)。总复杂度O(m \log n) 。代码
自己的:
//看std的吧,写不动了std:
#include<bits/stdc++.h> using namespace std ; const int N = 1.01e5,M = 5 ; // N: 最大网格大小,M: 最大颜色数 int n,m,C,A ; // n: 网格大小,m: 操作数,C: 颜色数,A: 状态总数 (2^C) struct node { int l,r,col,tag ; // 事件结构: 列区间 [l,r],颜色 col,操作类型 tag (+1/-1) 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] ; // 事件数组: upt[i] 存储第 i 行的事件 struct Node { int cnt[M],s[1 << M] ; // 线段树节点: cnt-颜色覆盖次数,s-状态数组 } t[N << 2] ; // 线段树数组 #define Lid (id << 1) // 左儿子索引 #define Rid (Lid | 1) // 右儿子索引 #define Mid ((L + R) >> 1) // 区间中点
void build (int id,int L,int R) { t[id].s[0] = R - L + 1 ; // 初始化: 所有列状态为 0 if (L != R) { build(Lid,L,Mid) ; // 递归构建左子树 build(Rid,Mid + 1,R) ; // 递归构建右子树 } }
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,Mid,l,r,col,tag) ; update(Rid,Mid + 1,R,l,r,col,tag) ; } // 计算当前覆盖掩码 z (存在的颜色集合) int z = 0 ; for (int i = 0 ; i < C ; i++) { if (t[id].cnt[i] > 0) z |= (1 << i) ; } // 重置状态数组 s for (int i = 0 ; i < A ; i++) t[id].s[i] = 0 ; if (L == R) { // 叶子节点: 当前列状态为 z t[id].s[z] = 1 ; } else { // 合并左右子树状态: 子树的掩码 i 与当前掩码 z 取或 for (int i = 0 ; i < A ; i++) { t[id].s[i | z] += t[Lid].s[i] + t[Rid].s[i] ; } } }
signed main() { scanf("%d %d %d",&n,&m,&C) ; A = 1 << C ; // 状态总数 = 2^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-- ; // 颜色转为 0-indexed upt[lx].push_back(node(ly,ry,c,1)) ; // 添加事件 upt[rx + 1].push_back(node(ly,ry,c,-1)) ; // 移除事件 } long long ans = 0 ; for (int i = 1 ; i <= n ; i++) { // 扫描每一行 // 处理当前行事件 for (unsigned j = 0 ; j < upt[i].size() ; j++) { node& e = upt[i][j] ; update(1,1,n,e.l,e.r,e.col,e.tag) ; } ans += t[1].s[A - 1] ; // 累加当前行全颜色覆盖的列数 } printf("%lld\n",ans) ; return 0 ; }