A Mean Inequality

题目大意

给你一个数n和2n个数,把这2n个数凑成一个环,且每个数都不等于左右两边的数的和的一半,求出一个数的排列

主要思路

首先考虑怎么才能让当前数不等于左右两边的数的和的一半,如果左右两边的数都大于或者小于当前数,那么一定不会出现当前数等于左右两边的数的和的一半,所以我们把数组排序,然后取一个最大的数放在第一个位置,取一个最小的数放在第二个位置,取次大的数放在第三个位置…以此类推, 由于题目保证一定有解这种构造方法一定正确

  1. #include <iostream>
  2. #include <cstring>
  3. #include <string>
  4. #include <algorithm>
  5. #include <stack>
  6. #include <string>
  7. #include <cmath>
  8. #include <unordered_map>
  9. #include <queue>
  10. using namespace std;
  11. #define int long long
  12. #define debug(a) cout << #a << " = " << a << endl;
  13. #define x first
  14. #define y second
  15. typedef pair<int, int> P;
  16. const int N = 30;
  17. int n, a[2 * N];
  18. int ans[2 * N];
  19. signed main(void)
  20. {
  21. std::ios::sync_with_stdio(false);
  22. int T;
  23. cin >> T;
  24. while(T--)
  25. {
  26. cin >> n;
  27. for(int i = 0; i < 2 * n; i++) cin >> a[i];
  28. sort(a, a + 2 * n);
  29. int cnt = 0;
  30. for(int i = 0, j = 2 * n - 1; i <= j; i++, j--)
  31. {
  32. ans[cnt++] = a[j];
  33. ans[cnt++] = a[i];
  34. }
  35. for(int i = 0; i < 2 * n; i++) cout << ans[i] << ' ';
  36. }
  37. return 0;
  38. }

B I Hate 1111

题目大意

有11,111,1111…这些数,给你一个数x,问你是否x能被这些数凑成

主要思路

定理:a和b互质那么他们不能凑出的最大数是a*b-a-b

我们用这个性质算出11和111不能凑出的最大数是1099,那么!!大于1099的数都能凑出来!!

小于1099的直接打表即可

  1. #include <iostream>
  2. #include <cstring>
  3. #include <string>
  4. #include <algorithm>
  5. #include <stack>
  6. #include <string>
  7. #include <cmath>
  8. #include <unordered_map>
  9. #include <queue>
  10. using namespace std;
  11. #define int long long
  12. #define debug(a) cout << #a << " = " << a << endl;
  13. #define x first
  14. #define y second
  15. typedef pair<int, int> P;
  16. const int N = 2010;
  17. int f[N];
  18. signed main(void)
  19. {
  20. std::ios::sync_with_stdio(false);
  21. int T;
  22. cin >> T;
  23. for(int i = 0; i * 11 <= 1099; i++)//计算出前1099有哪些数能被凑成
  24. {
  25. for(int j = 0; i * 11 + j * 111 <= 1099; j++)
  26. {
  27. f[i * 11 + j * 111] = 1;
  28. }
  29. }
  30. while(T--)
  31. {
  32. int x;
  33. cin >> x;
  34. if(x > 1099) puts("YES");
  35. else
  36. {
  37. if(f[x]) puts("YES");
  38. else puts("NO");
  39. }
  40. }
  41. return 0;
  42. }

C Potions (Hard Version)

C2和C1区别仅为数据范围不同,那么直接冲C2

题目大意

给你n个数,你的初始健康值为0,从左到右选择,你可以选当前数或者不选当前数,唯一的要求是你的健康值不能为负数,问最多能选择的数的个数

主要思路

反悔型贪心。

首先我们用一个小根堆(优先队列)存储当前已经选择的负数的数有哪些。

  • 假设选择当前数后健康值小于0,那么当前数一定是负数,那么我们先选择当前数,并且放入优先队列中,然后在优先队列中找出负数中最小的数,并且不选择此数即可
  • 假设选择当前数后健康值大于等于0,那么也选择这个数,如果该数为负数,则放入优先队列
  1. #include <iostream>
  2. #include <cstring>
  3. #include <string>
  4. #include <algorithm>
  5. #include <stack>
  6. #include <string>
  7. #include <cmath>
  8. #include <unordered_map>
  9. #include <queue>
  10. using namespace std;
  11. #define int long long
  12. #define debug(a) cout << #a << " = " << a << endl;
  13. #define x first
  14. #define y second
  15. typedef pair<int, int> P;
  16. const int N = 200010;
  17. int f[N][2], g[N][2], a[N], n;
  18. signed main(void)
  19. {
  20. std::ios::sync_with_stdio(false);
  21. cin >> n;
  22. for(int i = 0; i < n; i++) cin >> a[i];
  23. priority_queue<int, vector<int>, greater<int>> q;
  24. int res = 0;
  25. int s = 0;
  26. for(int i = 0; i < n; i++)
  27. {
  28. if(s + a[i] < 0)
  29. {
  30. q.push(a[i]);
  31. s += a[i];
  32. int t = q.top();
  33. s -= t;
  34. q.pop();
  35. }
  36. else
  37. {
  38. s += a[i];
  39. res++;
  40. if(a[i] < 0) q.push(a[i]);
  41. }
  42. }
  43. cout << res << endl;
  44. return 0;
  45. }

D题放一放,会了再补充!