zoukankan      html  css  js  c++  java
  • Gym 101201I Postman (贪心)

    题意:有个邮递员,要送信,每次最多带 m 封信,有 n 个地方要去送,每个地方有x 封要送,每次都到信全送完了,再回去,对于每个地方,可以送多次直到送够 x 封为止。

    析:一个很简单的贪心,就是先送最远的,如果送完最远的还剩下,那么就送次远的,如果不够了,那么就加上回来的距离,重新带够 m 封信,对于左半轴和右半轴都是独立的,两次计算就好。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <sstream>
    #include <list>
    #include <assert.h>
    #include <bitset>
    #define debug() puts("++++");
    #define gcd(a, b) __gcd(a, b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define fi first
    #define se second
    #define pb push_back
    #define sqr(x) ((x)*(x))
    #define ms(a,b) memset(a, b, sizeof a)
    //#define sz size()
    #define pu push_up
    #define pd push_down
    #define cl clear()
    #define FOR(x,n)  for(int i = (x); i < (n); ++i)
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    #define pii pair<int, int>
    using namespace std;
    
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<LL, int> P;
    const int INF = 0x3f3f3f3f;
    const LL LNF = 1e18;
    const double inf = 1e20;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 1000 + 10;
    const LL mod = 1e9 + 7;
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, 1, 0, -1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline bool is_in(int r, int c) {
      return r >= 0 && r < n && c >= 0 && c < m;
    }
    
    struct Node{
      int pos;
      LL val;
      Node(int p = 0, LL v = 0) : pos(p), val(v) { }
      bool operator < (const Node rhs) const{
        return pos > rhs.pos;
      }
    };
    
    Node node1[maxn], node2[maxn];
    
    int main(){
      scanf("%d %d", &n, &m);
      int cnt1 = 0, cnt2 = 0;
      for(int i = 0; i < n; ++i){
        int a, b;  scanf("%d %d", &a, &b);
        if (a < 0)  node1[cnt1++] = Node(-a, b);
        else  node2[cnt2++] = Node(a, b);
      }
      sort(node1, node1 + cnt1);
      sort(node2, node2 + cnt2);
      LL ans = 0;
      LL pre_left = 0;
      for(int i = 0; i < cnt1; ++i){
        if (pre_left >= node1[i].val){
          pre_left -= node1[i].val;
          node1[i].val = 0;
          continue;
        }
        node1[i].val -= pre_left;
        LL num = (node1[i].val - 1) / m + 1;
        ans += num * node1[i].pos * 2;
        pre_left = num * m - node1[i].val;
      }
      pre_left = 0;
      for (int i = 0; i < cnt2; ++i){
        if (pre_left >= node2[i].val){
          pre_left -= node2[i].val;
          node2[i].val = 0;
          continue;
        }
        node2[i].val -= pre_left;
        LL num = (node2[i].val - 1) / m + 1;
        ans += num * node2[i].pos * 2;
        pre_left = num * m - node2[i].val;
      }
      printf("%I64d
    ", ans);
      return 0;
    }
    

      

  • 相关阅读:
    MVC 4 异步编程简化了
    Plextor 浦科特M7VC性能
    epson Robot 指令集合
    在MFC下面实际演示CCriticalSection 的使用
    如何看MFC程序
    SCADA 必备函数之 :关于消息的函数
    SCADA必备函数 实际测试。
    (转)extern关键字两种场景的使用
    (转)全局变量、extern/static/const区别与联系
    (转 )C++ static、const和static const 以及它们的初始化
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/7580437.html
Copyright © 2011-2022 走看看