卖书

#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的更多相关文章

  1. 清北刷题冲刺 11-03 a.m

    纸牌 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> ...

  2. 2017-10-2 清北刷题冲刺班a.m

    一道图论神题 (god) Time Limit:1000ms   Memory Limit:128MB 题目描述 LYK有一张无向图G={V,E},这张无向图有n个点m条边组成.并且这是一张带权图,只 ...

  3. 2017-10-2 清北刷题冲刺班p.m

    最大值 (max) Time Limit:1000ms   Memory Limit:128MB 题目描述 LYK有一本书,上面有很多有趣的OI问题.今天LYK看到了这么一道题目: 这里有一个长度为n ...

  4. 清北刷题冲刺 11-01 p.m

    轮换 #include<iostream> #include<cstdio> #include<cstring> #define maxn 1010 using n ...

  5. 清北刷题冲刺 10-31 a.m

    集合 #include<iostream> #include<cstdio> #include<algorithm> using namespace std; ], ...

  6. 清北刷题冲刺 10-30 a.m

    星空 #include<iostream> #include<cstdio> using namespace std; int n,m; int main(){ freopen ...

  7. 清北刷题冲刺 10-29 p.m

    洗澡 /* 这个题不能单纯判断左括号和右括号的多少,而应该从左到右扫一遍,看应该如何配对 */ #include<iostream> #include<cstdio> #inc ...

  8. 清北刷题冲刺 10-28 p.m

    水题(贪心) (water) Time Limit:1000ms   Memory Limit:128MB 题目描述 LYK出了道水题. 这个水题是这样的:有两副牌,每副牌都有n张. 对于第一副牌的每 ...

  9. 清北刷题冲刺 10-28 a.m

    立方数 (cubic) Time Limit:1000ms   Memory Limit:128MB 题目描述 LYK定义了一个数叫“立方数”,若一个数可以被写作是一个正整数的3次方,则这个数就是立方 ...

随机推荐

  1. cf 429B Working out(简单dp)

    B. Working out time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  2. Spring4自动装配(default-autowire) (转)

    原文地址:http://blog.csdn.net/conglinyu/article/details/63684957 Spring 自动装配 通过配置default-autowire 属性,Spr ...

  3. C#异步编程之浅谈Task

    上一篇讲到了.Net4.5新增的async和await关键字,其实async和await算是一组标记,真正实现异步操作的是Task新开的任务线程. 什么是Task Task是.Net4.0新增用来处理 ...

  4. Linux_服务器_08_网卡eth1修改为eth0

    一.现象 二.解决步骤 1.修改 70-persistent-net.rules 执行命令: vim /etc/udev/rules.d/-persistent-net.rules 找到与ifconf ...

  5. 如何在MySQl数据库中给已有的数据表添加自增ID?

    由于使用MySQL数据库还没有多久的缘故,在搭建后台往数据库导入数据的时候发现新增的表单是没有自增id的,因次就有了上面这个问题. 解决方法 1.给某一张表先增加一个字段,这里我们就以node_tab ...

  6. encodeURI,encodeURIComponent编码

    encodeURI().encodeURIComponent().decodeURI().decodeURIComponent() URL编码 Global对象的encodeURI()和encodeU ...

  7. bzoj 3671 随机数生成器 —— 暴力

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3671 原来256M是可以开两个3e7的数组的: 因为答案只有 n+m-1 个数,所以暴力判断 ...

  8. centos 6 rsync+inotify 实时同步

    主机名.ip: server 172.31.82.184 client  172.31.82.185 需求: 1.server端 ”/data/server“ 做为client端 “/data/cli ...

  9. Java数组的基本讲解

    由于Java数组涵盖的内容比较多,这里从个人的角度对主要的内容进行相关的讲解. 如有不足,欢迎批评指正~ 1)Java数组是动态还是静态的啊?     Java语言是典型的静态语言,由此推断Java数 ...

  10. varnish安装和配置

    实验环境:CentOS7 Varnish是高性能开源的反向代理服务器和HTTP缓存服务器. #varnish服务器:172.16.252.142 [root@varnish localhost]#yu ...