这周训练了以前自学过的栈和队列的题目,把以前回顾的知识又重新练习了一遍,就这做的第一题,一直超时,下面附上题目稍微看一下为什么会超时
Vasya has got nn books, numbered from 11 to nn, arranged in a stack. The topmost book has number a1a1, the next one — a2a2, and so on. The book at the bottom of the stack has number anan. All numbers are distinct.
Vasya wants to move all the books to his backpack in nn steps. During ii-th step he wants to move the book number bibi into his backpack. If the book with number bibi is in the stack, he takes this book and all the books above the book bibi, and puts them into the backpack; otherwise he does nothing and begins the next step. For example, if books are arranged in the order [1,2,3][1,2,3] (book 11 is the topmost), and Vasya moves the books in the order [2,1,3][2,1,3], then during the first step he will move two books (11 and 22), during the second step he will do nothing (since book 11 is already in the backpack), and during the third step — one book (the book number 33). Note that b1,b2,…,bnb1,b2,…,bn are distinct.
Help Vasya! Tell him the number of books he will put into his backpack during each step.
Input
The first line contains one integer n (1≤n≤2⋅105)n (1≤n≤2⋅105) — the number of books in the stack.
The second line contains nn integers a1,a2,…,an (1≤ai≤n)a1,a2,…,an (1≤ai≤n) denoting the stack of books.
The third line contains nn integers b1,b2,…,bn (1≤bi≤n)b1,b2,…,bn (1≤bi≤n) denoting the steps Vasya is going to perform.
All numbers a1…ana1…an are distinct, the same goes for b1…bnb1…bn.
Output
Print nn integers. The ii-th of them should be equal to the number of books Vasya moves to his backpack during the ii-th step.
Examples
Input
3 1 2 3 2 1 3
Output
2 0 1
Input
5 3 1 4 2 5 4 5 1 3 2
Output
3 2 0 0 0
Input
6 6 5 4 3 2 1 6 5 3 4 2 1
Output
1 1 2 0 1 1
Note
The first example is described in the statement.
In the second example, during the first step Vasya will move the books [3,1,4][3,1,4]. After that only books 22 and 55 remain in the stack (22 is above 55). During the second step Vasya will take the books 22 and 55. After that the stack becomes empty, so during next steps Vasya won't move any books.
这么写就超时,时timelimited
#include <iostream>
#include <algorithm>
#include <stack>
using namespace std;
stack<int>a;
int c[200010];
int judge[200010];
int main()
{
int n,x;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>c[i];
}
for(int i=n;i>=1;i--)
{
a.push(c[i]);
}
for(int i=1;i<=n;i++)
{
cin>>x;
int num=0;
if(judge[x]==1||a.empty())
{
cout<<0<<" ";
continue;
}
while(true)
{
if(a.empty())
{
cout<<0<<" ";
break;
}
int temp=a.top();
a.pop();
num++;
judge[temp]=1;
if(temp==x){
cout<<num<<" ";
break;
}
}
}
return 0;
}
#include <iostream>
#include <algorithm>
#include <stack>
using namespace std;
stack<int>a;
int c[200010];
int judge[200010];
int main()
{
int n,x;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>c[i];
}
for(int i=n;i>=1;i--)
{
a.push(c[i]);
}
for(int i=1;i<=n;i++)
{
cin>>x;
int num=0;
if(judge[x]==1||a.empty())
{
cout<<0<<" ";
continue;
}
while(true)
{
int temp=a.top();
a.pop();
num++;
judge[temp]=1;
if(temp==x){
cout<<num<<" ";
break;
}
}
}
return 0;
}
但如果我这道题这么写就ac了,区别就是我在循环里面把判断栈空给删了,其实仔细想一下,这个情况完全没有必要,因为这个东西选或者不选,因为这本书要么还没拿出去要么就已经拿出去了,所以这个判断就显得可有可无了
Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into the N planks (i.e., whose length is the sum of the lengths Li). FJ is ignoring the "kerf", the extra length lost to sawdust when a sawcut is made; you should ignore it, too.
FJ sadly realizes that he doesn't own a saw with which to cut the wood, so he mosies over to Farmer Don's Farm with this long board and politely asks if he may borrow a saw.
Farmer Don, a closet capitalist, doesn't lend FJ a saw but instead offers to charge Farmer John for each of the N-1 cuts in the plank. The charge to cut a piece of wood is exactly equal to its length. Cutting a plank of length 21 costs 21 cents.
Farmer Don then lets Farmer John decide the order and locations to cut the plank. Help Farmer John determine the minimum amount of money he can spend to create the N planks. FJ knows that he can cut the board in various different orders which will result in different charges since the resulting intermediate planks are of different lengths.
Input
Line 1: One integer N, the number of planks
Lines 2.. N+1: Each line contains a single integer describing the length of a needed plank
Output
Line 1: One integer: the minimum amount of money he must spend to make N-1 cuts
Sample Input
3 8 5 8
Sample Output
34
Hint
He wants to cut a board of length 21 into pieces of lengths 8, 5, and 8.
The original board measures 8+5+8=21. The first cut will cost 21, and should be used to cut the board into pieces measuring 13 and 8. The second cut will cost 13, and should be used to cut the 13 into 8 and 5. This would cost 21+13=34. If the 21 was cut into 16 and 5 instead, the second cut would cost 16 for a total of 37 (which is more than 34).
#include<iostream>
#include <queue>
#include <algorithm>
#include<vector>
using namespace std;
int main()
{
int n;
cin>>n;
int x;
priority_queue<int,vector<int>,greater<int> > Q;
for(int i=1;i<=n;i++)
{
cin>>x;
Q.push(x);
}
int z;
long long sum = 0;
while(Q.size()>1)
{
int x = Q.top();
Q.pop();
int y = Q.top();
Q.pop();
int z = x+y;
Q.push(z);
sum+=z;
}
if(n==1)sum=x;
cout<<sum<<endl;
return 0;
}
A bracket sequence is a string, containing only characters "(", ")", "[" and "]".
A correct 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 correct (the resulting expressions are: "(1)+[1]", "([1+1]+1)"), and "](" and "[" are not. The empty string is a correct bracket sequence by definition.
A substring s[l... r] (1 ≤ l ≤ r ≤ |s|) of string s = s 1 s 2... s |s| (where |s| is the length of string s) is the string s l s l + 1... s r. The empty string is a substring of any string by definition.
You are given a bracket sequence, not necessarily correct. Find its substring which is a correct bracket sequence and contains as many opening square brackets «[» as possible.
Input
The first and the only line contains the bracket sequence as a string, consisting only of characters "(", ")", "[" and "]". It is guaranteed that the string is non-empty and its length doesn't exceed 105 characters.
Output
In the first line print a single integer — the number of brackets «[» in the required bracket sequence. In the second line print the optimal sequence. If there are more than one optimal solutions print any of them.
Examples
Input
([])
Output
1 ([])
Input
(((
Output
0
这个题目的话看似比较简单但其实就非常难了,需要考虑的知识有很多很多,我也是查阅了好多资料才最终把这个题目ac了
#include <cstdio>
#include <map>
#include <stack>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
#endif // ONLINE_JUDGE
map<char,int> m;
m['('] = 1;
m[')'] = -1;
m['['] = 2;
m[']'] = -2;
char str[100000+22];
int l;
int ans;
while (~scanf ("%s",str))
{
l = strlen(str);
stack<int> s;
ans = 0;
for (int i = 0 ; i < l ; i++)
{
if (m[str[i]] == 1 || m[str[i]] == 2)
s.push(i);
else if (s.empty())
s.push(i);
else if (m[str[i]] + m[str[s.top()]] == 0)
s.pop();
else
s.push(i);
}
if (s.empty())
{
for (int i = 0 ; i < l ; i++)
if (m[str[i]] == 2)
ans++;
printf ("%d\n%s\n",ans,str);
}
else
{
int st,endd;
int ans_st,ans_endd;
int ant;
s.push(l);
while (s.size() != 1)
{
endd = s.top() - 1;
s.pop();
st = s.top() + 1;
ant = 0;
for (int i = st ; i <= endd ; i++)
if (m[str[i]] == 2)
ant++;
if (ant > ans)
{
ans = ant;
ans_st = st;
ans_endd = endd;
}
}
st = 0;
endd = s.top() - 1;
ant = 0;
for (int i = st ; i <= endd ; i++)
if (m[str[i]] == 2)
ant++;
if (ant > ans)
{
ans = ant;
ans_st = st;
ans_endd = endd;
}
if (ans)
{
printf ("%d\n",ans);
for (int i = ans_st ; i <= ans_endd ; i++)
printf ("%c",str[i]);
printf ("\n");
}
else
printf ("0\n\n");
}
}
return 0;
}