寒假算法摸鱼day2 排序

知识点整理

sort函数用法(升序排序):

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

默认状态下cmp函数省略,若要改写排序规则,则重写cmp函数。如改成降序排序:

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

Presetation Error(输出格式错误):检查是不是行末多输出了一个空格。可以用以下代码实现行末没有空格:

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

查找某个值在容器中处于第几个位置:

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

用迭代器作为对象访问元素,需要加括号,如:

(*iter).begin();

题目

A题

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

代码:

#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题

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

这题容易出现presetation error,原因为每行最后多了个空格。输出的时候注意一下就可以了。

代码:

#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题

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

一开始题目理解错了,是求jack分数的排名,不是求jack分数是第几高的分数。这题是多组输入!多组输入!多组输入!好几个人(包括我自己)都没注意到。

代码:

#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题

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.

一开始用了两层循环,不管怎么优化实现都总会有一个测试点让你超时,还以为是因为用了容器的原因,换成不用容器的写法,发现能通过的测试点还减少了一个。
后来发现一层循环就能解决问题了,还是有点巧妙的,一开始没想到这个方法。

代码:

#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;
}

吐槽:D题的确是我脑袋不好,没想到一层循环方法。分类上D题应该分在第三天的结构体排序里的,发题人的一个小失误。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值