zoukankan      html  css  js  c++  java
  • Codeforces1132A——Regular Bracket Sequence(水题)

    Regular Bracket Sequence

    time limit per test:1 second

    memory limit per test:256 megabytes

    input:standard input

    output:standard output

    A string is called bracket sequence if it does not contain any characters other than “(” and “)”. A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters “+” and “1” into this sequence. For example, “”, “(())” and “()()” are regular bracket sequences; “))” and “)((” are bracket sequences (but not regular ones), and “(a)” and “(1)+(1)” are not bracket sequences at all.

    You have a number of strings; each string is a bracket sequence of length 22. So, overall you have cnt1cnt1 strings “((”, cnt2cnt2 strings “()”, cnt3cnt3 strings “)(” and cnt4cnt4 strings “))”. You want to write all these strings in some order, one after another; after that, you will get a long bracket sequence of length 2(cnt1+cnt2+cnt3+cnt4)2(cnt1+cnt2+cnt3+cnt4). You wonder: is it possible to choose some order of the strings you have such that you will get a regular bracket sequence? Note that you may not remove any characters or strings, and you may not add anything either.

    Input

    The input consists of four lines, ii-th of them contains one integer cnticnt_i (0cnti109)(0≤cnt_i≤10^9).

    Output

    Print one integer: 11 if it is possible to form a regular bracket sequence by choosing the correct order of the given strings, 00 otherwise.

    Examples

    input

    3
    1
    4
    3
    

    output

    1
    

    input

    0
    0
    0
    0
    

    output

    1
    

    input

    1
    2
    3
    4
    

    output

    0
    

    Note

    In the first example it is possible to construct a string “(())()(()((()()()())))”, which is a regular bracket sequence.

    In the second example it is possible to construct a string “”, which is a regular bracket sequence.

    Solve

    你有四种括号,"((","()",")(","))", 给出四种括号的数量, 问能否将这些括号以某种顺序连接起来, 使得每个左括号都有与之匹配的右括号。

    观察可以发现:如果要完全匹配,()是不会对结果产生影响的,然后可以推出一个关系式:cnt1=2×cnt3+cnt4,cnt4=2×cnt3+cnt1cnt1=2 imes cnt3+cnt4,cnt4=2 imes cnt3+cnt1

    即:当cnt30,cnt1=cnt40cnt3 eq0,cnt1=cnt4 eq0cnt3=0,cnt1=cnt2cnt3=0,cnt1=cnt2时,才会完全匹配

    Code

    /*************************************************************************
    
    	 > Author: WZY
    	 > School: HPU
    	 > Created Time:   2019-03-15 10:34:53
    	 
    ************************************************************************/
    #include <cmath>
    #include <cstdio>
    #include <time.h>
    #include <cstring>
    #include <limits.h>
    #include <iostream>
    #include <algorithm>
    #include <random>
    #include <iomanip>
    #include <map>
    #include <set>
    #include <stack>
    #include <queue>
    #include <vector>
    #include <string>
    #include <random>
    #define ll long long
    #define ull unsigned long long
    #define lson o<<1
    #define rson o<<1|1
    #define ms(a,b) memset(a,b,sizeof(a))
    #define SE(N) setprecision(N)
    #define PSE(N) fixed<<setprecision(N)
    #define bug cerr<<"-------------"<<endl
    #define debug(...) cerr<<"["<<#__VA_ARGS__":"<<(__VA_ARGS__)<<"]"<<"
    "
    #define LEN(A) strlen(A)
    const double E=exp(1);
    const double eps=1e-9;
    const double pi=acos(-1.0);
    const int mod=1e9+7;
    const int maxn=1e6+10;
    const int maxm=1e3+10;
    const int moha=19260817;
    const int inf=1<<30;
    const ll INF=1LL<<60;
    using namespace std;
    inline void Debug(){cerr<<'
    ';}
    inline void MIN(int &x,int y) {if(y<x) x=y;}
    inline void MAX(int &x,int y) {if(y>x) x=y;}
    inline void MIN(ll &x,ll y) {if(y<x) x=y;}
    inline void MAX(ll &x,ll y) {if(y>x) x=y;}
    template<class FIRST, class... REST>void Debug(FIRST arg, REST... rest){
    	cerr<<arg<<"";Debug(rest...);}
    int main(int argc, char const *argv[])
    {
    	ios::sync_with_stdio(false);cin.tie(0);
    	cout.precision(20);
    	#ifndef ONLINE_JUDGE
    	    freopen("in.txt", "r", stdin);
    	    freopen("out.txt", "w", stdout);
    		srand((unsigned int)time(NULL));
    	#endif
    	int cnt1,cnt2,cnt3,cnt4;
    	while(cin>>cnt1>>cnt2>>cnt3>>cnt4)
    	{
    		if(cnt1+cnt4==0)
    		{
    			if(cnt3)
    				cout<<0<<endl;
    			else
    				cout<<1<<endl;
    			continue;
    		}
    		if(cnt1==cnt4)
    			cout<<1<<endl;
    		else
    			cout<<0<<endl;
    	}
    	#ifndef ONLINE_JUDGE
    	    cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s.
    ";
    	#endif
    	return 0;
    }
    
  • 相关阅读:
    hive分区学习
    pyspark的学习
    往hive表中插入数据以及导出数据
    【Pytest学习3】setup和teardown简单用法,fixture里面的scope等于setup,用yield等于teardown
    【Pytest学习2】 pytest用例设计规则,terminal中使用常见命令行参数,pycharm中使用常见的命令行参数
    Jmeter响应内容显示乱码问题的解决办法
    Jmeter(三)测试计划和线程组
    Jmeter(二)Jmeter目录介绍 & 元件介绍
    JMeter之Ramp-up Period(in seconds)说明
    badboy云盘下载链接
  • 原文地址:https://www.cnblogs.com/Friends-A/p/11054963.html
Copyright © 2011-2022 走看看