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说明/提示
- 对于 的数据,,。
- 对于另外 的数据,,。
- 对于全部数据 ,,,,,,,并且保证 , 和 ,。
题解
两个等腰三角形 ,,不妨设 较小的一个是 。
设 的 的高为 , 的 的高为 。
如果 即为 ,否则 。
推一下:
最后只需要检查 就行了,避免了精度问题。
代码
自己的:
#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)
题目描述
给定一个长度为 ( 为偶数),由左右括号组成的字符串 。
每次操作可以修改 中的某个字符,求出最少的操作次数,使 变成一个括号串。
我们定义满足以下条件的字符串为括号串:
- 空串是括号串。
- 如果 是括号串, 是括号串。
- 如果 , 都是括号串, 是括号串。
输入格式
第一行输入一个整数 。
第二行输入一个长度为 的字符串 , 中每个字符为左括号或者右括号。
输出格式
输出一行一个整数,表示最少的操作次数。
输入输出样例 #1
- 输入 #1
4
())(- 输出 #1
2输入输出样例 #2
- 输入 #2
2
((- 输出 #2
1说明/提示
- 对于 的数据,。
- 对于另外 的数据,。
- 对于另外 的数据, S 中仅包含左括号。
- 对于全部数据,, 是偶数, 长度为 ,而且每个字符为左括号或者右括号。
题解
栈的板子,遇到 ( 就入栈,) 弹栈。
- 如果全是
(或全是),直接输出 即可。 - 当遇到
)时如果已经空栈了,说明(少,增加答案,并将一个(入栈即可(其实是将这个)变成了()。 - 当遍历完一遍后栈里还有元素,说明
(多,需要将一半的数量变成)来配对,答案增加(栈里剩下的元素个数/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 条边的无向图,每条边上标有左括号 "" ,或者右括号 "" 。
我们定义满足以下条件的字符串为括号串:
- 空串是括号串。
- 如果 是括号串,是括号串。
- 如果 , 都是括号串, 是括号串。
我们定义一条括号路径满足以下条件:从起点到终点经过的所有边上标的括号按经过的先后顺序写下来,构成的字符串是括号串。
有 组询问,每次询问起点为 ,终点为 的最短括号路径的长度,如果不存在合法路径则输出 。
输入格式
第一行输入三个整数 ,,。
接下来 行,每行两个整数和一个字符: ,,,表示一条连接节点 和 的无向边(),上面标记的括号为 。
接下来 q 行,每行两个整数 ,,表示一组询问的起点和终点。
输出格式
输出 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说明/提示
- 对于 的数据,,。
- 对于另外 的数据,,。
- 对于另外 的数据,。
- 对于全部数据,,,,,,。
题解
一些东西:
- 平衡度:起点到某一点的路径中,左括号比右括号多的数量。
- :起点 到点 ,平衡度为 的最短路径长度。
- ,:打标记用的数组,优先队列(存<0>当前路径长度,<1>当前节点,<2>当前平衡度)。
的实现:
- 算平衡度:是
(就 ,否则 。 - 查越界:平衡度保持非负,且不能超过上界。
- 转移(松弛):当新路径长度小于当前记录值时,更新状态。
路径的合法条件:最后平衡值为 ,且中途任意时刻平衡值不能为负(其实就是避免 )( 的出现)
时间复杂度:( 为平衡值上界)(?)( 和 差不多我就不管了)
代码
自己的:
#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)
题目描述
有一个 n 行 n 列的网格。
有 m 次操作,每次操作给定 ,,, c,表示对于所有第 行,第 列的格点,如果满足 ,,则在这个位置上放入一枚染色为 的棋子(同一个格子可以放入多枚棋子)。
小 Z 想知道 次操作后,有多少个格子内部有全部 种颜色的棋子。
输入格式
第一行三个整数 ,, 表示棋盘大小,操作次数,颜色种类数。
接下来 行,每行五个整数 ,,,, 表示一次操作 ( )。
输出格式
输出一行一个整数,表示内部有全部 种颜色的棋子的格点数量。
输入输出样例 #1
- 输入 #1
6 3 3
1 4 1 4 1
2 5 2 5 2
3 6 3 6 3- 输出 #1
4说明/提示
- 对于 的数据,。
- 对于另外 的数据,,。
- 对于另外 的数据,。
- 对于另外 的数据,。
- 对于另外 的数据,。
- 对于全部数据,,,,,,。
题解
代码
自己的:
std:
#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 ;
}