-
Notifications
You must be signed in to change notification settings - Fork 31
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
#include <iostream> | ||
#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; | ||
} | ||
|
||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
提供了很赞的解法。
and 找出错误了么?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
找出了……
一个判断条件下的return语句没写。。
看来越是低级的错反而越容易犯。