赞
踩
第一题,一个老头分财产,老大拿最大的,老二拿第二大。接下来总财产小的拿,如果一样就老大拿,判断最后多的比少的多拿多少?
双指针
- #include<iostream>
- #include<vector>
- #include<algorithm>
-
- using namespace std;
- int main()
- {
-
- int n;
- cin >> n;
- vector<int>vec(2*n);
- for (int k = 0; k < 2 * n; k++)
- {
-
- cin>>vec[k];
- }
-
- sort(vec.begin(), vec.end());
- //双指针
- int i = vec.size() - 1, j = vec.size() - 2;
- int son1 = vec[i], son2 =vec[j];
- while (i > 0 && j > 0)
- {
- if (son1 > son2)
- {
- j--;
- i -= 3;
- son1 = son1 + vec[i];
- son2 = son2 + vec[j];
-
- }
- else
- {
- i--;
- j -= 3;
- son1 = son1 + vec[i];
- son2 = son2 + vec[j];
-
- }
-
- }
- cout << son1 - son2;
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。