The North American Invitational Programming Contest 2017 题目

NAIPC 2017

Yin and Yang Stones

  • 75.39%
  • 1000ms
  • 262144K

A mysterious circular arrangement of black stones and white stones has appeared. Ming has been tasked with balancing the stones so that only one black and one white stone remain.

Ming has two operations for balancing the stones:

  1. Take some consecutive sequence of stones where there is exactly one more black stone than a white stone and replace the stones with a single black stone
  1. Take some consecutive sequence of stones where there is exactly one more white stone than black stone and replace the stones with a single white stone

Given a circular arrangement, determine if it is possible for Ming to balance the stones.

Input

Each input will consist of a single test case. Note that your program may be run multiple times on different inputs. The input will consist of a single string sss (1≤∣s∣≤105)(1 \le |s| \le 10^5)(1≤∣s∣≤105), with only the characters capital ‘BBB’ and ‘WWW’. The stones are arranged in a circle, so the first stone and the last stone are adjacent.

Output

Output 111 if it is possible for Ming to balance the stones with his rules. Otherwise, output 000.

样例输入1

WWBWBB

样例输出1

1

样例输入2

WWWWBBW

样例输出2

0

样例输入3

WBBBBBWWBW

样例输出3

0

题目来源

The North American Invitational Programming Contest 2017

思路:W与B相同输出1,否则输出0。只有这样才能保持黑白平衡。

Pieces of Parentheses

  • 22.03%
  • 1000ms
  • 262144K

You are teaching a class in programming, and you want to cover balanced parentheses. You’ve got a great visual aid, a sign with a very long, balanced string of parentheses. But, alas, somehow, your visual aid has been broken into pieces, and some pieces may be missing! You’ve got to try to put it back together as best you can. Given the string of parentheses on each piece, what is the longest balanced string you can form by concatenating some of them in some order? Each piece may be used at most once, and the pieces cannot be reversed.

A balanced string of parentheses is defined as:

  1. The empty string
  1. ABABAB where AAA and BBB are both balanced strings of parentheses
  1. (A)(A)(A) where AAA is a balanced string of parentheses

Input

Each input will consist of a single test case. Note that your program may be run multiple times on different inputs. The first line of input will contain a single integer n(1≤n≤300)n (1 \le n \le 300)n(1≤n≤300), which is the number of pieces.

Each of the next nnn lines will hold a single string s(1≤∣s∣≤300)s (1 \le |s| \le 300)s(1≤∣s∣≤300), which consists only of the characters ’(((’ and ’)))’. This describes one of the pieces.

Output

Output a single integer, which is the length of the longest string of balanced parentheses you can form from the pieces. Note that the empty string is technically a balanced string of parentheses, so it is always possible to form a string of length at least 000 (although the empty string is not a very effective visual aid!).

样例输入1

3
())
((()
)()

样例输出1

10

样例输入2

5
)))))
)
((
))((
(

样例输出2

2

题目来源

The North American Invitational Programming Contest 2017

代码:

  1 #include <iostream>
  2 #include <cstring>
  3 #include <queue>
  4 using namespace std;
  5 template <class T, class C>
  6 using heap = priority_queue<T, vector<T>, C>;
  7 void abc(string s, int &a, int &b, int &c)
  8 {
  9     a = 0,
 10     b = 0,
 11     c = s.length();
 12     for (int i = 0; i < s.length(); i++)
 13     {
 14         switch (s[i])
 15         {
 16         case ‘(‘:
 17             a++;
 18             break;
 19         case ‘)‘:
 20             if (a > 0)
 21             {
 22                 a--;
 23             }
 24             else
 25             {
 26                 b++;
 27             }
 28         }
 29     }
 30 }
 31 struct triple
 32 {
 33     int a,
 34         b,
 35         c;
 36 };
 37 bool operator>(const triple &A, const triple &B)
 38 {
 39     if (A.b ^ B.b)
 40     {
 41         return A.b > B.b;
 42     }
 43     if (A.a ^ B.a)
 44     {
 45         return A.a < B.a;
 46     }
 47     return A.c < B.c;
 48 }
 49 bool operator<(const triple &A, const triple &B)
 50 {
 51     if (A.a ^ B.a)
 52     {
 53         return A.a > B.a;
 54     }
 55     if (A.b ^ B.b)
 56     {
 57         return A.b < B.b;
 58     }
 59     return A.c < B.c;
 60 }
 61 int main()
 62 {
 63     int n{0};
 64     cin >> n;
 65     int A[90001], B[90001];
 66     memset(A, 0xf0, sizeof(A));
 67     memset(B, 0xf0, sizeof(B));
 68     A[0] = 0;
 69     B[0] = 0;
 70     heap<triple, greater<triple>> I;
 71     heap<triple, less<triple>> D;
 72     for (int i = 1; i <= n; i++)
 73     {
 74         string s;
 75         cin >> s;
 76         int a{0}, b{0}, c{0};
 77         abc(s, a, b, c);
 78         if (a >= b)
 79         {
 80             I.push({a, b, c});
 81         }
 82         else
 83         {
 84             D.push({a, b, c});
 85         }
 86     }
 87     while (I.size())
 88     {
 89         const int a = I.top().a,
 90                   b = I.top().b,
 91                   c = I.top().c;
 92         for (int x = 90000; x >= max(b, a - b); x--)
 93         {
 94             A[x] = max(A[x], A[x - a + b] + c);
 95         }
 96         I.pop();
 97     }
 98     while (D.size())
 99     {
100         const int a = D.top().a,
101                   b = D.top().b,
102                   c = D.top().c;
103         for (int x = 90000; x >= max(a, b - a); x--)
104         {
105             B[x] = max(B[x], B[x - b + a] + c);
106         }
107         D.pop();
108     }
109     int reponse{0};
110     for (int x = 0; x <= 90000; x++)
111     {
112         reponse = max(reponse, A[x] + B[x]);
113     }
114     cout << reponse << endl;
115     return 0;
116 }

参考博客:http://www.cnblogs.com/JebediahKerman/p/9742462.html

原文地址:https://www.cnblogs.com/weixq351/p/9742759.html

时间: 2024-08-27 06:26:06

The North American Invitational Programming Contest 2017 题目的相关文章

North American Invitational Programming Contest 2018

题目链接:https://nanti.jisuanke.com/?kw=The%20North%20American%20Invitational%20Programming%20Contest%202018 本博客参考了(抄了)巨佬Claris的博客 https://www.cnblogs.com/clrs97/p/8730429.html,但是因为Claris太巨了,每道题就几句话完事了,我结合自己的理解补充了一下,部分由于自己的菜,暂时还不会,留着以后补. A-Cut it out 本题是

训练20191007 2017-2018 ACM-ICPC Latin American Regional Programming Contest

2017-2018 ACM-ICPC Latin American Regional Programming Contest 试题地址:http://codeforces.com/gym/101889 总体情况 总共通过7题CEFGHIJ.其中我完成HIJ三题.纯属被大佬带飞系列. 解题报告 H - Hard choice 签到题 #include <bits/stdc++.h> using namespace std; int a1,b1,c1,a2,b2,c2; int main() {

2017-2018 ACM-ICPC Latin American Regional Programming Contest

题面pdfhttps://codeforc.es/gym/101889/attachments/download/7471/statements-2017-latam-regional.pdf zyn感冒,两个人打.刚开始两题超迅速,40分钟后开始各种写不出,自闭.然后突然又开出两题. 4题全部1A,70名左右应该能稳个银. 说明卡题了可千万不能放弃!虽然可能简单题做不出来,过的多的题目做不出来,也不要放弃去开题. 以及周末这两场都说明一定要胆子大. B---Buggy ICPC[找规律] 题意

Egyptian Collegiate Programming Contest 2017 (ACM ECPC 2017) - original tests edition

题目链接:http://codeforces.com/gym/101856 看另一个榜的话,应该E也是可以试试的. 主要差在A.E.F. L - Lazy ERCD 签到题 K -Katryoshka 签到题2号,zf做的,不知道怎么考虑. 给一堆a,b,c,可以用(2,0,1),(2,1,1),(1,1,1)三种组合组合出一个物品,求尽可能组合出最多的物品的数量. 很显然第二种不好,zf说第三种可以合并到第一种,大概是这个意思: 固定消耗一个a和一个c,在这基础上每多一个a或者多一个b都可以组

2020.04.06 UCF Local Programming Contest 2017

E题:Opposites Attract 题目链接:https://nanti.jisuanke.com/t/44821 题目大意: 题目明确了圆盘上各块的得分,给出m(t)个点,问这些点在圆盘上的分数总和. 思路: 一个2π的圆,可以想到用弧度来确定点的位置(一开始做的时候用的角度,却WA了),弧度就是atan(x,y)  如果再除以 π,就可以简化,注意单独讨论点在y轴的情况. 解题代码: 1 #include <cstdio> 2 #include <iostream> 3

UCF Local Programming Contest 2017(2020-04-06)

原题地址:https://www.jisuanke.com/contest/7195?view=challenges A. Electric Bill 题意:分级收费,用电1000以下一档,以上一档,问应支付多少钱 AC代码: #include<iostream> #include<cstring> #include<algorithm> using namespace std; int main(){ int a,b,n,temp; cin>>a>&

2017-2018 ACM-ICPC Latin American Regional Programming Contest Solution

A - Arranging tiles 留坑. B - Buggy ICPC 题意:给出一个字符串,然后有两条规则,如果打出一个辅音字母,直接接在原字符串后面,如果打出一个元音字母,那么接在原来的字符串后面之后再翻转整个字符串,在这两条规则之下,求有多少种打印给定字符串的方法 思路:如果第一个字符是辅音,那么答案为0 如果全是辅音或全是元音,那么答案为1 如果只有一个辅音,答案为len 否则是最中间两个元音中间的辅音字符个数+1 1 #include <bits/stdc++.h> 2 3 u

2017-2018 ACM-ICPC Latin American Regional Programming Contest GYM101889

挺有意思的一套题,题也没有啥毒瘤了,本来是队切的结果种种原因大家全挂机了. 只补了百人题,一共7个,其他的暂时先不补了,,也不会嘛qwq H:签到 1 #include <bits/stdc++.h> 2 using namespace std; 3 int a[18],b[18]; 4 int main(){ 5 cin>>a[1]>>a[2]>>a[3]>>b[1]>>b[2]>>b[3]; 6 int ans = 0

2019-2020 ACM-ICPC Latin American Regional Programming Contest L - Leverage MDT

#include<map> #include<queue> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #define LL long long const int N=1010; int val[N][N]; int res[N][N]; char c[N][N]; int n,m; bo