voidsolve(){ int M, D; std::cin >> M >> D; int y, m, d; std::cin >> y >> m >> d; d ++; if(d > D){ d -= D; m ++; if(m > M){ m -= M; y ++; } } std::cout << y << " " << m << " " << d << endl;; }
#define int long long #define endl '\n' constint INF = 0x3f3f3f3f;
voidsolve(){ int n, s, m, l; std::cin >> n >> s >> m >> l; int ans = INF; for(int i = 0 ; i <=100 ; i ++){ for(int j = 0; j <= 100; j ++){ for(int k = 0 ; k <= 100; k ++){ int w = i * s + j * m + l * k; if(i * 6 + 8 * j + 12 * k >= n) ans = std::min (ans, w); } } } std::cout << ans << endl; }
B数组为输入的数组, 现有 s记录数组元素的总和,数组 A记录 B数组中大小为 i的
元素个数为 A[i]个,我们在遍历一次 A数组,只要 A[i] > 0就可以将 A[i]赋值为 s - A[i] * i
当然,同时 s -= i * A[i]
最后,再遍历一次数组 B,输出 A[B[i]].
参考代码
#include<bits/stdc++.h>
#define int long long #define endl '\n' constint INF = 0x3f3f3f3f;
voidsolve(){ int n, s = 0, ans = 0,x; std::cin >> n; std::vector<int> a(1e6 + 50, 0ll), b(n, 0ll); for(int i = 0 ; i < n ; i ++) std::cin >> b[i], a[b[i]] ++, s += b[i];
for(int i = 1 ; i <= 1000000 ;i ++){ if(a[i]){ s -= a[i] * i; a[i] = s; } } for(int i = 0 ; i < n ; i ++) std::cout << a[b[i]] << " "; }