Skip to content

Commit

Permalink
彻底服了OJ...
Browse files Browse the repository at this point in the history
  • Loading branch information
ihciah committed Oct 21, 2014
1 parent 7621b4f commit dbed41c
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 0 deletions.
43 changes: 43 additions & 0 deletions 13307130364/assignment4/Largest Rectangle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include<iostream>
#include<stack>
#include<algorithm>
using namespace std;
int func(const int* a,const int count){
stack<int> r;
int temp;
int minium=0;
int s=0;
int w = 0;
for (int i=0; i < count;i++){
if (r.size()==0||a[i] >= a[r.top()]){
r.push(i);
}
else{
while (!r.empty() && a[r.top()] > a[i]){
temp = r.top();
r.pop();
w = r.empty() ? i : (i - r.top()-1);
s = max(s, a[temp] * w);
}
r.push(i);
}
}
return s;
}
int main(){
int count;
int flag = 0;
cin >> count;
while (count){
int *a = new int[count+1];

This comment has been minimized.

Copy link
@whimsycwd

whimsycwd Oct 23, 2014

释放空间

for (int i = 0; i < count; i++)
cin >> a[i];
a[count] = 0;

This comment has been minimized.

Copy link
@whimsycwd

whimsycwd Oct 23, 2014

这里的哨兵加得非常好!!! 赞。

if (flag)
cout << endl;
cout << func(a,count+1);
flag = 1;
cin >> count;
}
return 0;
}
60 changes: 60 additions & 0 deletions 13307130364/assignment4/Rails.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include<iostream>
#include <stack>
#include<string>
using namespace std;
string t="";
void func(const int* a,const int count){
stack<int> p;
int j = 1;
int i = 0;
int flag = 0;
for (; i < count;){
if (a[i] > j)
p.push(j++);
else if(a[i]==j){
j++;
i++;
}
else{
if (a[i] == p.top()){
i++;
p.pop();
}
else{
flag = 0;
break;
}
}
if (j == count + 1 && i == count){
t+= "\nYes";
flag = 1;
break;
}
}
if (!flag)
t += "\nNo";

}
int main(){
int count,temp,flag=1;
cin >> count;
for (; count;cin>>count){
int *a = new int[count];

This comment has been minimized.

Copy link
@whimsycwd

whimsycwd Oct 23, 2014

释放空间

//cout << endl;//Blank line?
while (1){
cin >> temp;
if (temp == 0){
t += "\n";
break;
}

This comment has been minimized.

else{
a[0] = temp;
for (int i = 1; i < count; i++)
cin >> a[i];
func(a,count);
}
}
}
cout << t.substr(1,t.length()-2);
return 0;
}
37 changes: 37 additions & 0 deletions 13307130364/assignment4/Sliding Window.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include<iostream>
using namespace std;
int a[1000005],q[1000005];
void minq(int n,int m){
int tail = 0, head = 1;
for (int i = 1; i <= n; i++){
while (head <= tail&&a[q[tail]] >= a[i]) tail--;
q[++tail] = i;
if (i >= m){
while (q[head]<i - m + 1) head++;
cout<<a[q[head]]<<" ";
}
}
cout << endl;
}
void maxq(int n,int m){
int tail = 0, head = 1;
for (int i = 1; i <= n; i++){
while (head <= tail&&a[q[tail]] <= a[i]) tail--;
q[++tail] = i;
if (i >= m){
while (q[head]<i - m + 1) head++;
cout<<a[q[head]]<<" ";
}
}
}
int main()
{
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++){
cin >> a[i];
}

This comment has been minimized.

minq(n, k);
maxq(n, k);
return 0;
}

0 comments on commit dbed41c

Please sign in to comment.