卖书

#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应该再优化一下,尽量拿到更多部分分

小结

最新文章

  1. UTFGrid
  2. Openfire 编译插件
  3. POJ 3903
  4. Window 下 Qt5 使用QMediaplayer 进行视频播放 流播放问题
  5. 如何创建多个Memcached服务
  6. 实现关闭窗口IE不提示兼容火狐
  7. 【BZOJ】【1003】【ZJOI2006】物流运输trans
  8. linux 一些遇到的问题
  9. Android USB Host与HID通讯 (一)
  10. 2014.3.6-C语言学习小结
  11. 微型orm框架--dapper的简单使用
  12. 201521123118《java与程序设计》第14周学习总结
  13. this的四种绑定形式
  14. JAVA学习总结-多线程基础:
  15. BZOJ5019[Snoi2017]遗失的答案——FWT+状压DP
  16. 指针数组&amp;数组指针
  17. 【BZOJ4316】小C的独立集(动态规划)
  18. 如何在代码中减少if else语句的使用
  19. 类和类的关系——java
  20. java获取某个范围内的一个随机数

热门文章

  1. 分享知识-快乐自己:JS 检查元素是否含有某种css样式
  2. linux应用之vim的安装与配置(centos)
  3. codeforces 622D D. Optimal Number Permutation(找规律)
  4. 20179203李鹏举 《Linux内核原理与分析》第一周学习笔记
  5. vue-router路由嵌套的使用
  6. [转]各种开源协议介绍 BSD、Apache Licence、GPL V2 、GPL V3 、LGPL、MIT
  7. freeMaker的工具类
  8. phpMailer在CentOS 6.5下无法发送邮件的解决办法
  9. Ruby中的include
  10. Java enum(枚举)使用详解之三