Description
Let us define a regular brackets sequence in the following way:
1. Empty sequence is a regular sequence.
2. If S is a regular sequence, then (S) is a regular sequence.
3. If A and B are regular sequences, then AB is a regular sequence.
For example, these sequences of characters are regular brackets sequences: (), (()), ()(), ()(()), ((())())().
And all the following character sequences are not: (, ), ((), ()), ())(, (()(, ()))().
A sequence of characters '(' and ')' is given. You can insert only one '(' or ')' into the left of the sequence, the right of the sequence, or the place between any two adjacent characters, to try changing this sequence to a regular brackets sequence.
Input
The first line has a integer T (1 <= T <= 200), means there are T test cases in total.
For each test case, there is a sequence of characters '(' and ')' in one line. The length of the sequence is in range [1, 105].
Output
For each test case, print how many places there are, into which you insert a '(' or ')', can change the sequence to a regular brackets sequence.
What's more, you can assume there has at least one such place.
Sample Input
4 ) ()) (()(()) ((())())(()
Sample Output
1 3 7 3
#include <iostream>
#include <string.h>
#include <string>
#include <cstring>
#include <set>
#include <stack>
using namespace std;
typedef long long ll;
const int MOD=1e8+7;
const int maxn=100005;
struct Node
{
char a;
int id;
}node[maxn];
string c;
stack<Node>s;
ll ans;
int main()
{
int T;
cin>>T;
while(T--)
{
while(!s.empty())
s.pop();
cin>>c;
int len=c.length();
ans=0;
for(int i=0;i<len;i++)
{
node[i].a=c[i];
node[i].id=i+1;
}
for(int i=0;i<len;i++)
{
if(c[i]=='(')
s.push(node[i]);
else
{
if(s.empty())
ans+=(i+1);
else
s.pop();
}
}
if(!s.empty())
{
Node temp=s.top();
ans+=(len-temp.id+1);
while(!s.empty())
{
s.pop();
if(!s.empty())
{
ans+=(temp.id-s.top().id);
temp=s.top();
}
}
}
cout<<ans<<endl;
}
return 0;
}
/**********************************************************************
Problem: 1271
User: jk1601zr
Language: C++
Result: AC
Time:80 ms
Memory:3120 kb
**********************************************************************/
Description
小X学习了dfs,为了练习搜索,开始给自己出题玩。
玩着玩着,一会把自己难住了,一会又被自己难倒了,真是有趣诶!
小X出的题:
现在有N个不同的正整数,求它们可以组成多少个这样的集合,满足:
- 集合内的元素数量S>1
- 集合内任意两个数的差的绝对值都大于集合内的元素数量。
Input
第一行,一个正整数T(T<=20)表示数据组数。
对于每组数据,有两行。第一行为一个正整数N(3≤N≤25),第二行为N个用空格隔开的正整数xi(xi≤200)。
Output
对于每组数据,输出一行一个整数表示题中所描述的集合的个数。
Sample Input
1 5 2 3 5 8 1
Sample Output
6
#include <iostream>
#include <string.h>
#include <string>
#include <math.h>
#include <algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=30;
int n;
int a[maxn];
int b[maxn];
int ans;
void dfs(int x,int k,int mingap)
{
if(x==(n-1)||mingap<=k+1)
return;
for(int i=x+1;i<n;i++)
{
if(a[i]-a[x]>k+1)
{
dfs(i,k+1,min(mingap,a[i]-a[x]));
ans++;
}
}
}
int main()
{
int T;
cin>>T;
while(T--)
{
memset(a,0,sizeof(a));
ans=0;
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);
for(int i=0;i<n;i++)
dfs(i,1,INF);
cout<<ans<<endl;
}
return 0;
}
/**********************************************************************
Problem: 1973
User: jk1601zr
Language: C++
Result: AC
Time:80 ms
Memory:2024 kb
**********************************************************************/
Description
Input
Output
一个整数,为最少拆毁的障碍物数量,如果不能逃离废墟,输出-1。
Sample Input
3 3 3 ### #@* *** 3 4 #### #@.* **.* 3 3 .#. #@# .#.
Sample Output
1 0 -1
#include<iostream>
#include<string>
#include<string.h>
#include<cstdio>
#include<map>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
const int maxn = 105;
int n,m;
char a[maxn][maxn];
int vis[maxn][maxn];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
struct Node
{
int x;
int y;
int cnt;
};
bool operator <(Node p,Node q)
{
return p.cnt>q.cnt;
}
int bfs(int xx,int yy)
{
priority_queue<Node>q;
Node now,temp,next;
now.x=xx;
now.y=yy;
now.cnt=0;
vis[xx][yy]=1;
q.push(now);
while(!q.empty())
{
temp=q.top();
q.pop();
if(temp.x==n-1||temp.y==m-1||temp.x==0||temp.y==0)
return temp.cnt;
for(int i=0;i<4;i++)
{
next.x=temp.x+dx[i];
next.y=temp.y+dy[i];
if(next.x>=0&&next.y>=0&&next.y<m&&next.x<n&&a[next.x][next.y]!='#'&&vis[next.x][next.y]==0)
{
if(a[next.x][next.y]=='*')
next.cnt=temp.cnt+1;
else
next.cnt=temp.cnt;
q.push(next);
vis[next.x][next.y]=1;
}
}
}
return -1;
}
int main()
{
int T;
cin>>T;
while(T--)
{
cin>>n>>m;
int x1,y1;
memset(vis,0,sizeof(vis));
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>a[i][j];
if(a[i][j]=='@')
{
x1=i;y1=j;
}
}
}
cout<<bfs(x1,y1)<<endl;
}
return 0;
}
/**********************************************************************
Problem: 1726
User: jk1601zr
Language: C++
Result: AC
Time:56 ms
Memory:2076 kb
**********************************************************************/
Description
A Horcrux is an object in which a Dark wizard or witch has hidden a fragment of his or her soul for the purpose of attaining immortality. Constructing a Horcrux is considered Dark magic of the foulest, most evil kind, as it violates laws of nature and morality, and requires a horrific act (a.k.a. murder) to accomplish.
There are two kinds of horcruxes, the white one, denoted as △
, and the black one, denoted as ▲
. Topper has got N horcruxes, and he wants to destroy them to win the Dark wizard. Toper places all the horcruxes in a line from left to right, one by one, and then says a magic spell to destroy them. In order to make the magic spell works, Toper needs to know the number of the white horcruxes.
Since the horcruxes also has magic, when placing the horcruxes, they will change color from white to black or from black to white under the following rules:
When Topper places the i-th horcrux and i is an even number: If the i-th horcrux and the rightmost horcrux have different colors, all consecutive horcruxes of the same color on the right change its color.
- In other situations, no magic works.
For example, suppose the horcruxes on the line are:
△△▲▲△△△
After placing 7 horcruxes.
If the 8-th horcrux is white, since its color and the color of the rightmost horcrux are the same. Therefore, the horcruxes on the line become as follows:
△△▲▲△△△△
If the 8-th horcrux is black, since its color and the color of the rightmost horcrux are different, the 3 consecutive white stones on the right change their color. Therefore, the stones on the line become as follows:
△△▲▲▲▲▲▲
You see, it’s not an easy job to beat the Dark wizard. So write a program to help Topper.
Input
There are some test cases. In each test case, the first line contains a positive integer n (1≤n≤100,000), which is the number of horcruxes. The following n lines denote the sequence in which Topper places the horcruxes. 0 stands for white horcrux, and 1 stands for black horcrux.
Output
For each test case, output one line containing only the number of white horcruxes on the line after Topper places n horcruxes.
Sample Input
8 1 0 1 1 0 0 0 0 8 1 0 1 1 0 0 0 1
Sample Output
6 2
#include <iostream>
#include <string>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <queue>
#include <vector>
#include <map>
#include <set>
using namespace std;
const int maxn=100005;
typedef long long LL;
struct soul
{
int color,num;
}a[maxn];
int main()
{
int n,x,cnt;
while(cin>>n)
{
cnt=0;
cin>>x;
a[cnt].color=x;
a[cnt].num=1;
for(int i=2;i<=n;i++)
{
cin>>x;
if(x==a[cnt].color)
a[cnt].num++;
else
{
if(i%2==1)
{
cnt++;
a[cnt].color=x;
a[cnt].num=1;
}
else
{
a[cnt].num++;
a[cnt].color=x;
if(cnt>0)
{
a[cnt-1].num+=a[cnt].num;
cnt--;
}
}
}
}
x=0;
for(int i=0;i<=cnt;i++)
{
if(a[i].color==0)
x+=a[i].num;
}
cout<<x<<endl;
}
return 0;
}
/**********************************************************************
Problem: 1008
User: jk1601zr
Language: C++
Result: AC
Time:44 ms
Memory:2804 kb
**********************************************************************/
Description
The Happy Desert is full of sands. There is only a kind of animal called camel living on the Happy Desert. Cause they live here, they need water here. Fortunately, they find a pond which is full of water in the east corner of the desert. Though small, but enough. However, they still need to stand in lines to drink the water in the pond.
Now we mark the pond with number 0, and each of the camels with a specific number, starting from 1. And we use a pair of number to show the two adjacent camels, in which the former number is closer to the pond. There may be more than one lines starting from the pond and ending in a certain camel, but each line is always straight and has no forks.
Input
There’re multiple test cases. In each case, there is a number N (1≤N≤100000) followed by N lines of number pairs presenting the proximate two camels. There are 99999 camels at most.
Output
For each test case, output the camel’s number who is standing in the last position of its line but is closest to the pond. If there are more than one answer, output the one with the minimal number.
Sample Input
1 0 1 5 0 2 0 1 1 4 2 3 3 5 5 1 3 0 2 0 1 0 4 4 5
Sample Output
1 4 2
#include <iostream>
#include <cmath>
#include <cstdio>
#include <string.h>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
#define M 100005
#define INF 1<<29
#define CLS(x,v) memset(x,v,sizeof(x))
int r[M];
int head[M];
void link(int x, int y)
{
r[x] = y;
}
int main()
{
int n, cnt;
int a, b;
while (cin>>n)
{
memset(r, -1, sizeof(r));
cnt = 0;
for (int i = 0; i < n; i++)
{
cin >> a >> b;
if (a == 0)head[cnt++] = b;
else link(a, b);
}
int ans = n, flag = 1;
while (flag)
{
for (int i = 0; i < cnt; i++)
{
if (r[head[i]] == -1)
{
ans = min(ans, head[i]);
flag = 0;
}
head[i] = r[head[i]];
}
}
printf("%d\n", ans);
}
return 0;
}
/**********************************************************************
Problem: 1010
User: jk1601zr
Language: C++
Result: AC
Time:120 ms
Memory:2804 kb
**********************************************************************/
Description
Did you know that if you draw a circle that fills the screen on your 1080p high definition display, almost a million pixels are lit? That's a lot of pixels! But do you know exactly how many pixels are lit? Let's find out!
Assume that our display is set on a Cartesian grid where every pixel is a perfect unit square. For example, one pixel occupies the area of a square with corners (0,0) and (1,1). A circle can be drawn by specifying its center in grid coordinates and its radius. On our display, a pixel is lit if any part of it is covered by the circle being drawn; pixels whose edge or corner are just touched by the circle, however, are not lit.
Your job is to compute the exact number of pixels that are lit when a circle with a given position and radius is drawn.
Input
The input consists of several test cases, each on a separate line. Each test case consists of three integers, x,y, and r(1≤x,y,r≤1,000,000), specifying respectively the center (x,y) and radius of the circle drawn. Input is followed by a single line with x = y = r = 0, which should not be processed.
Output
For each test case, output on a single line the number of pixels that are lit when the specified circle is drawn. Assume that the entire circle will fit within the area of the display.
Sample Input
1 1 1 5 2 5 0 0 0
Sample Output
4 88
#include <iostream>
#include <string>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <queue>
#include <vector>
#include <map>
#include <set>
using namespace std;
const int maxn=1005;
typedef long long LL;
int x,y,r;
LL ans;
int main()
{
while(cin>>x>>y>>r)
{
if(x==0&&y==0&&r==0)
return 0;
else
{
ans=0;
int i=r-1,j=0;
LL temp=r*r;
while(j<r)
{
if(i*i+j*j<temp)
{
ans+=(i+1);
//cout<<ans<<endl;
}
else
{
i--;
continue;
}
j++;
}
cout<<ans*4<<endl;
}
}
return 0;
}
/**********************************************************************
Problem: 1011
User: jk1601zr
Language: C++
Result: AC
Time:36 ms
Memory:2020 kb
**********************************************************************/