#include <cstdio> #include <string> #include <algorithm> #include <set> #include <vector> #include <iterator> using namespace std; #define REP(i,n) for(int i=0;i<(n);++i) #define FOR(i,a,b) for(int i=(a);i<=(b);++i) #define FORD(i,a,b) for(int i=(a);i>=(b);--i) #define FOREACH(t, i,c) for(t::iterator i=(c).begin();i!=(c).end();++i) #define ZERO(a) memset(a,0,sizeof(a)) template<class T> inline int size(const T &c) { return c.size(); } #define LL long long vector<pair<LL, LL> > polana; LL current_k; bool cmp_value(const pair<LL, LL> &a, const pair<LL, LL> b) { LL v1 = current_k * a.first + a.second; LL v2 = current_k * b.first + b.second; if (v1 == v2) { if (a.first == b.first) { return a.second > b.second; } return a.first > b.first; } return v1 < v2; } void print_vec(const char* msg, vector<pair<LL, LL> > &v) { printf("%s ", msg); for (int i = 0; i < v.size(); i++) { printf("[%lld=(%lld, %lld)] ", v[i].first * current_k + v[i].second, v[i].first, v[i].second); } printf("\n"); } int main() { LL n; scanf("%lld", &n); LL ai, bi; for (int i = 0; i < n; i++) { scanf("%lld%lld", &ai, &bi); polana.push_back(make_pair(ai, bi)); } for (LL k = 0; k < n; k++) { current_k = k; sort(polana.begin(), polana.end(), cmp_value); LL res = 0; vector<LL> ais; for (LL y = n - 1; y >= n - 1 - k; y--) { res += k * polana[y].first + polana[y].second; ais.push_back(polana[y].first); } LL half_res = res; sort(ais.begin(), ais.end()); LL len = ais.size(); //for (int i = 0; i < len; i++) { // printf("%lld ", ais[i]); //} //printf("\n"); for (LL i = 0; i < len; i++) { res -= i * ais[len - i - 1]; } //printf("day %lld: res=%lld half_res=%lld", k, res, half_res); //print_vec("", polana); printf("%lld\n", res); } return 0; } /* 4 3 1 2 1 2 4 1 5 5 11 18 26 */
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | #include <cstdio> #include <string> #include <algorithm> #include <set> #include <vector> #include <iterator> using namespace std; #define REP(i,n) for(int i=0;i<(n);++i) #define FOR(i,a,b) for(int i=(a);i<=(b);++i) #define FORD(i,a,b) for(int i=(a);i>=(b);--i) #define FOREACH(t, i,c) for(t::iterator i=(c).begin();i!=(c).end();++i) #define ZERO(a) memset(a,0,sizeof(a)) template<class T> inline int size(const T &c) { return c.size(); } #define LL long long vector<pair<LL, LL> > polana; LL current_k; bool cmp_value(const pair<LL, LL> &a, const pair<LL, LL> b) { LL v1 = current_k * a.first + a.second; LL v2 = current_k * b.first + b.second; if (v1 == v2) { if (a.first == b.first) { return a.second > b.second; } return a.first > b.first; } return v1 < v2; } void print_vec(const char* msg, vector<pair<LL, LL> > &v) { printf("%s ", msg); for (int i = 0; i < v.size(); i++) { printf("[%lld=(%lld, %lld)] ", v[i].first * current_k + v[i].second, v[i].first, v[i].second); } printf("\n"); } int main() { LL n; scanf("%lld", &n); LL ai, bi; for (int i = 0; i < n; i++) { scanf("%lld%lld", &ai, &bi); polana.push_back(make_pair(ai, bi)); } for (LL k = 0; k < n; k++) { current_k = k; sort(polana.begin(), polana.end(), cmp_value); LL res = 0; vector<LL> ais; for (LL y = n - 1; y >= n - 1 - k; y--) { res += k * polana[y].first + polana[y].second; ais.push_back(polana[y].first); } LL half_res = res; sort(ais.begin(), ais.end()); LL len = ais.size(); //for (int i = 0; i < len; i++) { // printf("%lld ", ais[i]); //} //printf("\n"); for (LL i = 0; i < len; i++) { res -= i * ais[len - i - 1]; } //printf("day %lld: res=%lld half_res=%lld", k, res, half_res); //print_vec("", polana); printf("%lld\n", res); } return 0; } /* 4 3 1 2 1 2 4 1 5 5 11 18 26 */ |