题目链接:http://codeforces.com/contest/1151/problem/D

题目大意:

  有n个学生排成一队(序号从1到n),每个学生有2个评定标准(a, b),设每个学生的位置为j,则每个学生所要交的学费为a * (j - 1) + b * (n - j),要求把这些学生从新排序使得整体所交学费最小。

分析:

  变换一下学费公式 = j * (a - b) + n * b - a,由于是求和,所以可以只看j * (a - b)部分,这就很显而易见了,(a - b)大的要排前面,(a - b)小的要排后面。

代码如下:

 #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std; #define INIT() std::ios::sync_with_stdio(false);std::cin.tie(0);
#define Rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
#define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin()) #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define MP make_pair
#define PB push_back
#define ft first
#define sd second template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
} template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
for (auto &x: v)
in >> x;
return in;
} template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
out << "[" << p.first << ", " << p.second << "]" << "\n";
return out;
} typedef long long LL;
typedef unsigned long long uLL;
typedef pair< double, double > PDD;
typedef pair< int, int > PII;
typedef set< int > SI;
typedef vector< int > VI;
typedef map< int, int > MII;
const double EPS = 1e-;
const int inf = 1e9 + ;
const LL mod = 1e9 + ;
const int maxN = 1e5 + ;
const LL ONE = ;
const LL evenBits = 0xaaaaaaaaaaaaaaaa;
const LL oddBits = 0x5555555555555555; LL n, ans; struct Student{
LL a, b; LL disSa(int x) const {
return a * (x - ) + b * (n - x);
}
}; Student stu[maxN]; bool cmp(const Student &x, const Student &y) {
return x.a - x.b > y.a - y.b;
} int main(){
INIT();
cin >> n;
For(i, , n) cin >> stu[i].a >> stu[i].b; sort(stu + , stu + n + , cmp); For(i, , n) ans += stu[i].disSa(i);
cout << ans << endl;
return ;
}

CodeForces 1151D Stas and the Queue at the Buffet的更多相关文章

  1. Codeforces Round #553 (Div. 2) D. Stas and the Queue at the Buffet 贪心+公式转化

    题意 给出n个pair (a,b) 把它放在线性序列上 1--n 上 使得  sum(a*(j-1)+b*(n-j))  最小 思路 :对式子进行合并 同类项 有:    j*(a-b)+  (-a+ ...

  2. B类——Stas and the Queue at the Buffet

    http://codeforces.com/contest/1151/problem/D 题意: n个学生,每个学生都有自己的位置,最后要使

  3. 洛谷 题解 CF1151D 【Stas and the Queue at the Buffet】

    本蒟蒻又双叒叕被爆踩辣!!! 题目链接 这道题我个人觉得没有紫题的水平. 步入正题 先看题: 共有n个人,每个人2个属性,a,b; 窝们要求的是总的不满意度最小,最满意度的公式是什么? \(ai * ...

  4. CodeForces Round #553 Div2

    A. Maxim and Biology 代码: #include <bits/stdc++.h> using namespace std; int N; string s; int mi ...

  5. Codeforces Round #553 (Div. 2) 题解

    昨晚深夜修仙上紫记,虽然不错还是很有遗憾的. A. Maxim and Biology 看完就会做的题,然而手速跟不上 #include<cstdio> #include<iostr ...

  6. Codeforces Round #553 (Div. 2)/codeforces1151

    CodeForces1151 Maxim and Biology 解析: 题目大意 每次可以使原串中的一个字符\(+1/-1\),\(Z + 1\to A, A -1\to Z\),求至少修改多少次可 ...

  7. codeforces 1151 D

    SM的水题. codeforces 1151D 当时写对了,因为第一题卡了,,然后这题就没细想,原来是没开longlong. 题意:n个位置每个位置有a和b,让sum=(每个点的左面的点的数量*a+右 ...

  8. codeforces D. Queue 找规律+递推

    题目链接: http://codeforces.com/problemset/problem/353/D?mobile=true H. Queue time limit per test 1 seco ...

  9. Codeforces Beta Round #75 (Div. 1 Only) B. Queue 线段树+二分

    B. Queue Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 codeforces.com/problemset/problem/91/B Descrip ...

随机推荐

  1. AspNetCore 多环境配置 以及注册 消费Consul

    本文主要记录 CoreApi 的多环境配置以及如何消费在consul中注册的Api 服务 1.创建三个CoreApi 我们在项目中创建三个站点分别为 UserServices “用户服务”,Order ...

  2. Eclipse设置全局用户名

    -Duser.name=你的名字

  3. hash一致性

    参照:https://www.cnblogs.com/moonandstar08/p/5405991.html 参照:http://www.cnblogs.com/haippy/archive/201 ...

  4. 洛谷P3366 【模板】最小生成树(Boruvka算法)

    题意 题目链接 Sol 自己yy着写了一下Boruvka算法. 算法思想很简单,就是每次贪心的用两个联通块之间最小的边去合并. 复杂度\(O(n \log n)\),然鹅没有Kruskal跑的快,但是 ...

  5. 生鲜配送管理系统_升鲜宝V2.0 价格组功能 操作说明_15382353715

    价格组功能是B端供应链系统,必不可少的一个功能,其主要实现不同的客户不同的价格,B端系统有一个最大的不同就是,有些商品后台下单人员能看到的.有些商品在销售的那一瞬间,还不知道价格.所以这些商品只有后台 ...

  6. 自动化测试 Appium之Python运行环境搭建 Part1

    Appium之Python运行环境搭建 Part1 by:授客 QQ:1033553122 实践环境 Win7 Python 3.4.0 JAVA JDK 1.8.0_121 node.js8.11. ...

  7. 智能指针std::weak_ptr

    std::weak_ptr 避免shared_ptr内存泄漏的利器.

  8. (办公)mybatis工作中常见的问题(不定时更新)

    1.mybatis的like查询的方式. <if test="shopName != null and shopName != ''"> <bind name=& ...

  9. DataPipeline |《Apache Kafka实战》作者胡夕:Apache Kafka监控与调优

    胡夕 <Apache Kafka实战>作者,北航计算机硕士毕业,现任某互金公司计算平台总监,曾就职于IBM.搜狗.微博等公司.国内活跃的Kafka代码贡献者. 前言 虽然目前Apache ...

  10. ORA-279 signalled during: alter database recover logfile

    在RMAN的RECOVER还原过程中,RMAN界面正常,但是检查.刷新告警日志,发现告警日志里面有ORA-279,如下所示: alter database recover logfile '/u06/ ...