首页 > 其他分享 >Stas and the Queue at the Buffet

Stas and the Queue at the Buffet

时间:2023-01-07 00:44:56浏览次数:48  
标签:std const Stas int ll Buffet Queue

Stas and the Queue at the Buffet

题解:贪心+思维

我们看到公式\(ai⋅(j−1)+bi⋅(n−j)\),直接进行化简得到

\[(a[i]-b[i])*j+n*b[i]-a[i] \]

我们就知道\(n*b[i]-a[i]\)是个定值,所以我们把差值最大的放在前面,把最小的放在后面,贪心放即可

#include <bits/stdc++.h>
#define Zeoy std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0)
#define all(x) (x).begin(), (x).end()
#define endl '\n'
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-9;
const int N = 1e5 + 10;
struct node
{
    ll a, b, d;
    bool operator<(const node &t) const
    {
        return d > t.d;
    }
} a[N];
int main(void)
{
    Zeoy;
    int t = 1;
    // cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        for (int i = 1; i <= n; ++i)
        {
            cin >> a[i].a >> a[i].b;
            a[i].d = a[i].a - a[i].b;
        }
        sort(a + 1, a + 1 + n);
        ll sum = 0;
        for (int i = 1; i <= n; ++i)
        {
            sum += a[i].d * i + n * a[i].b - a[i].a;
        }
        cout << sum << endl;
    }
    return 0;
}

标签:std,const,Stas,int,ll,Buffet,Queue
From: https://www.cnblogs.com/Zeoy-kkk/p/17032010.html

相关文章

  • Java容器之PriorityQueue源码分析
    一、简介优先级队列,是0个或多个元素的集合,集合中的每个元素都有一个权重值,每次出队都弹出优先级最大或最小的元素。一般来说,优先级队列使用堆来实现。二、源码分析2.1......
  • windows下springboot项目部署elk日志系统教程elasticsearch与logstash与kibana
    1.项目中加入依赖:compile'net.logstash.logback:logstash-logback-encoder:6.0'如果是maven项目的话:字符串中的冒号为隔断,第一个为groupid,第二个为artifactId,第三个为versi......
  • Java并发容器之LinkedBlockingQueue源码分析
    一、简介LinkedBlockingQueue是java并发包下一个以单链表实现的阻塞队列,它是线程安全的,至于它是不是有界的,请看下面的分析。二、源码分析2.1属性 //容量private......
  • RabbitMQ学习笔记03:Work Queues
    参考资料:RabbitMQtutorial-WorkQueues—RabbitMQ   前言这篇文章我们会创建一个WorkQueue,它会在多个worker(即消费者consumer)中分发耗时的任务。WorkQueue......
  • git stash保存和恢复进度
    gitstash保存和恢复进度一、应用场景当正在dev分支上开发某个项目,这时项目中出现一个bug,需要紧急修复,但是正在开发的内容只是完成一半,还不想提交,这时可以用gitstash命......
  • BlockingQueue之LifoSemMPMCQueue
    【五种阻塞队列】  【先看LifoSem】lastinfirstout信号量通知的queue【调用栈】  ./fbcode_builder_getdeps-ZrootZfollyZbuildZfbcode_builder-root/buil......
  • ELK日志系统:Elasticsearch + Logstash + Kibana 搭建教程 good
     elk中kibna搜索时,如果搜索 包含 单个 双引号 的字符串时:"'/goods"&&"result\":true"  使用双引号包起来作为一个短语搜索"likeGecko"#字段也可以按页面左侧显示......
  • logstash 时区问题
    场景日志信息按天写入索引,output代码如下output{elasticsearch{hosts=>["192.168.23.20:9200","192.168.23.21:9200","192.168.23.22:9200","192.168......
  • 集合5 - Queue
    Queue-ArrayBlockingQueue底层实现--数组Array(数组)-Blocking(创建对象时的容量,多了就堵塞)-Queue(先进先出)publicclass_Queue{publicstaticvoidm......
  • 第十三章《集合》第4节:Queue集合
    ​Queue是一个接口,它也是Collection接口的子接口。Queue用来模拟队列这种数据结构。队列这种数据结构最明显的特征是元素先入先出,队列的头部的元素是所有元素中最先进入队列......