清北考前刷题day6早安
2024-08-25 14:47:19
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm> #define N 1000010 using namespace std;
int n,a[N],cnt1,cnt2;
int read()
{
int x=,f=;char c=getchar();
while(c>''||c<''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return ;
}
int main()
{
scanf("%d",&n);
for(int i=; i<=n; i++)a[i]=read();
for(int i=; i<=n; i++)
{
if(a[i]==)cnt1++;
if(a[i]==)
{
if(cnt1==)
{
puts("NO");return ;
}
else cnt2++,cnt1--;
}
if(a[i]==)
{
if(cnt1==||(cnt2==&&cnt1<))
{
puts("NO");
return ;
}
if(cnt2) cnt2--,cnt1--;
else cnt1-=;
}
}
puts("YES");
return ;
}
/*
贪心
反过来括号匹配。
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
''
using namespace std;
const int maxn = ;
int p = ;
int z[maxn],a[maxn],n,m; int main()
{
scanf("%d",&n);
for (int i = ; i<=n; i++)
scanf("%d",&a[i]);
scanf("%d",&m);
int x;
for (int i = ; i<=m; i++)
{
scanf("%d",&x);
if (a[x]>) a[x] = -a[x];
}
for (int x = n; x>=; x--)
{
if (a[x]>)
{
if (z[p] == -a[x]) p--;
else
{
a[x] = -a[x];
p++;
z[p]= a[x];
}
}
else
{
p++;z[p]=a[x];
}
}
if (p==)
{
for (int i = ; i<=n; i++)
{
if (a[i]>) printf("+%d ",a[i]);
else printf("%d ",a[i]);
}
}
else printf("NO\n");
return ;
}
/*
Floyed
F[i][j][k]表示从i到j能否以k的方式到达。
K = 括号完全匹配 或者 缺少一个右括号
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
using namespace std;
int n,m;
int g[][][],q[**][],t,x,y,z;
int main()
{
freopen("maze.in","r",stdin);
freopen("maze.out","w",stdout);
scanf("%d%d",&n,&m);
memset(g,,sizeof(g));
t = ;
for (int i = ; i<=m; i++)
{
scanf("%d%d%d",&x,&y,&z);
if (z!=)
{
if (z<) z = abs(z);
else z+=;
g[x][y][z] = ;
g[y][x][z] = ;
}
else
{
g[x][y][z] = ;
g[y][x][z] = ;
t++;
q[t][] = x;
q[t][] = y;
q[t][] = ;
t++;
q[t][] = y;
q[t][] = x;
q[t][] = ;
}
}
for (int i =; i<=n; i++)
{
g[i][i][] = ;
t++;
q[t][] = i;
q[t][] = i;
q[t][] = ;
}
for (int s = ; s<=t; s++)
{
int x = q[s][];
int y = q[s][];
int status = q[s][];
if (status!=)
{
for (int i = ; i<=n; i++)
if (g[i][x][status-] == )
{
if (g[i][y][] ==)
{
g[i][y][] = ;
t++;
q[t][] = i;
q[t][] = y;
q[t][] = ;
}
}
}
else
{
for (int i =; i<=n; i++)
{
if (g[i][x][] == && g[i][y][] == )
{
g[i][y][] =;
t++;
q[t][]= i;
q[t][] = y;
q[t][]= ;
}
if (g[y][i][] == && g[x][i][] ==)
{
g[x][i][] = ;
t++;
q[t][] = x;
q[t][] = i;
q[t][]= ;
}
for (int j = ; j<=; j ++)
{
if (g[y][i][j] == && g[x][i][j+] == )
{
g[x][i][j+] = ;
t++;
q[t][] = x;
q[t][] = i;
q[t][] = j+;
}
}
}
}
}
int tt = ;
scanf("%d",&tt);
for (; tt; tt--)
{
scanf("%d%d",&x,&y);
if (g[x][y][] ==) printf("YES\n");
else printf("NO\n");
}
}
最新文章
- 《挑战程序设计竞赛》 4.1.1 矩阵 P286
- Linux配置静态IP
- jQuery父级以及同级元素查找介绍
- maven:安装m2eclipse插件
- webpack和webpack-dev-server的区别
- tensorflow学习笔记2:c++程序静态链接tensorflow库加载模型文件
- 深入理解Major GC, Full GC, CMS
- 使用dd备份和恢复ASM中的数据文件头
- Linux 小知识翻译 - 「别名」
- tcpdump -i eth0 -n -vvv src or dst port 443
- centos 打包报错Can&#39;t connect to X11 window server using &#39;:0.0&#39; as the value of the DISPLAY variable.
- 【Excel】SUMIF的错位问题
- [SoapUI]怎样运用Schema通过*.xsd文件来验证response对应的xml文件
- linux shell scripts:Syntax error: Bad for loop variable
- UVa 11149 矩阵的幂(矩阵倍增法模板题)
- Yii2之发送电子邮件
- .apache.commons.io 源代码学习(二)FilenameUtils类
- iOS资讯详情页实现—WebView和TableView混合使用(转)
- UI:SQL语句
- 人脸识别(初学篇)-VS2015+opencv3.2的配置