清北刷题冲刺 11-02 a.m
卖书

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 1000010
using namespace std;
int n,a[maxn],cnt1,cnt2;
int qread(){
int i=;
char ch=getchar();
while(ch<''||ch>'')ch=getchar();
while(ch<=''&&ch>=''){i=i*+ch-'';ch=getchar();}
return i;
}
int main(){
freopen("book.in","r",stdin);freopen("book.out","w",stdout);
// freopen("Cola.txt","r",stdin);
scanf("%d",&n);
for(int i=;i<=n;i++)a[i]=qread();
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 ;
}
100分 贪心
写代码

#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 1000010
using namespace std;
int n,a[maxn],s1[maxn],s2[maxn],top1,top2,m,sid1[maxn],sid2[maxn],b[maxn];
bool ex[maxn];
void out(){
for(int i=;i<=n;i++){
if(b[i]==)printf("+%d ",a[i]);
else printf("-%d ",a[i]);
}
}
int main(){
freopen("program.in","r",stdin);freopen("program.out","w",stdout);
// freopen("Cola.txt","r",stdin);
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);ex[x]=;}
for(int i=;i<=n;i++){
if(ex[i]){
top2+=;
s2[top2]=a[i];b[i]=-;
sid2[top2]=i;
while(top2){
if(s2[top2]==s1[top1]){
b[sid2[top2]]=-;
b[sid1[top1]]=;
top1-=;top2-=;
}
else{
sid2[top2+]=sid1[top1];
top2+=;
s2[top2]=s1[top1];top1-=;
}
if(top1<){puts("NO");return ;}
}
}
else{
top1+=;
s1[top1]=a[i];
sid1[top1]=i;
}
}
if(top1==&&top2==){
out();
return ;
}
while(top1){
s2[++top2]=s1[top1];
sid2[top2]=sid1[top1];top1--;
while(top2){
if(s2[top2]==s1[top1]){
b[sid2[top2]]=-;
b[sid1[top1]]=;
top1--;top2--;
}
else{
sid2[top2+]=sid1[top1];
s2[++top2]=s1[top1--];
}
if(top1<){puts("NO");return ;}
}
}
out();return ;
}
100分 括号匹配
迷宫



#include<iostream>
#include<cstdio>
#include<vector>
#define maxn 110
using namespace std;
int n,m,num,head[maxn],q,s,t,st[maxn],top;
struct node{
int to,pre,v;
}e[maxn*maxn*];
void Insert(int from,int to,int v){
e[++num].to=to;
e[num].v=v;
e[num].pre=head[from];
head[from]=num;
}
bool vis[maxn],flag;
void dfs(int now){
if(now==t&&top==){
flag=;
return;
}
if(flag)return;
vector<int>zu;
for(int i=;i<=top;i++)zu.push_back(st[i]);
for(int i=head[now];i;i=e[i].pre){
int to=e[i].to;
if(vis[to])continue;
else{
vis[to]=;
if(e[i].v==){
dfs(to);
top=;
for(int j=;j<zu.size();j++)st[++top]=zu[j];
vis[to]=;
continue;
}
for(int j=;j<=;j+=){
if(e[i].v>){
for(int k=;k<=j;k++){
top=top+;
st[top]=e[i].v;
}
dfs(to);
if(flag)return;
top=;
for(int k=;k<zu.size();k++)top=top+,st[top]=zu[k];
}
if(e[i].v<){
top-=j-;
if(st[top]!=-e[i].v||top<)break;
else top-=;
dfs(to);
top=;
for(int k=;k<zu.size();k++)st[++top]=zu[k];
}
}
vis[to]=;
}
}
}
int main(){
freopen("maze.in","r",stdin);freopen("maze.out","w",stdout);
// freopen("Cola.txt","r",stdin);
scanf("%d%d",&n,&m);
int x,y,z;
for(int i=;i<=m;i++){
scanf("%d%d%d",&x,&y,&z);
Insert(x,y,z);Insert(y,x,z);
}
scanf("%d",&q);
while(q--){
flag=;top=;
scanf("%d%d",&s,&t);
vis[s]=;
dfs(s);
vis[s]=;
if(flag)puts("YES");
else puts("NO");
}
return ;
}
5分 暴力
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
using namespace std;
int n,m,g[][][],q[**][],t,x,y,z;
int main(){
// freopen("maze.in","r",stdin);freopen("maze.out","w",stdout);
freopen("maze20.in","r",stdin);
scanf("%d%d",&n,&m);
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][],y=q[s][],sta=q[s][];
if(sta!=){
for(int i=;i<=n;i++){
if(g[i][x][sta-]==){
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][]==)puts("YES");
else puts("NO");
}
}
100分 动态规划
预计得分100++
实际得分100++
T1较简单,是以前做过的原题。T2看到函数递归就觉得应该和栈有关系,仔细研究,这不就是个括号序列吗,一开始以为要用双向链表,后来发现加个数组记录一下id,再加一个栈模拟退栈就好了,虽然和标解不太一样,但是A了。T3我思索良久,想过建分层图,但复杂度太高,正确性也很玄学,所以就写了个dfs,才搜了5分
今天的题都是关于栈的,发挥的还好,但我觉得T3的dfs应该再优化一下,尽量拿到更多部分分
小结
清北刷题冲刺 11-02 a.m的更多相关文章
- 清北刷题冲刺 11-03 a.m
纸牌 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> ...
- 2017-10-2 清北刷题冲刺班a.m
一道图论神题 (god) Time Limit:1000ms Memory Limit:128MB 题目描述 LYK有一张无向图G={V,E},这张无向图有n个点m条边组成.并且这是一张带权图,只 ...
- 2017-10-2 清北刷题冲刺班p.m
最大值 (max) Time Limit:1000ms Memory Limit:128MB 题目描述 LYK有一本书,上面有很多有趣的OI问题.今天LYK看到了这么一道题目: 这里有一个长度为n ...
- 清北刷题冲刺 11-01 p.m
轮换 #include<iostream> #include<cstdio> #include<cstring> #define maxn 1010 using n ...
- 清北刷题冲刺 10-31 a.m
集合 #include<iostream> #include<cstdio> #include<algorithm> using namespace std; ], ...
- 清北刷题冲刺 10-30 a.m
星空 #include<iostream> #include<cstdio> using namespace std; int n,m; int main(){ freopen ...
- 清北刷题冲刺 10-29 p.m
洗澡 /* 这个题不能单纯判断左括号和右括号的多少,而应该从左到右扫一遍,看应该如何配对 */ #include<iostream> #include<cstdio> #inc ...
- 清北刷题冲刺 10-28 p.m
水题(贪心) (water) Time Limit:1000ms Memory Limit:128MB 题目描述 LYK出了道水题. 这个水题是这样的:有两副牌,每副牌都有n张. 对于第一副牌的每 ...
- 清北刷题冲刺 10-28 a.m
立方数 (cubic) Time Limit:1000ms Memory Limit:128MB 题目描述 LYK定义了一个数叫“立方数”,若一个数可以被写作是一个正整数的3次方,则这个数就是立方 ...
随机推荐
- spring+mybatis的事务配置
出自:http://kinglixing.blog.51cto.com/3421535/723870 定义一个实体类:Emp.java package com.lixing.scm.entity; p ...
- 二 Django框架,urls.py模块,views.py模块,路由映射与路由分发以及逻辑处理——url控制器
Django框架,urls.py模块,views.py模块,路由映射与路由分发以及逻辑处理——url控制器 这一节主讲url控制器 一.urls.py模块 这个模块是配置路由映射的模块,当用户访问一个 ...
- 一個在WCF學習中的小教訓(本人非科班菜鳥,此經驗無參考價值,衹是自己的經驗記錄)
1.关于“ServiceHost 仅支持类服务类型”的解决: Service属性必须执行,不是接口. 改为下图所示: 解决! (注:按朱哥的方法WCF已经可以通信---截至今天的11:11(例子在 ...
- python生成excel格式座位表
脚本分两个文件: 1.生成二维随机列表:GenerateLocaltion.py 2.将列表导入excel文件:CreateExcel.py 先上GenerateLocaltion.py: impor ...
- Android之单元测试学习
1.单元测试概念 单元测试(又称为模块测试),检验程序模块(软件设计的最小单位)正确性的测试工作,常常是程序员写的一段代码.对于面向对象编程,最小单元就是方法,包括基类(超类).抽象类.或者派生类(子 ...
- FEC之我见三
继续上文讲解: 3) 标准的RTP头结构如下所示: 其中第一个字节中的x标志位是否扩展了RTP头,RTP协议允许用户自定义的扩展,扩展的字段紧挨上述RTP固定头.RTP扩展投中承载如下信息: 1).当 ...
- 每天一个linux命令(10):touch命令
版权声明更新:2017-05-14博主:LuckyAlan联系:liuwenvip163@163.com声明:吃水不忘挖井人,转载请注明出处! 1 文章介绍 本文介绍了Linux下面的mv命令. 2. ...
- 让网站变灰的CSS代码(支持IE、FIREFOX和CHROME)(转)
方法1:支持IE <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xht ...
- 【转】Pro Android学习笔记(三十):Menu(1):了解Menu
目录(?)[-] 创建Menu MenuItem的属性itemId MenuItem的属性groupId MenuItem的属性orderId MenuItem的属性可选属性 Menu触发 onOpt ...
- Webpack 基础使用
使用webstorm编译: 1.新建一个工程(最基本的工程) 2.在webstorm的控制台,使用命令行 cnpm install webpack -g 全局安装 3.安装完后,可以使用 ...