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 ...
随机推荐
- 关于Linux的交叉编译环境配置中的问题
Linux的交叉编译arm-linux-gcc搭建时,安装结束却无法查看版本.输入以下命令查看Ubuntu的版本: uname -a 可以看到此Ubuntu为64位16.04.1版本,所以需要下载32 ...
- 针对于网络安全领域中基于PCAP流量的数据集
网络安全领域中基于PCAP流量的数据集 MAWI Working Group Traffic Archive URL:http://mawi.wide.ad.jp/mawi/ CIC dataset ...
- SQL用法总全
https://www.yiibai.com/sql 比较全面 事务就是对数据库执行的工作单元.事务是完成逻辑顺序的工作,无论是在手动方式由用户或者自动地通过某种数据库程序的序列的单元. 事务是一个或 ...
- net辅助工具列表
最近对.net的辅助工具比较感兴趣,网上也已经有.net的十个必备工具的帖子了,这里提供了一份全面的工具列表: Code generation NVelocity CodeSmith X-Code . ...
- 20145234黄斐《Java程序设计》实验五—网络安全与编程
1: 两人一组结对编程: 0. 参考http://www.cnblogs.com/rocedu/p/6766748.html#SECDSA 1. 结对实现中缀表达式转后缀表达式的功能 MyBC.jav ...
- 在CMD 中,如何切换python2.x 版本 和 python3.x版本?
1.cmd中输入python,启动python2.x版本,如何切换呢? 2.找到python2.x安装路径,修改python.exe名称为:python27.exe 3. 打开cmd,输入python ...
- 洛谷 P4026 [SHOI2008]循环的债务
水水的dp 设f[i][a][b]表示交换完前i种面值的钞票,第一个人有a元,第二个人有b元的最小代价 直接转移就行了 需要注意的是算的式子 第1个人\(\Delta A\),第二个人\(\Delta ...
- 【BZOJ3555】企鹅QQ
蛤希. 用map会T. 只需要枚举删掉哪个字符,然后算出每个的hash值,sort一遍就行了. 用map会T!!! // It is made by XZZ #include<cstdio> ...
- JavaScript函数作用域与对象以及实用技巧
1. JS作用域 1.1 全局作用域和局部作用域 函数外面声明的就是 全局作用域 函数内是局部作用域 全局变量可以直接在函数内修改和使用 变量,使用var是声明,没有var是使用变量. 如果在函数内使 ...
- [Java] Design Pattern:Code Shape - manage your code shape
[Java] Design Pattern:Code Shape - manage your code shape Code Shape Design Pattern Here I will intr ...