赞
踩
有n(n≤2×106) 名同学陆陆续续进入教室。我们知道每名同学的学号(在 1到 10^9 之间),按进教室的顺序给出。上课了,老师想知道第 i 个进入教室的同学的学号是什么(最先进入教室的同学 i=1),询问次数不超过 10^5 次。
第一行 2 个整数 n 和 m,表示学生个数和询问次数。
第二行 n 个整数,表示按顺序进入教室的学号。
第三行 m 个整数,表示询问第几个进入教室的同学。
输出 m 个整数表示答案,用换行隔开。
输入
10 3 1 9 2 60 8 17 11 4 5 14 1 5 9
输出
1 8 5
- #include <iostream>
- using namespace std;
- int n, m, t, a[2000001];
- int main()
- {
- cin>>n>>m;
- for (int i = 1; i <= n; i++)
- cin>>a[i];
- while (m--)
- {
- cin>>t;
- cout<< a[t]<<endl;
- }
- }
定义如下规则:
例如,下面的字符串都是平衡括号序列:
()
,[]
,(())
,([])
,()[]
,()[()]
而以下几个则不是:
(
,[
,]
,)(
,())
,([()
现在,给定一个仅由 (
,)
,[
,]
构成的字符串 s,请你按照如下的方式给字符串中每个字符配对:
配对结束后,对于 s 中全部未配对的括号,请你在其旁边添加一个字符,使得该括号和新加的括号匹配。
输入只有一行一个字符串,表示 s。
输出一行一个字符串表示你的答案。
输入
([()
输出
()[]()
输入
([)
输出
()[]()
对于全部的测试点,保证 ss 的长度不超过 100100,且只含 (
,)
,[
,]
四种字符。
- #include<iostream>
- #include <bits/stdc++.h>
- using namespace std;
- int a[101];
- int main()
- {
- int i,j;
- string s;
- cin >> s;
- for (i=0; i<s.length(); i++) {
- if (s[i] == ')') {
- for (j=i-1; j>=0; j--) {
- if (s[j] == '(' and a[j] == 0) {
- a[i] = a[j] = 1;
- break;
- }
- else if (s[j] == '[' and a[j] == 0) break;
- }
- }
- else if (s[i] == ']') {
- for (j=i-1; j>=0; j--) {
- if (s[j] == '[' and a[j] == 0) {
- a[i] = a[j] = 1;
- break;
- }
- else if (s[j] == '(' and a[j] == 0) break;
- }
- }
- }
- for (i=0; i<s.length(); i++) {
- if (a[i] == 0) {
- if (s[i] == '(' or s[i] == ')') cout << "()";
- else cout << "[]";
- }
- else cout << s[i];
- }
- return 0;
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。