Topcoder SRM 634 div.2 [ABC]

Source: Internet
Author: User
Topcoder SRM 634 div.2 [ABC]

ACM

Topcoder SRM 634

After the game, I felt that orz could not be done at the scene. I can't say much about it.

Level one-mountainranges]

Question:
Several of the Q & A sequences are completely greater than the adjacent peaks.

Analysis:
Not much.

Code:

/**  Author:      illuz <iilluzen[at]gmail.com>*  File:        one.cpp*  Create Date: 2014-09-26 21:01:23*  Descripton:   */#include <cstdio>#include <vector>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define repf(i,a,b) for(int i=(a);i<=(b);i++)typedef long long ll;const int N = 0;class MountainRanges {public:int countPeaks(vector<int> h) {int ret = 0, sz = h.size();if (sz == 1) {return 1;}if (sz == 2) {return h[0] != h[1];}if (h[0] > h[1])ret++;if (h[sz - 1] > h[sz - 2])ret++;// cout << sz << ' ' << ret;repf (i, 1, sz - 2) {if (h[i] > h[i - 1] && h[i] > h[i + 1])ret++, i++;}return ret;}};int main() {// ios_base::sync_with_stdio(0);MountainRanges a;int n, t;vector<int> v;cin >> n;while (n--) {cin >> t;v.push_back(t);}cout << a.countPeaks(v) << endl;return 0;}


Level Two-ShoppingSurveyDiv2 (Mathematics]

Question:
In a survey, N people participated in the survey. You got a survey result, that is, several people bought each item.
Now you only have the results of this survey, that is, the I-th item has been purchased by someone else.
I have bought at least a few of you.

Analysis:

We can consider the number of items that nobody buys, that is, each item should have been bought by N people.sum(N - s[i]).
At this time, we can allocate these things to everyone as much as possible, so the rest of the people can not only buy all, that is, the least. If the score (N >= sum(N - s[i])), Then everyone may not have bought all of them.

Code:

/**  Author:      illuz <iilluzen[at]gmail.com>*  File:        two.cpp*  Create Date: 2014-09-26 22:36:58*  Descripton:   */#include <cstdio>#include <vector>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define repf(i,a,b) for(int i=(a);i<=(b);i++)typedef long long ll;const int N = 0;class ShoppingSurveyDiv2 {public:int minValue(int N, vector<int> s) {int sz = s.size(), sum = 0;repf (i, 0, sz - 1) sum += s[i];int t = N - (N * sz - sum);if (t < 0) t = 0;return t;}};int main() {// ios_base::sync_with_stdio(0);int n, m, t;vector<int> v;cin >> n >> m;repf (i, 0, m - 1) {cin >> t;v.push_back(t);}ShoppingSurveyDiv2 a;cout << a.minValue(n, v);return 0;}


Level three-specialstrings [structure]

Question:
Set a special string
1. 01 string
2. divide it into two strings at any position, and the Lexicographic Order is always smaller than the latter.

Now we provide a special string to ensure that you can ask what the next string in the Lexicographic Order of the same length is. If it is the last one, it will return null.

Analysis:

Obviously, this string must be the next in the Lexicographic Order, that is, the 01 string must be carried, so we must first add 1 to it, that is, change the last 0 to 1, the following changes to X indicate unknown.
To01101111011110111As an example01101111011111XXX.

Can 0 be followed by condition 2? Obviously, no.

We should first consider modifying the front part of the vertex.
Since the part before modification has strictly followed condition 2, and the position of the original 0 is changed to 1, so the position of the previous surface is used as the split point, the second half of the string is bigger than the original one, so the previous part does not need to be changed.

The main problem is in the following section. We have modified the vertex to a split point. Or, based on the example above, the front and back strings become01101111011111AndXXX.
Then the subsequent x string will be larger than the previous one. Because the next Lexicographic Order is required, the X string can be copied to the previous part directly, and then + 1 will work.

How can we prove that this string can also meet condition 2 when the split point is followed? Obviously, because the latter part is completely copied to the front of + 1, therefore, the Split points are the same as those after the Split points. The front point is already compliant with condition 2, so there is no problem in the future. Think about it.

In this way, this string is obtained.

Code:

/**  Author:      illuz <iilluzen[at]gmail.com>*  File:        three.cpp*  Create Date: 2014-09-26 21:57:10*  Descripton:   */#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define repf(i,a,b) for(int i=(a);i<=(b);i++)typedef long long ll;const int N = 0;class SpecialStrings {public:string findNext(string s) {int len = s.length(), pos = 0;for (int i = len - 1; i >= 0; i--) {if (s[i] == '0') {pos = i;break;}}if (pos == 0)return "";s[pos] = '1';repf (i, pos + 1, len - 1)s[i] = s[i - pos - 1];for (int i = len - 1; i >= 0; i--) {if (s[i] == '0') {s[i] = '1';return s;}}}};int main() {// ios_base::sync_with_stdio(0);SpecialStrings a;string s;cin >> s;cout << a.findNext(s) << endl;return 0;}



Topcoder SRM 634 div.2 [ABC]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.