ZOJ 1016 Parencodings

Parencodings

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=16

比较简单,直接在该位置往前找,找到能匹配的,越过的右括号+1即为所求答案。

 1 //Problem Name:Parencodings
 2 //Source: ZOJ
 3 //Author: jinjin18
 4 //Main idea:
 5 //Language: C++
 6 //======================================================================
 7
 8 #include<stdio.h>
 9 #include<vector>
10
11 using namespace std;
12
13 vector<int> p;
14 vector<int> w;
15 int main(){
16     int n;
17     scanf("%d",&n);
18     while(n--){
19         p.clear();          //remember to initialize
20         w.clear();
21         int m;
22         scanf("%d",&m);
23         for(int i = 0; i < m ; i++){
24             int x;
25             scanf("%d",&x);
26             p.push_back(x);
27         }
28         w.push_back(1);
29         printf("%d",w[0]);
30         for(int i = 1; i < m ; i++){
31             int j = i - 1;
32             while(p[i]-p[j] - (i-j) < 0&&j >= 0){
33                 j--;
34             }
35             w.push_back(i-j);
36
37             printf(" %d",w[i]);
38         }
39         printf("\n");
40
41     }
42     return 0;
43 }

原文地址:https://www.cnblogs.com/jinjin-2018/p/9016788.html

时间: 2024-08-29 01:06:09

ZOJ 1016 Parencodings的相关文章

[JAVA][ZOJ 1016][Parencodings]

Let S = s1 s2 ... s2n be a well-formed string of parentheses. S can be encoded in two different ways: By an integer sequence P = p1 p2 ... pn where pi is the number of left parentheses before the ith right parenthesis in S (P-sequence). By an integer

zoj题目分类

饮水思源---zoj 转载自:http://bbs.sjtu.edu.cn/bbscon,board,ACMICPC,file,M.1084159773.A.html 注:所有不是太难的题都被归成了“简单题”,等到发现的时候已经太晚了,我太死脑筋 了……:( 有些题的程序我找不到了,555……:( SRbGa的题虽然都很经典……但是由于其中的大部分都是我看了oibh上的解题报告后做 的,所以就不写了…… 题目排列顺序没有规律……:( 按照个人感觉,最短路有的算做了DP,有的算做了图论. 有些比较

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i

【BZOJ】【1016】【JSOI2008】最小生成树计数

Kruskal/并查集+枚举 唉我还是too naive,orz Hzwer 一开始我是想:最小生成树删掉一条边,再加上一条边仍是最小生成树,那么这两条边权值必须相等,但我也可以去掉两条权值为1和3的,再加上权值为2和2的,不也满足题意吗?事实上,如果这样的话……最小生成树应该是1和2,而不是1和3或2和2!!! 所以呢?所以对于一个图来说,最小生成树有几条边权为多少的边,都是固定的!所以我们可以做一遍Kruskal找出这些边权,以及每种边权出现的次数.然后,对于每种边权,比方说出现了$v_i$

概率dp ZOJ 3640

Help Me Escape Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3640 Appoint description:  System Crawler  (2014-10-22) Description Background     If thou doest well, shalt thou not be accepted? an

zoj 2156 - Charlie&#39;s Change

题目:钱数拼凑,面值为1,5,10,25,求组成n面值的最大钱币数. 分析:dp,01背包.需要进行二进制拆分,否则TLE,利用数组记录每种硬币的个数,方便更新. 写了一个 多重背包的 O(NV)反而没有拆分快.囧,最后利用了状态压缩优化 90ms: 把 1 cents 的最后处理,其他都除以5,状态就少了5倍了. 说明:貌似我的比大黄的快.(2011-09-26 12:49). #include <stdio.h> #include <stdlib.h> #include <

ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 最小生成树 Kruskal算法

题目链接:ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 Building a Space Station Time Limit: 2 Seconds      Memory Limit: 65536 KB You are a member of the space station engineering team, and are assigned a task in the construction process of the statio

ZOJ 3607 Lazier Salesgirl (贪心)

Lazier Salesgirl Time Limit: 2 Seconds      Memory Limit: 65536 KB Kochiya Sanae is a lazy girl who makes and sells bread. She is an expert at bread making and selling. She can sell the i-th customer a piece of bread for price pi. But she is so lazy