C. Bracket Sequences Concatenation Problem
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

A bracket sequence is a string containing only characters "(" and ")".

A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, bracket sequences "()()", "(())" are regular (the resulting expressions are: "(1)+(1)", "((1+1)+1)"), and ")(" and "(" are not.

You are given nn bracket sequences s1,s2,…,sns1,s2,…,sn. Calculate the number of pairs i,j(1≤i,j≤n)i,j(1≤i,j≤n) such that the bracket sequence si+sjsi+sj is a regular bracket sequence. Operation ++ means concatenation i.e. "()(" + ")()" = "()()()".

If si+sjsi+sj and sj+sisj+si are regular bracket sequences and i≠ji≠j, then both pairs (i,j)(i,j) and (j,i)(j,i) must be counted in the answer. Also, if si+sisi+si is a regular bracket sequence, the pair (i,i)(i,i) must be counted in the answer.

Input

The first line contains one integer n(1≤n≤3⋅105)n(1≤n≤3⋅105) — the number of bracket sequences. The following nn lines contain bracket sequences — non-empty strings consisting only of characters "(" and ")". The sum of lengths of all bracket sequences does not exceed 3⋅1053⋅105.

Output

In the single line print a single integer — the number of pairs i,j(1≤i,j≤n)i,j(1≤i,j≤n) such that the bracket sequence si+sjsi+sj is a regular bracket sequence.

Examples
input

Copy
3
)
()
(
output

Copy
2
input

Copy
2
()
()
output

Copy
4
Note

In the first example, suitable pairs are (3,1)(3,1) and (2,2)(2,2).

In the second example, any pair is suitable, namely (1,1),(1,2),(2,1),(2,2)(1,1),(1,2),(2,1),(2,2).

http://codeforces.com/contest/990/problem/C

题意 n个括号字符串 有多少种组合把两个字符串连起来之后还是合法字符串   若(i,j)(j,i) 都合法 且i != j 算两种不同组合 反之算一种

解析  对每个字符串的未能在本字符串匹配的左右括号进行统计 比如(())  左右都是0 (() 右是1  左使0 )((( 右是3 左是1   再根据左右括号数量匹配

对于一个字符串只有左右括号至少有一个为零的才能进行匹配

1 左括号为零的和右括号为零进行匹配

2 左右括号都为零的进行匹配

AC代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e5+,mod = ,inf=0x3f3f3f3f;
typedef long long ll;
struct node
{
int l,r;
}a[maxn];
char c[maxn];
int main()
{
map<int,int> ml,mr;
int n;
ll m=;
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%s",c);
int len=strlen(c);
a[i].l=a[i].r=;
for(int j=;j<len;j++)
{
//cout<<i<<" "<<c[i]<<endl;
if(c[j]=='(')
a[i].l++;
else if(c[j]==')'&&a[i].l==)
a[i].r++;
else
a[i].l--;
}
}
for(int i=;i<n;i++)
{
if(a[i].l==&&a[i].r==)
m++;
else if(a[i].l==)
mr[a[i].r]++;
else if(a[i].r==)
ml[a[i].l]++;
}
ll ans=;
for(auto it=ml.begin();it!=ml.end();it++)
{
int u=it->first;
// cout<<ans<<" "<<u<<endl;
if(mr[u])
ans+=(ll)ml[u]*mr[u];
}
cout<<ans+m*(m-)+m<<endl;
}
D. Graph And Its Complement
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Given three numbers n,a,bn,a,b. You need to find an adjacency matrix of such an undirected graph that the number of components in it is equal to aa, and the number of components in its complement is bb. The matrix must be symmetric, and all digits on the main diagonal must be zeroes.

In an undirected graph loops (edges from a vertex to itself) are not allowed. It can be at most one edge between a pair of vertices.

The adjacency matrix of an undirected graph is a square matrix of size nn consisting only of "0" and "1", where nn is the number of vertices of the graph and the ii-th row and the ii-th column correspond to the ii-th vertex of the graph. The cell (i,j)(i,j) of the adjacency matrix contains 11if and only if the ii-th and jj-th vertices in the graph are connected by an edge.

A connected component is a set of vertices XX such that for every two vertices from this set there exists at least one path in the graph connecting this pair of vertices, but adding any other vertex to XX violates this rule.

The complement or inverse of a graph GG is a graph HH on the same vertices such that two distinct vertices of HH are adjacent if and only if they are not adjacent in GG.

Input

In a single line, three numbers are given n,a,b(1≤n≤1000,1≤a,b≤n)n,a,b(1≤n≤1000,1≤a,b≤n): is the number of vertexes of the graph, the required number of connectivity components in it, and the required amount of the connectivity component in it's complement.

Output

If there is no graph that satisfies these constraints on a single line, print "NO" (without quotes).

Otherwise, on the first line, print "YES"(without quotes). In each of the next nn lines, output nn digits such that jj-th digit of ii-th line must be 11if and only if there is an edge between vertices ii and jj in GG (and 00 otherwise). Note that the matrix must be symmetric, and all digits on the main diagonal must be zeroes.

If there are several matrices that satisfy the conditions — output any of them.

Examples
input

Copy
3 1 2
output

Copy
YES
001
001
110
input

Copy
3 3 3
output

Copy
NO
http://codeforces.com/contest/990/problem/D题意 n个点的无向图 为是否存在某种 原图的连通分量为a 其补图的连通分量为b (且该原图的邻接矩阵是对称的 没有自环 其实只是个没用的条件 因为 无向图的邻接矩肯定是对称的)
解析 思考一下 就可以得出 a和b 至少要有一个为1 且a==1&&b==1时 要特判 尤其是1 被坑了一手 AC代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e3+,mod = ,inf=0x3f3f3f3f;
typedef long long ll;
int g[maxn][maxn];
int main()
{
int n,a,b;
scanf("%d %d %d",&n,&a,&b);
if(a!=&&b!=)
{
cout<<"NO"<<endl;
}
else if(a==&&b==)
{
if(n==||n==)
cout<<"NO"<<endl;
else
{
for(int i=;i<=n;i++)
{
g[i][i+]=;
g[i+][i]=;
}
cout<<"YES"<<endl;
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
printf("%d",g[i][j]);
printf("\n");
}
}
}
else
{
int temp=max(a,b)-;
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if(i==j)
g[i][j]=;
else if(i<=temp||j<=temp)
g[i][j]=;
else
g[i][j]=;
}
}
if(a==)
{
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(i==j)
g[i][j]=;
else
g[i][j]=!g[i][j];
}
printf("YES\n");
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
printf("%d",g[i][j]);
printf("\n");
}
}
}

Educational Codeforces Round 45 (Rated for Div. 2) C、D的更多相关文章

  1. Educational Codeforces Round 45 (Rated for Div. 2) E - Post Lamps

    E - Post Lamps 思路:一开始看错题,以为一个地方不能重复覆盖,我一想值这不是sb题吗,直接每个power check一下就好....复杂度nlogn 然后发现不是,这样的话,对于每个po ...

  2. Educational Codeforces Round 45 (Rated for Div. 2) G - GCD Counting

    G - GCD Counting 思路:我猜测了一下gcd的个数不会很多,然后我就用dfs回溯的时候用map暴力合并就好啦. 终判被卡了MLE.....  需要每次清空一下子树的map... #inc ...

  3. Educational Codeforces Round 45 (Rated for Div. 2) F - Flow Control

    F - Flow Control 给你一个有向图,要求你给每条边设置流量,使得所有点的流量符合题目给出的要求. 思路:只有在所有点的流量和为0时有解,因为增加一条边的值不会改变所有点的总流量和, 所以 ...

  4. Educational Codeforces Round 45 (Rated for Div. 2)

    A bracket sequence is a string containing only characters "(" and ")". A regular ...

  5. Educational Codeforces Round 92 (Rated for Div. 2) B、C题解

    TAT 第一场codeforces B. Array Walk #暴力 #贪心 题目链接 题意 有\(a1, a2, ..., an\) 个格子(每个格子有各自分数),最初为1号格(初始分数为\(a1 ...

  6. Educational Codeforces Round 88 (Rated for Div. 2) B、New Theatre Square C、Mixing Water

    题目链接:B.New Theatre Square 题意: 你要把所有"." 都变成"*",你可以有两个选择,第一种就是一次铺一个方块(1*1),第二种就是同一 ...

  7. Educational Codeforces Round 88 (Rated for Div. 2) E、Modular Stability 逆元+思维

    题目链接:E.Modular Stability 题意: 给你一个n数,一个k,在1,2,3...n里挑选k个数,使得对于任意非负整数x,对于这k个数的任何排列顺序,然后用x对这个排列一次取模,如果最 ...

  8. Educational Codeforces Round 88 (Rated for Div. 2) D、Yet Another Yet Another Task

    题意: 给你一个含n个数a1,a2...an的数组,你要找到一个区间[l,r],使得al+a(l+1)+...+a(r-1)+ar减去max(al,a(l+1),...,a(r-1),ar)的值尽可能 ...

  9. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

随机推荐

  1. [Luogu1345][USACO5.4]Telecowmunication 最大流

    题目链接:https://www.luogu.org/problem/show?pid=1345 求最小割点集的大小,直接拆点转化成最小割边.把一个点拆成出点入点,入点向出点连一条容量为1的边,其他的 ...

  2. 一个完整的http请求分析

    Request URL:http://localhost:8080/test.jhtmlRequest Method:POSTStatus Code:200 OKRemote Address:[::1 ...

  3. 绿化VSCode

    通过啃源码, 终于找到了解决办法, 设置环境变量: VSCODE_APPDATA=C:\Program Files\VSCode\UserData VSCODE_EXTENSIONS=%VSCODE_ ...

  4. q-oo-p , a piggy domain name. Very cute. boyan.zheng at foxmail.com

    Contact me.

  5. Jmeter中的参数化常用的几种方式

    Jmeter中的参数化常用的几种方式,这里讲一下前两个方式,最后一个在csv参数化里已详细讲解. 1.用户参数 2.函数助手 3.CSV Data Set Config  一.用户参数 位置:添加-前 ...

  6. oracle 表之间的连接

    排序 - - 合并连接(Sort Merge Join, SMJ): a) 对于非等值连接,这种连接方式的效率是比较高的. b) 如果在关联的列上都有索引,效果更好. c) 对于将2个较大的row s ...

  7. selenium兼容非标准chrome内核的浏览器

    多浏览器兼容性测试(1) RIDE已经支持多浏览器兼容性测试,例如: firefox ie chrome safari 但是,项目要求支持360极速和360安全浏览器.所以,我们需要增加代码让RIDE ...

  8. react router activeclassName 没有显示

    import React, {Component} from 'react' import {NavLink, Switch, Route, Redirect} from 'react-router- ...

  9. caffe LOG LOG_IF

    caffe使用了glog,在caffe的solver中输出都是用的LOG和LOG_IF LOG_IF是条件输出: LOG_IF(INFO, num_cookies > ) << &q ...

  10. BZOJ 4464 旅行时的困惑 最小流

    题面: Waldives 有 N 个小岛.目前的交通系统中包含 N-1 条快艇专线,每条快艇 专线连接两个岛.这 N-1条快艇专线恰好形成了一棵树. 由于特殊的原因,所有N-1条快艇专线都是单向的.这 ...