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\n", ans);
  return 0;
}

  

时间: 2024-10-15 03:46:10

Gym 101201I Postman (贪心)的相关文章

Gym - 100338E Numbers 贪心

Gym - 100338E 题意:给你n,k问在1-n中能整出k的字典序最小的数.范围1018 思路:比较简单的贪心了,枚举10的幂m,然后加上k-m%k, 更新答案就可以了,数据一定要用unsigned long long,我就在这里挂了几次,查了半天. 1 #include <iostream> 2 #include <cstdio> 3 #include <fstream> 4 #include <algorithm> 5 #include <c

Codeforces Gym 100203E bits-Equalizer 贪心

原题链接:http://codeforces.com/gym/100203/attachments/download/1702/statements.pdf 题解 考虑到交换可以减少一次操作,那么可以的话就尽量交换,设问号的个数是z,上面是1下面是0的个数是x,反过来的是y,那么最后的答案就是z+min(x,y)+abs(x-y).代码是队友写的,我是嘴炮流选手. 代码 #include <stdio.h> #include <algorithm> #include <ios

HDU 5695 ——Gym Class——————【贪心思想,拓扑排序】

Gym Class Time Limit: 6000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 768    Accepted Submission(s): 309 Problem Description 众所周知,度度熊喜欢各类体育活动. 今天,它终于当上了梦寐以求的体育课老师.第一次课上,它发现一个有趣的事情.在上课之前,所有同学要排成一列, 假设最开始每个人有

Gym - 100989H (贪心)

After the data structures exam, students lined up in the cafeteria to have a drink and chat about how much they have enjoyed the exam and how good their professors are. Since it was late in the evening, the cashier has already closed the cash registe

Gym - 100801G: Graph (贪心+set+拓扑)(好题)

题意:给定一个N点M边的有向图,叫你加最多K条边,使得最小拓扑序最大. 思路:不是那么简单的题.  参照了别人的代码,最后想通了. 贪心原则: 用两个单调队列维护, 第一个序列S1单增, 表示当前入度为0的点 ; 第二个序列S2单减,表示需要加边的点. 如果S1的最大值大于S2的最大值,则对其加边. #include<bits/stdc++.h> using namespace std; const int maxn=100010; priority_queue<int>p; pr

GYM 101933I(贪心、大整数)

我读题有问题呀,题目中到底哪里保证数据一定至少是2倍关系了Orz--然后既然题意就是保证了那贪心一下即可,因为如果当前这个大的不选,那剩下一堆逐渐小于上一代的1/2的,凑起来都不如这个大的,更别说答案了. /** * * @author alphawa */ import java.util.*; import java.math.BigInteger; public class Solution { public static class alpha { String name; BigInt

E - Third-Party Software - 2 Gym - 102215E (贪心)

Pavel is developing another game. To do that, he again needs functions available in a third-party library too famous to be called. There are mm functions numbered from 11to mm, and it is known that the ii-th version of the library contains functions

codeforces Gym 100338E Numbers

题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include<bits/stdc++.h> using namespace std; typedef unsigned long long ull; const int maxbit = 19; ull base[maxbit], n, k; void preDeal() { base[0] = 1; for(in

codeforces Gym 100187F F - Doomsday 区间覆盖贪心

F. Doomsday Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/F Description Doomsday comes in t units of time. In anticipation of such a significant event n people prepared m vaults in which, as they think, it will