Winter fishing in troubled waters day2 sorting algorithm

Disclaimer: This article is a blogger original article, For reprint please contact the bloggers. https://blog.csdn.net/GregoryHanson/article/details/86626670

Finishing knowledge

sort function usage (in ascending order):

sort(vec.begin(),vec.end(),cmp);
//sort(开始地址,结束地址,cmp);

cmp default function is omitted, to rewrite the collation, the override function cmp. Such as change in descending order:

bool cmp(const int &a, const int &b) {
	return a > b;
}

Presetation Error (output format error): Check the end of the line is not a multiple-output space. It can be no space to achieve by the end of the following line of code:

cout<<vec[0];
for (int i = 1; i < N; i++) {
    cout << " " << vec[i];
}

Search for a value in the first several positions in the container:

cout << find(vec.begin(), vec.end(), valueToFind) - vec.begin() + 1 << endl;

With access iterator as an object element, you need to add brackets, such as:

(*iter).begin();

topic

A title

HDU - 1029 Ignatius and the Princess IV

“OK, you are not too bad, em… But you can never pass the next test.” feng5166 says.
“I will tell you an odd number N, and then N integers. There will be a special integer among them, you have to tell me which integer is the special one after I tell you all the integers.” feng5166 says.
“But what is the characteristic of the special integer?” Ignatius asks.
“The integer will appear at least (N+1)/2 times. If you can’t find the right integer, I will kill the Princess, and you will be my dinner, too. Hahahaha…” feng5166 says.
Can you find the special integer for Ignatius?
Input
The input contains several test cases. Each test case contains two lines. The first line consists of an odd integer N(1<=N<=999999) which indicate the number of the integers feng5166 will tell our hero. The second line contains the N integers. The input is terminated by the end of file.
Output
For each test case, you have to output only one line which contains the special number you have found.
Sample Input
5
1 3 2 3 3
11
1 1 1 1 1 5 5 5 5 5 5
7
1 1 1 1 1 1 1
Sample Output
3
5
1

Code:

#define _crt_secure_no_warnings
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string>
#include<cstring>
#include<vector>
#include<stdlib.h>
#include<memory.h>
using namespace std;

int main() {
	int N;
	while (scanf("%d", &N) != EOF) {
		vector<int> vec;
		for (int i = 0; i < N; i++) {
			int temp;
			cin >> temp;
			vec.push_back(temp);
		}
		sort(vec.begin(), vec.end());
		cout << vec[(N - 1) / 2] << endl;
	}
	return 0;
}

B title

HDU - 1040 As Easy As A+B

These days, I am thinking about a question, how can I get a problem as easy as A+B? It is fairly difficulty to do such a thing. Of course, I got it after many waking nights.
Give you some integers, your task is to sort these number ascending (升序).
You should know how easy the problem is now!
Good luck!
Input
Input contains multiple test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow. Each test case contains an integer N (1<=N<=1000 the number of integers to be sorted) and then N integers follow in the same line.
It is guarantied that all integers are in the range of 32-int.
Output
For each case, print the sorting result, and one line one case. Sample Input
2
3 2 1 3
9 1 4 7 2 5 8 3 6 9
Sample Output
1 2 3
1 2 3 4 5 6 7 8 9

This problem prone presetation error, the reason for each row last more spaces. When the output of the note on it.

Code:

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string>
#include<cstring>
#include<vector>
#include<stdlib.h>
#include<memory.h>
using namespace std;

int main() {
	int T;
	cin >> T;
	while (T--) {
		int N;
		cin >> N;
		vector<int> vec;
		for (int i = 0; i < N; i++) {
			int temp;
			cin >> temp;
			vec.push_back(temp);
		}
		sort(vec.begin(), vec.end());
		for (int i = 0; i < N; i++) {
			cout << vec[i];
			if (i != N - 1) {
				cout << " ";
			}
		}
		cout << endl;
	}
	return 0;
}

C title

HDU - 1718 Rank

Jackson wants to know his rank in the class. The professor has posted a list of student numbers and marks. Compute Jackson’s rank in class; that is, if he has the top mark(or is tied for the top mark) his rank is 1; if he has the second best mark(or is tied) his rank is 2, and so on.
Input
The input consist of several test cases. Each case begins with the student number of Jackson, an integer between 10000000 and 99999999. Following the student number are several lines, each containing a student number between 10000000 and 99999999 and a mark between 0 and 100. A line with a student number and mark of 0 terminates each test case. There are no more than 1000 students in the class, and each has a unique student number.
Output
For each test case, output a line giving Jackson’s rank in the class.
Sample Input
20070101
20070102 100
20070101 33
20070103 22
20070106 33
0 0
Sample Output
2

Beginning misunderstood subject, is seeking jack score ranking, not seeking jack score is the first of several high score. This question is multiple sets of input! Multiple sets of input! Multiple sets of input! Several people (including myself) did not notice.

Code:

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string>
#include<cstring>
#include<vector>
#include<stdlib.h>
#include<map>
using namespace std;

bool cmp(const int &a, const int &b) {
	return a > b;
}

int main() {
	int jacknum;
	while (scanf("%d", &jacknum) != EOF) {
		int jackscore;
		vector<int> vec;
		int num;
		int score;
		while (cin >> num >> score) {
			if (num == 0 && score == 0) {
				break;
			}
			if (jacknum == num) {
				jackscore = score;
			}
			vec.push_back(score);
		}
		sort(vec.begin(), vec.end(), cmp);
		cout << find(vec.begin(), vec.end(), jackscore) - vec.begin() + 1 << endl;
	}
	return 0;
}

D title

CodeForces - 137C History

Polycarpus likes studying at school a lot and he is always diligent about his homework. Polycarpus has never had any problems with natural sciences as his great-great-grandfather was the great physicist Seinstein. On the other hand though, Polycarpus has never had an easy time with history.Everybody knows that the World history encompasses exactly n events: the i-th event had continued from the year ai to the year bi inclusive (ai < bi). Polycarpus easily learned the dates when each of n events started and ended (Polycarpus inherited excellent memory from his great-great-granddad). But the teacher gave him a more complicated task: Polycaprus should know when all events began and ended and he should also find out for each event whether it includes another event. Polycarpus’ teacher thinks that an event j includes an event i if aj < ai and bi < bj. Your task is simpler: find the number of events that are included in some other event.
Input
The first input line contains integer n (1 ≤ n ≤ 105) which represents the number of events. Next n lines contain descriptions of the historical events, one event per line. The i + 1 line contains two integers ai and bi (1 ≤ ai < bi ≤ 109) — the beginning and the end of the i-th event. No two events start or finish in the same year, that is, ai ≠ aj, ai ≠ bj, bi ≠ aj, bi ≠ bj for all i, j (where i ≠ j). Events are given in arbitrary order.
Output
Print the only integer — the answer to the problem.Examples
Input
5
1 10
2 9
3 8
4 7
5 6
Output
4
Input
5
1 100
2 50
51 99
52 98
10 60
Output
4
Input
1
1 1000000000
Output
0
Note
In the first example the fifth event is contained in the fourth. Similarly, the fourth event is contained in the third, the third — in the second and the second — in the first.
In the second example all events except the first one are contained in the first.
In the third example only one event, so the answer is 0.

Beginning with a two-cycle, no matter how optimized implementations there is always a test point allows you to overtime, I thought the reason is because with the container, do not replace the wording of the container and found that test point can also reduce a .
Later found a layer of cycling will be able to solve the problem, or a little clever, I did not think this method.

Code:

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string>
#include<cstring>
#include<vector>
#include<stdlib.h>
#include<memory.h>
#include<map>
using namespace std;

bool cmp(const pair<int, int> &a, const pair<int, int> &b) {
	return a.first < b.first;
}

int main() {
	int N;
	cin >> N;
	vector<pair<int, int>> vec;
	int a, b;
	while (N--) {
		cin >> a >> b;
		vec.push_back(pair<int, int>(a, b));
	}
	sort(vec.begin(), vec.end(), cmp);
	int count = 0;
	int maxa = (*vec.begin()).second;
	for (vector<pair<int, int>>::iterator iter = vec.begin();
		iter != vec.end(); ++iter) {
		if ((*iter).second < maxa) {
			count++;
		}
		else {
			maxa = (*iter).second;
		}
	}
	cout << count << endl;
	return 0;
}

Tucao: D title indeed my head is not good, I did not expect one cycle method. The question should be divided in Category D structure of the sort in the third, he made a small mistake people problem.

Guess you like

Origin blog.csdn.net/GregoryHanson/article/details/86626670