贪心题是很有趣的...

首先,本题为括号匹配问题,那么可以考虑进行栈模拟

然后,我们思考一下如何匹配:虽然题目中仅对右括号的位置提出了区域性要求,但可以发现,对能匹配上的栈顶括号立刻进行匹配一定是一种最优解!

为什么?

根据括号匹配原则,如果栈顶括号未被匹配,那么其他括号将无法被匹配,那么栈顶括号越长时间不被匹配,栈内括号失配的可能就越大。相反,如果我们对能匹配的栈顶括号立刻进行匹配,不会存在反例使得本能匹配上的其他括号未能被匹配。

所以这题的剩余部分就变成了一个模拟...

#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
int l[605],r[605];
int n;
int my_stack[605];
int posi[605];
int ttop=0;
int typ[1205];
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d%d",&l[i],&r[i]);
}
int pos=2;
int cnt=1;
typ[1]=1;
my_stack[++ttop]=1;
posi[ttop]=1;
while(pos<=2*n)
{
int t=my_stack[ttop];
int p=posi[ttop];
if(p+l[t]<=pos&&p+r[t]>=pos)
{
typ[pos]=2;
ttop--;
pos++;
}else
{
my_stack[++ttop]=++cnt;
posi[ttop]=pos;
typ[pos]=1;
pos++;
}
}
if(ttop)
{
printf("IMPOSSIBLE\n");
return 0;
}else
{
for(int i=1;i<=2*n;i++)
{
if(typ[i]==1)
{
printf("(");
}else
{
printf(")");
}
}
printf("\n");
}
return 0;
}

CF508E的更多相关文章

  1. CF508E Arthur and Brackets

    题目大意:给出n对括号,并给出每对括号距离的范围.问能否找到这样一个序列. 题解:好多人都用贪心.这么好的题为什么不搜一发呢? 注意:千万不要在dfs里面更新答案. 代码: #include<c ...

  2. CF508E (贪心+搜索+构造)

    题目大意:让你构造一个括号序列,括号匹配的方式类似于栈,给出从左数每个括号 到和它匹配的右括号的 最小和最大距离,让你输出一个合法括号序列 看错题了以为是二分图,然后写了搜索 贪心发现如果距离往小了填 ...

  3. 2021record

    2021-10-14 P2577 [ZJOI2004]午餐 2021-10-13 CF815C Karen and Supermarket(小小紫题,可笑可笑) P6748 『MdOI R3』Fall ...

  4. 遇到括号就是栈(bushi)

    CF508E Arthur and Brackets 我在赛场上想都没想直接DP \(O(n^3)\)过了 但别人说正解是栈+贪心 讲讲DP \(bool\) \(dp[i][j]\)表示从第i对括号 ...

随机推荐

  1. react踩坑记录——使用fetch获取json数据报错

    报错: 原因其实是list.json文件路径错误,该文件路径是相对于index.html的,而不是App.js或者index.js.

  2. no plugin found for prefix 'tomcat 7' in the current project

    使用maven build编译出错 “no plugin found for prefix 'tomcat 7' in the current project..........” 参照下面方法 ht ...

  3. [Kubernetes]关于K8s,你应该知道的一些东西

    Kubernetes概述 Kubernetes(也常称K8s,用8代替8个字符"ubernete"而成的缩写),是一个开源的,用于管理云平台中多个主机上的容器化应用. 它的一个核心 ...

  4. GPU Tips

    <1> Basic #include <stdio.h> #include <cuda_runtime.h> #include <device_launch_ ...

  5. Python-列表的常用操作

    #先定义一个列表 names = ["ZhangXueyou","LiMing","GuoFucheng","LiuDehua&q ...

  6. k64 datasheet学习笔记45---10/100-Mbps Ethernet MAC(ENET)之功能描述

    1.前言 本文是对K64 datasheet 之ENET部分的功能描述,将对每个部分进行详细说明 2.Ethernet MAC frame formats MAC帧组成格式 (1)7字节前导码:如按最 ...

  7. MySQL主从复制报错1594处理【转】

    一.问题描述 Mysql主从复制模式中,slave上报错 “relay log read failure”,导致主从同步停止. mysql> show slave status\G ****** ...

  8. 009_关闭linux的THP

    背景:公司某个大型业务系统反馈最近数据库服务器总是宕机(此处描述不准确,后面解释),最后,客户.运维人员都觉得实在是忍无可忍了,项目经理打电话找到我问是否能帮忙诊断一下,刚好第二天要去现场沟通另外一个 ...

  9. webservice:com.sun.xml.internal.ws.server.ServerRtException: [failed to localize]

    发布webservice发生了错误,一直没有能够解决,错误如下: Exception in thread "main" com.sun.xml.internal.ws.server ...

  10. 【原创】大叔经验分享(36)CM部署kafka

    1 下载kafka parcel http://archive.cloudera.com/kafka/parcels/latest/KAFKA-3.1.1-1.3.1.1.p0.2-el7.parce ...