The North American Invitational Programming Contest 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:
- 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
- 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:
- The empty string
- ABABAB where AAA and BBB are both balanced strings of parentheses
- (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
代码:
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
template <class T, class C>
using heap = priority_queue<T, vector<T>, C>;
void abc(string s, int &a, int &b, int &c)
{
a = ,
b = ,
c = s.length();
for (int i = ; i < s.length(); i++)
{
switch (s[i])
{
case '(':
a++;
break;
case ')':
if (a > )
{
a--;
}
else
{
b++;
}
}
}
}
struct triple
{
int a,
b,
c;
};
bool operator>(const triple &A, const triple &B)
{
if (A.b ^ B.b)
{
return A.b > B.b;
}
if (A.a ^ B.a)
{
return A.a < B.a;
}
return A.c < B.c;
}
bool operator<(const triple &A, const triple &B)
{
if (A.a ^ B.a)
{
return A.a > B.a;
}
if (A.b ^ B.b)
{
return A.b < B.b;
}
return A.c < B.c;
}
int main()
{
int n{};
cin >> n;
int A[], B[];
memset(A, 0xf0, sizeof(A));
memset(B, 0xf0, sizeof(B));
A[] = ;
B[] = ;
heap<triple, greater<triple>> I;
heap<triple, less<triple>> D;
for (int i = ; i <= n; i++)
{
string s;
cin >> s;
int a{}, b{}, c{};
abc(s, a, b, c);
if (a >= b)
{
I.push({a, b, c});
}
else
{
D.push({a, b, c});
}
}
while (I.size())
{
const int a = I.top().a,
b = I.top().b,
c = I.top().c;
for (int x = ; x >= max(b, a - b); x--)
{
A[x] = max(A[x], A[x - a + b] + c);
}
I.pop();
}
while (D.size())
{
const int a = D.top().a,
b = D.top().b,
c = D.top().c;
for (int x = ; x >= max(a, b - a); x--)
{
B[x] = max(B[x], B[x - b + a] + c);
}
D.pop();
}
int reponse{};
for (int x = ; x <= ; x++)
{
reponse = max(reponse, A[x] + B[x]);
}
cout << reponse << endl;
return ;
}
参考博客:http://www.cnblogs.com/JebediahKerman/p/9742462.html
The North American Invitational Programming Contest 2017 题目的更多相关文章
- The North American Invitational Programming Contest 2018 D. Missing Gnomes
A family of nn gnomes likes to line up for a group picture. Each gnome can be uniquely identified by ...
- The North American Invitational Programming Contest 2018 H. Recovery
Consider an n \times mn×m matrix of ones and zeros. For example, this 4 \times 44×4: \displaystyle \ ...
- The North American Invitational Programming Contest 2018 E. Prefix Free Code
Consider nn initial strings of lower case letters, where no initial string is a prefix of any other ...
- North American Invitational Programming Contest (NAIPC) 2017
(待补) A. Pieces of Parentheses 将括号处理完成后排序,方式参加下面的博客.然后做一遍背包即可. 2018 Multi-University Training Contest ...
- North American Invitational Programming Contest (NAIPC) 2016
(待补) A. Fancy Antiques 爆搜. B. Alternative Bracket Notation C. Greetings! D. Programming Team 0/1分数规划 ...
- North American Invitational Programming Contest 2018
A. Cut it Out! 枚举第一刀,那么之后每切一刀都会将原问题划分成两个子问题. 考虑DP,设$f[l][r]$表示$l$点顺时针一直到$r$点还未切割的最小代价,预处理出每条边的代价转移即可 ...
- 2014 ACM-ICPC Beijing Invitational Programming Contest
点击打开链接 Happy Reversal Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld J ...
- ACM International Collegiate Programming Contest, Tishreen Collegiate Programming Contest (2017)- K. Poor Ramzi -dp+记忆化搜索
ACM International Collegiate Programming Contest, Tishreen Collegiate Programming Contest (2017)- K. ...
- 训练20191007 2017-2018 ACM-ICPC Latin American Regional Programming Contest
2017-2018 ACM-ICPC Latin American Regional Programming Contest 试题地址:http://codeforces.com/gym/101889 ...
随机推荐
- Scala(三):类
类:Class 1.简单类和无参方法 2.带getter和setter属性 3.只带getter属性 4.对象私有字段 5.Bean属性 6.辅助构造器 7.主构造器 8.嵌套类 1.简单类和无参方法 ...
- 20155332 linux进程间通信(IPC)机制(未完成,待续)
linux进程间通信(IPC)机制 1.共享内存 共享内存是在多个进程之间共享内存区域的一种进程间的通信方式,由IPC为进程创建的一个特殊地址范围,它将出现在该进程的地址空间(这里的地址空间具体是哪个 ...
- Why network port is open but no process attached?(为什么端口被打开,但是没有进程号)
When I check my system today, I noticed a weird output from netstat’s output, joseph# sudo netstat - ...
- 解读Web应用程序安全性问题的本质
转自 http://blog.csdn.net/iwebsecurity/article/details/1688304 相信大家都或多或少的听过关于各种Web应用安全漏洞,诸如:跨site脚本攻击( ...
- P3704 [SDOI2017]数字表格
P3704 [SDOI2017]数字表格 链接 分析: $\ \ \ \prod\limits_{i = 1}^{n} \prod\limits_{j = 1}^{m} f[gcd(i, j)]$ $ ...
- AGC 015 E - Mr.Aoki Incubator
E - Mr.Aoki Incubator 链接 题意: 数轴上有N个黑点,每个点都有一个方向向右的正速度v.当两个点在同一个位置上重合时,若其中一个是红色,另一个也变成红色.保证没有相同速度或初始坐 ...
- [NOI2018]归程 kruskal重构树
[NOI2018]归程 LG传送门 kruskal重构树模板题. 另一篇文章里有关于kruskal重构树更详细的介绍和更板子的题目. 题意懒得说了,这题的关键在于快速找出从查询的点出发能到达的点(即经 ...
- 2_C语言中的数据类型 (七)类型限定
1.1 类型限定 1.1.1 const const是代表一个不能改变值的常量 1.1.2 volatile 代表变量是一个可能被CPU指令之外的地方改 ...
- bzoj 2238 Mst
显然先求出最小生成树 如果删的是非树边就不用管,还是能取最小生成树 如果删的是树边就有非树边可以替代它 反向考虑,每条非树边可以替代最小生成树上一条路径的边 所以树剖加线段树,对每条非树边在树上更新对 ...
- springmvc 解决@ResponseBody注解返回中文乱码
使用messageConverters <bean class="org.springframework.web.servlet.mvc.method.annotation.Reque ...