10月24日备战Noip2018模拟赛20(A组)

T1 Cz礼物

题目描述

有Ñ种物品,第I种物品的价格为六,每天最多购买XI个。

有米天,第我天c♂x有无线的钱,他会不停购买能买得起的最贵的物品(送给c♀x的礼物当然要挑贵一些)。你需要求出他每天会购买多少个物品。

输入格式

第一行两个整数N,M。接下来Ñ行每行两个整数v [I]中,x [i]中。接下来中号行每行一个整数W [i]中。

输出格式

米行每行一个整数,第I行表示第我天购买的物品数量。

输入样例

3 3
1 1
2 2
3 3
5
10
15

输出样例

2
4
6

数据范围

对于20%的数据,N,m≤1000。

对于另外40%的数据,X1 = 1。

对于100%的数据,N,m≤100000,1≤vi≤109,1≤xi≤10000,0≤wi≤10^ 18。


思路

二分 +暴力

首先按照礼物的价值降序排列,因为c♂x非常喜欢c♀x,要尽量给c♀x买贵的东西。

然后用前缀和预先处理买下全部前我种物品要花费的前,以及前我种物品的数量

就然后可以愉快的二分了

1.二分查找出最多可以买第我件到第Ĵ件的全部物品;

2那么此时剩下的钱肯定是不能全部买下第就j + 1件物品的,那么可以只买一部分;。

3.二分查找出小于剩下的钱第ķ件物品的单价,重复1 .;

时间复杂度是

代码

#include <iostream>
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <cstring>using namespace std;const int MAXN = 1e5 + 5;struct Node{int cost;int amount;
}a[MAXN];int n, m, u, v, tot;
int preNum[MAXN];
long long money;
long long preSum[MAXN];inline long long read ();
inline bool cmp (const Node &A, const Node & B);
inline int lowerBoundDifference (int l, int r, long long x);
inline int lowerBound (int l, int r, int x);int main ()
{//freopen ("cz.in", "r", stdin);//freopen ("cz.out", "w", stdout);n = read (), m = read ();for (register int i = 1; i <= n; i ++){a[i].cost = read (), a[i].amount = read ();}sort (a + 1, a + 1 + n, cmp);//for (int i = 1; i <= n; i ++) printf ("\t%d\n", a[i].cost);memset (preSum, 0, sizeof (preSum));memset (preNum, 0, sizeof (preNum));for (int i = 1; i <= n; i ++){preSum[i] = preSum[i - 1] + 1ll * a[i].cost * a[i].amount;preNum[i] = preNum[i - 1] + a[i].amount;//printf ("preSum[%d] = %d, preNum[%d] = %d\n", i, preSum[i], i, preNum[i]);}while (m --){money = read ();tot = 0;v = 1;while (v <= n){u = lowerBoundDifference (v, n, money);//printf ("u = %d\n", u);tot += preNum[u] - preNum[v - 1];money -= preSum[u] - preSum[v - 1];//printf ("tot = %d, money = %d\n", tot, money);v = u + 1;//printf ("v = %d\n", v);if (v > n) break;tot += money / a[v].cost;money %= a[v].cost;//printf ("tot = %d, money = %d\n", tot, money);v = lowerBound (v + 1, n, money);//printf ("v = %d\n", v);}printf ("%d\n", tot);}//fclose (stdin);//fclose (stdout);return 0;
}inline long long read ()
{char ch = getchar ();int f = 1;while (!isdigit (ch)){if (ch == '-') f = -1;ch = getchar ();}long long x = 0;while (isdigit (ch)){x = x * 10 + ch - '0';ch = getchar ();}return x * f;
}
inline bool cmp (const Node &A, const Node &B)
{return A.cost > B.cost;
}inline int lowerBoundDifference (int l, int r, long long x)
{int mid;int left = l;int right = r;while (left <= right){mid = (right - left) / 2 + left;if (preSum[mid] - preSum[l - 1] <= x) left = mid + 1;else right = mid - 1;}return right;
}
inline int lowerBound (int l, int r, int x)
{int mid;int left = l;int right = r;while (left <= right){mid = (right - left) / 2 + left;if (a[mid].cost <= x) right = mid - 1;else left = mid + 1;}return left;
}

备战Noip2018模拟赛20 (A组) T1 Cz 礼物相关推荐

  1. 备战Noip2018模拟赛3(B组)T1 Apple 韬韬抢苹果

    10月3日备战Noip2018模拟赛3(B组) T1 Apple韬韬抢苹果 题目描述 又到了收获的季节,树上结了许多韬韬,错了,是许多苹果,有很多个小韬韬都来摘苹果.每个韬韬都想要最大的苹果,所以发生 ...

  2. 备战Noip2018模拟赛10(B组) T1 Max 和最大

    10月20日备战Noip2018模拟赛10 T1 Max和最大 题目描述 CYF的黑题,偏题,怪题,黑科技题,大码农题都做腻了,于是她想做一下签到水题,她希望从有一个长度为ň的整数序列(A1,A2,. ...

  3. 备战Noip2018模拟赛7(B组)T1 Voting 兔子选举

    10月5日备战Noip2018模拟赛7(B组) T1 Voting 兔子选举 题目描述 兔子常常感到孤独,所以一组的兔子决定走到一起,并举行选美比赛,以确定它们之间谁拥有最漂亮的耳朵.规则如下: 每只 ...

  4. 备战Noip2018模拟赛11(B组)T4 Path 好路线

    10月27日备战Noip2018模拟赛11(B组) T4路径好路线 题目描述 nodgd在旅游.现在,nodgd要从城市的西北角走到东南角去.这个城市的道路并不平坦,nodgd希望找出一条相对比较好走 ...

  5. 备战Noip2018模拟赛3(B组) T2 Dance 开场舞蹈

    10月3日备战Noip2018模拟赛3(B组) T2 Dance 开场舞蹈 题目描述 在全世界人民的期盼下,2008年北京奥林匹克运动会终于隆重召开了! 为了展示中华民族博大精深的优秀传统文化,负责开 ...

  6. 备战Noip2018模拟赛5(B组)T2 Tree 采果子

    10月4日备战Noip2018模拟赛5(B组) T2 Tree采果子 题目描述 LYL大牛今天心情不错,于是走到埃及郊外旅游.他边走边向四周望望,发现周围有许多果树.这些树之间互相到达的时间LYL是知 ...

  7. 备战Noip2018模拟赛7(B组)T2 Pearl 数数

    10月5日备战Noip2018模拟赛7(B组) T2珍珠数 题目描述 给定Ñ个整数,求值在某个范围内的数的个数. 输入格式 第一行为正整数ñ. 第二行有Ñ个整数(0 <=数值<= 231- ...

  8. 【蓝桥杯Web】大一小白参与蓝桥杯模拟赛二期web组体会

    目录 前言 一.相关比赛介绍 1.ACM国际大学生程序设计竞赛 2.蓝桥杯 3.GPLT团队程序设计天梯赛 4.leetcode周赛和双周赛 5.PAT 二.蓝桥杯 1.应该参加蓝桥杯吗? 2.如何进 ...

  9. EZ 2018 06 17 NOIP2018 模拟赛(十九)

    这次的题目难得的水,但是由于许多哲学的原因,第二题题意表述很迷. 然后是真的猜题意了搞了. 不过这样都可以涨Rating我也是服了. Upt:链接莫名又消失了 A. 「NOIP2017模拟赛11.03 ...

最新文章

  1. 树形dp——树的最远距离 hdu2196
  2. 经验分享:如何在自己的创业中,用上GPT-3等AI大模型
  3. 机器学习研究者必知的八个神经网络架构
  4. DevExpress助您开发Windows8 UI应用程序
  5. ElasticSearch和mysql对比
  6. 数据结构之二叉树的创建
  7. httpclient proxy 方式ssl 死锁 socketRead0问题解决
  8. 开发者应该关注的五项Web新兴技术:WebGL和SVG名列其中
  9. MAC PHP Composer
  10. 梅花易数C语言实现(六十四卦卦辞用的是python)用了python
  11. MySQL数据库知识的总结
  12. Spring Boot (Vue3+ElementPlus+Axios+MyBatisPlus +Spring Boot 前后端分离)
  13. 初识C语言--第二弹
  14. Android | WIFI Direct -1 Basic knowledge
  15. 恋与抽卡模拟器网页_《公主连结》抽卡模拟器网站是什么 抽卡模拟器地址介绍...
  16. python通过ip获取地理位置等ip信息
  17. 数据结构—约瑟夫环问题(C语言版)
  18. GIt远程仓库pull拉取代码
  19. MVC WebAPI 三层分布式框架开发
  20. php赤兔cms商业破解,赤兔CMS影院开源免费PHP版一键采集综合影视资源站

热门文章

  1. mysql与oracle like小知识
  2. Jdk8新特性一:jdk8新特性之default关键字、日期类、Base64类、Optional
  3. 修改 ChatGLM2-6B 自我认知的 Lora 微调教程
  4. ChatGPT带来的影响有哪些
  5. 【MyBatis】延迟加载
  6. ipad android tv,先河私有云持续大动作:继iPad/Android TV端/系统分享功能后,还有新玩法?...
  7. 关于 RenderControl 的问题
  8. Win10系统安装FT2232D系列USB转串口驱动
  9. 关于updatepanel
  10. Excel自学笔记 第四节 如何实现隐藏身份证后四位