#include<iostream> #include<vector> #include<algorithm> using namespace std; int main() { int t, n, l, r, k,a; vector<int>v; cin >> t; while (t--) { cin >> n>>l>>r>>k; for (int i = 0; i < n; ++i) { cin >> a; if (a <= r && a >= l) { v.push_back(a); } } vector<int>::iterator it = v.begin(); sort(v.begin(), v.end()); int i = 0; while (i<v.size()&&k >= 0) { k -= v[i]; ++i; } if (k < 0) cout << i - 1 << endl; else cout << i << endl; v.clear(); } }
#include<iostream> #include<vector> #include<algorithm> using namespace std; struct node { int num; long long val; }a; bool cmp(node& a, node& b) { return a.val > b.val; } int main() { int t,n; vector<node>v; cin >> t; while (t--) { cin >> n; for (int i = 0; i < n; ++i) { cin >> a.val; a.num = i; v.push_back(a); } sort(v.begin(), v.end(), cmp); int* s = new int[n]; int j = 1, k = -1; long long sum = 0, z = 0; for (int i = 0; i < n; ++i) { if (i % 2 == 0) s[v[i].num] = j, j += 1,z++; else s[v[i].num] = k, k -= 1; //这里!原本sum是longlong,v[i].val , z都是int,会造成算术溢出,结果错误! sum =sum+ v[i].val * z; } cout << sum*2 << endl; cout << 0 << " "; for (int i = 0; i < n; ++i) cout << s[i] << " "; cout << endl; v.clear(); } }
赛后一定补题,下周若没补题每人发个红包(逃