zoukankan      html  css  js  c++  java
  • P4266 [USACO18FEB]Rest Stops

    ##一眼过去

    什么都没想到
    认真分析了一下,觉得可能是个dp或者贪心。
    脑玩了一下,大概搞出来个贪心策略,就是直接选当前能选的最优选择就好,证明也很容易,反证即可
    还可以顺便证明草的tastiness 相同但地点不同时对权值贡献没有影响。
    贪心用堆维护就好

    #include <queue>
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    const int MAXN = 1e5 + 20;
    typedef pair<int, int> P;
    typedef long long ll;
    inline int read()
    {
    	int x = 0; char ch = getchar(); bool f = false;
    	while(!isdigit(ch)) f |= (ch == '-'), ch = getchar();
    	while(isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
    	return f ? -x : x;
    }
    
    priority_queue<P> q; 
    int L, N, rf, rb;
    
    int main()
    {
    	cin>>L>>N>>rf>>rb;
    	for(int i = 1; i <= N; i++){
    		P pos;
    		pos.second = read(), pos.first = read();
    		q.push(pos);
    	}
    
    	int pb = 0; ll ans = 0;
    	while(!q.empty()){
    		P pos = q.top(); q.pop();
    		if(pos.second <= pb) continue;
    		ans += (1LL * rf - rb) * (1LL * pos.second - pb) * pos.first;
    		pb = pos.second;
    	}
    	cout<<ans<<endl;
    	return 0;
    }
    
  • 相关阅读:
    python Exception中的raise、assert
    python循环语句与其他编程语言不同之处
    【转】最大似然估计
    尝试发表代码
    奇异分解(SVD)
    Python学习笔记3-string
    梯度下降法
    前后端分离
    Trie树
    HashMap的工作原理以及代码实现,为什么要转换成红黑树?
  • 原文地址:https://www.cnblogs.com/wsmrxc/p/9419798.html
Copyright © 2011-2022 走看看