Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

12307130066_XuanYuan_Assignment4 #273

Merged
merged 2 commits into from
Oct 24, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions 12307130066/assignment4/A.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <iostream>
#include <stack>

using namespace std;

struct Pair {
int value;
int x;
Pair(int a, int b) {
value = a;
x = b;
}
};

int main() {
while(1) {
int numbers, tmp;
cin >> numbers;
//cout << "......" << endl;
if(numbers == 0)
break;
stack<Pair> cols;
long long answer = 0;
int i;
for(i = 0; i < numbers; i++) {
cin >> tmp;
int x = i;
while(!cols.empty() && cols.top().value > tmp) {
x = cols.top().x;
answer = max(answer, (long long)(i - x) * cols.top().value);
cols.pop();
}
Pair temp(tmp, x);
cols.push(temp);
}
tmp = 0;
int x = i;
while(!cols.empty() && cols.top().value > tmp) {
x = cols.top().x;
answer = max(answer, (long long)(i - x) * cols.top().value);
cols.pop();
}
Pair temp(tmp, x);
cols.push(temp);
cout << answer << endl;
}
return 0;
}
48 changes: 48 additions & 0 deletions 12307130066/assignment4/B.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <iostream>
#include <stack>

using namespace std;

struct Pair {
int value;
int x;
Pair(int a, int b) {
value = a;
x = b;
}
};

int main() {
while(1) {
int numbers, tmp;
cin >> numbers;
//cout << "......" << endl;
if(numbers == 0)
break;
stack<Pair> cols;
long long answer = 0;
int i;
for(i = 0; i < numbers; i++) {
cin >> tmp;
int x = i;
while(!cols.empty() && cols.top().value > tmp) {
x = cols.top().x;
answer = max(answer, (long long)(i - x) * cols.top().value);
cols.pop();
}
Pair temp(tmp, x);
cols.push(temp);
}
tmp = 0;
int x = i;
while(!cols.empty() && cols.top().value > tmp) {
x = cols.top().x;
answer = max(answer, (long long)(i - x) * cols.top().value);
cols.pop();
}
Pair temp(tmp, x);
cols.push(temp);
cout << answer << endl;
}
return 0;
}
130 changes: 130 additions & 0 deletions 12307130066/assignment4/C.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#include <iostream>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

提供了很赞的解法。

and 找出错误了么?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

找出了……
一个判断条件下的return语句没写。。
看来越是低级的错反而越容易犯。

#include <stack>

using namespace std;

class SStack {
private:
stack<int> content;
stack<int> maximums;
stack<int> minimums;
public:
/*省略构造和析构函数*/
void push(int anum) {
content.push(anum);
if(maximums.empty()) {
maximums.push(anum);
minimums.push(anum);
} else {
if(maximums.top() > anum)
maximums.push(maximums.top());
else
maximums.push(anum);

if(minimums.top() < anum)
minimums.push(minimums.top());
else
minimums.push(anum);
}
}
int top() { return content.top(); }
void pop() {
content.pop();
maximums.pop();
minimums.pop();
}
int max() { return maximums.top(); }
int min() { return minimums.top(); }
int empty() { return content.empty(); }
};

class SQueue {
private:
SStack left;
SStack right;
public:
/*省略构造和析构函数*/
void push (int anum) {
right.push(anum);
}
int top() {
if(left.empty()) {
while(!right.empty()) {
left.push(right.top());
right.pop();
}
}
return left.top();
}
void pop() {
if(left.empty()) {
while(!right.empty()) {
left.push(right.top());
right.pop();
}
}
left.pop();
}
int max() {
if(left.empty())
return right.max();
if(right.empty())
return left.max();

if(left.max() > right.max())
return left.max();
else
return right.max();
}
int min() {
if(left.empty())
return right.min();
if(right.empty())
return left.min();

if(left.min() < right.min())
return left.min();
else
return right.min();
}
int empty() {
if(left.empty() && right.empty())
return 1;
else
return 0;
}
};

int main() {
int length, width, tmp;
cin >> length >> width;
SQueue window;
int result_size = length - width + 1;
int maxs[result_size], mins[result_size];
for(int i = 0; i < width; i++) {
cin >> tmp;
window.push(tmp);
}
maxs[0] = window.max();
mins[0] = window.min();
for(int j = 1; j < result_size; j++) {
window.pop();
cin >> tmp;
window.push(tmp);
maxs[j] = window.max();
mins[j] = window.min();
}
//cout << endl;
for(int a = 0; a < result_size; a++) {
cout << mins[a] << " ";
}
cout << endl;
for(int b = 0; b < result_size; b++) {
cout << maxs[b] << " ";
}
cout << endl;
return 0;
}