暑假训练day2

The Balance of the World

The world should be finely balanced. Positive vs. negative, light vs. shadow, and left vs. right brackets. Your mission is to write a program that judges whether a string is balanced with respect to brackets so that we can observe the balance of the world.

A string that will be given to the program may have two kinds of brackets, round (“( )”) and square (“[ ]”). A string is balanced if and only if the following conditions hold.

For every left round bracket (“(”), there is a corresponding right round bracket (“)”) in the following part of the string.
For every left square bracket (“[”), there is a corresponding right square bracket (“]”) in the following part of the string.
For every right bracket, there is a left bracket corresponding to it.
Correspondences of brackets have to be one to one, that is, a single bracket never corresponds to two or more brackets.
For every pair of corresponding left and right brackets, the substring between them is balanced.

Input

The input consists of one or more lines, each of which being a dataset. A dataset is a string that consists of English alphabets, space characters, and two kinds of brackets, round (“( )”) and square (“[ ]”), terminated by a period. You can assume that every line has 100 characters or less. The line formed by a single period indicates the end of the input, which is not a dataset.
Output

For each dataset, output “yes” if the string is balanced, or “no” otherwise, in a line. There may not be any extra characters in the output.
Sample Input

So when I die (the [first] I will see in (heaven) is a score list).
[ first in ] ( first out ).
Half Moon tonight (At least it is better than no Moon at all].
A rope may form )( a trail in a maze.
Help( I[m being held prisoner in a fortune cookie factory)].
([ (([( [ ] ) ( ) (( ))] )) ]).
.
.

Output for the Sample Input

yes
yes
no
no
no
yes
yes

题意:括号匹配 用栈模拟一下就行了

#include<iostream>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<string>
#include<set>
#include<cmath>

using namespace std;

int main() {
    string s;
    while (1) {
        getline(cin, s);
        stack<char> st;
        if (s == ".")
            break;
        bool flag = true;
        for (int i = 0; flag && i < s.size(); i++) {
            if (s[i] == '(' || s[i] == '[') {
                st.push(s[i]);
            } else if (s[i] == ')') {
                if(st.empty())
                    flag = false;
                else if (st.top() != '(')
                    flag = false;
                else st.pop();
            } else if (s[i] == ']') {
                if(st.empty())
                    flag = false;
                else if (st.top() != '[')
                    flag = false;
                else st.pop();
            }
        }
        if (!st.empty())
            flag = false;
        if (flag)
            cout << "yes" << endl;
        else
            cout << "no" << endl;
    }
}

Stones

 Because of the wrong status of the bicycle, Sempr begin to walk east to west every morning and walk back every evening. Walking may cause a little tired, so Sempr always play some games this time.
There are many stones on the road, when he meet a stone, he will throw it ahead as far as possible if it is the odd stone he meet, or leave it where it was if it is the even stone. Now give you some informations about the stones on the road, you are to tell me the distance from the start point to the farthest stone after Sempr walk by. Please pay attention that if two or more stones stay at the same position, you will meet the larger one(the one with the smallest Di, as described in the Input) first.

Input
In the first line, there is an Integer T(1<=T<=10), which means the test cases in the input file. Then followed by T test cases.
For each test case, I will give you an Integer N(0<N<=100,000) in the first line, which means the number of stones on the road. Then followed by N lines and there are two integers Pi(0<=Pi<=100,000) and Di(0<=Di<=1,000) in the line, which means the position of the i-th stone and how far Sempr can throw it.
Output
Just output one line for one test case, as described in the Description.
Sample Input

2
2
1 5
2 4
2
1 5
6 6

Sample Output

11
12

题意:不处理偶数的石头 把遇见数奇数的石头往前扔 知道不能扔为止的距离

题解:重构一下优先队列的优先级模拟即可

#include<iostream>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<string>
#include<set>
#include<cmath>

struct node {
    int u;
    int v;

    bool operator<(const node a) const {
        return u == a.u ? v > a.v : u > a.u;
    }
};

long long gcd(long long a, long long b) {
    return b > 0 ? gcd(b, a % b) : a;
}

using namespace std;

int main(void) {
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        priority_queue<node> q;
        while (n--) {
            node x;
            cin >> x.u >> x.v;
            q.push(x);
        }
        int flag = 1;
        node tmp;
        while (!q.empty()) {
            tmp = q.top();
            q.pop();
            if (flag) {
                tmp.u += tmp.v;
                q.push(tmp);
            }
            flag = flag == 1 ? 0 : 1;
        }
        cout << tmp.u << endl;
    }
    return 0;
}

Running Median

 For this problem, you will write a program that reads in a sequence of 32-bit signed integers. After each odd-indexed value is read, output the median (middle value) of the elements received so far. 

Input
The first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of data sets that follow. The first line of each data set contains the data set number, followed by a space, followed by an odd decimal integer M, (1 ≤ M ≤ 9999), giving the total number of signed integers to be processed.
The remaining line(s) in the dataset consists of the values, 10 per line, separated by a single space.
The last line in the dataset may contain less than 10 values.
Output
For each data set the first line of output contains the data set number, a single space and the number of medians output (which should be one-half the number of input values plus one). The output medians will be on the following lines, 10 per line separated by a single space. The last line may have less than 10 elements, but at least 1 element. There should be no blank lines in the output.
Sample Input

3
1 9
1 2 3 4 5 6 7 8 9
2 9
9 8 7 6 5 4 3 2 1
3 23
23 41 13 22 -3 24 -31 -11 -8 -7
3 5 103 211 -311 -45 -67 -73 -81 -99
-33 24 56

Sample Output

1 5
1 2 3 4 5
2 5
9 8 7 6 5
3 12
23 23 22 22 13 3 5 5 3 -3
-7 -3

题意:找出i为奇数的时候的中位数

题解:用对顶堆的思想来找中位数(代码是我第一次写对顶堆的 跟着别人题解看着写的)

#include<iostream>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<string>
#include<set>
#include<cmath>

using namespace std;
priority_queue<int> big;
priority_queue<int, vector<int>, greater<int> > small;

int main() {
    int t;
    cin >> t;
    while (t--) {
        while (!big.empty()) big.pop();
        while (!small.empty()) small.pop();
        int n, m;
        cin >> n >> m;
        int cnt = 1, a, mid = -0x3f3f3f3f;
        cout << n << " " << ((m + 1) / 2) << endl;
        for (int i = 1; i <= m; i++) {
            cin >> a;
            if (big.empty()) {
                big.push(a);
                mid = a;
                cout << a;
                if(!(m <= 2))
                    cout << " ";
                continue;
            }
            if (a < mid) big.push(a);
            else small.push(a);
            if (((int) big.size() - (int) small.size()) > 1) {
                int tmp = big.top();
                big.pop();
                small.push(tmp);
            } else if (((int) small.size() - (int) big.size()) > 1) {
                int tmp = small.top();
                small.pop();
                big.push(tmp);
            }
            if (i & 1) {
                ++cnt;
                if (big.size() > small.size()) mid = big.top();
                else mid = small.top();
                if (cnt % 10 == 0) cout << mid << endl;
                else {
                    cout << mid;
                    if (cnt < ((m + 1) / 2)) cout << " ";
                }
            } else mid = (small.top() + big.top()) / 2;
        }
        if (cnt % 10 != 0) cout << endl;
    }
    return 0;
}


Let the Balloon Rise

 Contest time again! How excited it is to see balloons floating around. But to tell you a secret, the judges' favorite time is guessing the most popular problem. When the contest is over, they will count the balloons of each color and find the result.

This year, they decide to leave this lovely job to you.

Input
Input contains multiple test cases. Each test case starts with a number N (0 < N <= 1000) – the total number of balloons distributed. The next N lines contain one color each. The color of a balloon is a string of up to 15 lower-case letters.

A test case with N = 0 terminates the input and this test case is not to be processed.

Output
For each case, print the color of balloon for the most popular problem on a single line. It is guaranteed that there is a unique solution for each test case.
Sample Input

5
green
red
blue
red
red
3
pink
orange
pink
0

Sample Output

red
pink

题意:找出颜色最多的气球

题解: 用map的键来对应气球数 输出最大的即可

#include<iostream>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<string>
#include<set>
#include<cmath>

using namespace std;


int main(void)
{
    int n;
    while(cin >> n && n)
    {
        map<string ,int> m;
        for(int i = 0; i < n; i++)
        {
            string s;
            cin >> s;
            m[s]++;
        }
        int maxx = 0;
        string ans = "";
        for(map<string,int>::iterator it = m.begin(); it != m.end(); it++)
        {
            if(it->second > maxx)
            {
                maxx = it->second;
                ans = it->first;
            }
        }
        cout << ans << endl;
    }
}

Shopping

 Every girl likes shopping,so does dandelion.Now she finds the shop is increasing the price every day because the Spring Festival is coming .She is fond of a shop which is called "memory". Now she wants to know the rank of this shop's price after the change of everyday. 

Input
One line contians a number n ( n<=10000),stands for the number of shops.
Then n lines ,each line contains a string (the length is short than 31 and only contains lowercase letters and capital letters.)stands for the name of the shop.
Then a line contians a number m (1<=m<=50),stands for the days .
Then m parts , every parts contians n lines , each line contians a number s and a string p ,stands for this day ,the shop p 's price has increased s.
Output
Contains m lines ,In the ith line print a number of the shop “memory” ‘s rank after the ith day. We define the rank as :If there are t shops’ price is higher than the “memory” , than its rank is t+1.
Sample Input

3
memory
kfc
wind
2
49 memory
49 kfc
48 wind
80 kfc
85 wind
83 memory

Sample Output

1
2

题意:找momory的价格排在第几

题解:与上题相似

#include<iostream>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<string>
#include<set>
#include<cmath>

using namespace std;


int main(void) {
    int n, mm;
    while (cin >> n) {
        map<string, int> m;
        for (int i = 0; i < n; i++) {
            string s;
            cin >> s;
            m[s] = 0;
        }
        cin >> mm;
        for (int i = 0; i < mm; i++) {
            for (int j = 0; j < n; j++) {
                int p;
                string sh;
                cin >> p >> sh;
                m[sh] += p;
            }
            int ans = 1;
            for (map<string, int>::iterator it = m.begin(); it != m.end(); it++) {
                if (it->second > m["memory"]) {
                    ans++;
                }
            }
            cout << ans << endl;
        }
    }
    return 0;
}


pairs

 John has n points on the X axis, and their coordinates are (x[i],0),(i=0,1,2,…,n−1). He wants to know how many pairs<a,b> that |x[b]−x[a]|≤k.(a<b) 

Input
The first line contains a single integer T (about 5), indicating the number of cases.
Each test case begins with two integers n,k(1≤n≤100000,1≤k≤109).
Next n lines contain an integer xi, means the X coordinates.
Output
For each case, output an integer means how many pairs<a,b> that |x[b]−x[a]|≤k.
Sample Input

2
5 5
-100
0
100
101
102
5 300
-100
0
100
101
102

Sample Output

3
10

题意:给你一段序列,让你找出满足 |x[b]−x[a]|≤k.(a<b)的个数。

题解:排序直接二分

#include<iostream>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<string>
#include<set>
#include<cmath>

using namespace std;


const long long maxn = 1e5 + 10;

int a[maxn];
int k;

bool check(long long x, long long y) {
    return a[y] - a[x] <= k;
}

int main() {
    long long t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n >> k;
        for (int i = 1; i <= n; i++)
            cin >> a[i];
        sort(a + 1, a + n + 1);
        long long ans = 0;
        for (long long i = 1; i < n; i++) {
            long long l = i + 1, r = n;
            while (l <= r) {
                long long mid = (l + r) / 2;
                if (check(i, mid)) l = mid + 1;
                else r = mid - 1;
            }
            ans += (r - i);
        }
        cout << ans << endl;
    }
    return 0;
}


产生冠军

 有一群人,打乒乓球比赛,两两捉对撕杀,每两个人之间最多打一场比赛。
球赛的规则如下:
如果A打败了B,B又打败了C,而A与C之间没有进行过比赛,那么就认定,A一定能打败C。
如果A打败了B,B又打败了C,而且,C又打败了A,那么A、B、C三者都不可能成为冠军。
根据这个规则,无需循环较量,或许就能确定冠军。你的任务就是面对一群比赛选手,在经过了若干场撕杀之后,确定是否已经实际上产生了冠军。

Input
输入含有一些选手群,每群选手都以一个整数n(n<1000)开头,后跟n对选手的比赛结果,比赛结果以一对选手名字(中间隔一空格)表示,前者战胜后者。如果n为0,则表示输入结束。
Output
对于每个选手群,若你判断出产生了冠军,则在一行中输出“Yes”,否则在一行中输出“No”。
Sample Input

3
Alice Bob
Smith John
Alice Smith
5
a c
c d
d e
b e
a d
0

Sample Output

Yes
No

题解:set去重最后剩一个就ok了

#include<iostream>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<string>
#include<set>
#include<cmath>

using namespace std;


int main(void)
{
    int n;
    while(cin >> n && n)
    {
        set<string> s;
        string s1[1010], s2[1010];
        for(int i = 0; i < n; i++)
        {
            cin >> s1[i] >> s2[i];
            s.insert(s1[i]);
            s.insert(s2[i]);
        }
        for(int i = 0; i < n; i++)
        {
            s.erase(s2[i]);
        }
        if(s.size() == 1)
            cout << "Yes" << endl;
        else
            cout << "No" << endl;
    }
    return 0;
}


Kiki & Little Kiki 1

 Kiki is considered as a smart girl in HDU, many boys fall in love with her! Now, kiki will finish her education, and leave school, what a pity! One day, zjt meets a girl, who is like kiki very much, in the campus, and calls her little kiki. Now, little kiki want to design a container, which has two kinds of operation, push operation, and pop operation.
Push M:
  Push the integer M into the container.
Pop M:
  Find the maximal integer, which is not bigger than M, in the container. And pop it from the container. Specially, for all pop operations, M[i] is no bigger than M[i+1].
Although she is as smart as kiki, she still can't solve this problem! zjt is so busy, he let you to help little kiki to solve the problem. Can you solve the problem?

Input
The input contains one or more data sets. At first line of each input data set is an integer N (1<= N <= 100000) indicate the number of operations. Then N lines follows, each line contains a word (“Push” or “Pop”) and an integer M. The word “Push” means a push operation, while “Pop” means a pop operation. You may assume all the numbers in the input file will be in the range of 32-bit integer.
Output
For each pop operation, you should print the integer satisfy the condition. If there is no integer to pop, please print “No Element!”. Please print a blank line after each data set.
Sample Input

9
Push 10
Push 20
Pop 2
Pop 10
Push 5
Push 2
Pop 10
Pop 11
Pop 19
3
Push 2
Push 5
Pop 2

Sample Output

No Element!
10
5
2
No Element!

2

题意:模仿一个stack,push x为向stack中推入一个x,pop x为从stack中推出一个比x小的最大的数,如果有这样一个数输出这个数,否则输出No Element。(x可重复)

题解:直接看代码吧(模拟就好了)

#include<iostream>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<string>
#include<set>
#include<cmath>

using namespace std;


multiset<int>st;



int main()
{
    int n, m;
    while (cin >> n)
    {
        string s;
        st.clear();
        for (int i = 0; i < n; i++)
        {
            cin >> s >> m;
            if (s == "Push")
            {
                st.insert(m);
            }
            else
            {
                multiset<int> :: iterator it;
                it = st.upper_bound(m);
                if (it == st.begin())
                {
                   cout << "No Element!" << endl;
                }
                else
                {
                    it--;
                    cout << (*it) << endl;
                    st.erase(it);
                }
            }
        }
        cout << endl;
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值