codeforces 670D1 - Magic Powder - 1
2016-05-06 11:51
399 查看
题目的意思,就是给出制作一种食物的每种材料所需的量,然后再给出每种材料目前总共的数量,问最多可以制作多少个这样的食物。
贪心。首先求出每种材料是总共有多少个这样的材料,然后由小到大排序,然后再用一个数组存那个后一个大的量的材料减去前面所有小的量的差,因为比如,有3种材料,每种材料的量分别是1, 2,4, 就需要sum[1] = 1, sum[2] = 3,然后就是贪心的计算了。
贪心。首先求出每种材料是总共有多少个这样的材料,然后由小到大排序,然后再用一个数组存那个后一个大的量的材料减去前面所有小的量的差,因为比如,有3种材料,每种材料的量分别是1, 2,4, 就需要sum[1] = 1, sum[2] = 3,然后就是贪心的计算了。
#include <bits/stdc++.h> using namespace std; const int MAX = 1e3 + 5; //int a[MAX], b[MAX], c[MAX]; struct NODE { int a, b, c; }node[MAX]; int sum[MAX]; bool comp(NODE a, NODE b) { return a.c < b.c; } const int INF = 0x3f3f3f; int main() { int n, k; scanf("%d%d", &n, &k); int tot = 0; for (int i = 0; i < n; ++i) { scanf("%d", &node[i].a); // tot += node[i].a; } for (int i = 0; i < n; ++i) { scanf("%d", &node[i].b); //k += node[i].b; } //int smallest = INF; for (int i = 0; i < n; ++i) { node[i].c = node[i].b / node[i].a; //if (smallest > c[i]) // smallest = c[i]; } sort(node, node + n, comp); sum[0] = node[0].a; for (int i = 1; i < n; ++i) { sum[i] = sum[i - 1] + node[i].a; } //for (int i = 0; i < n; ++i) // cout << node[i].c << endl; int i = 0; int res = node[0].c; while (i <= n - 1 && k > 0) { //cout << k << endl; k += (node[i].b % node[i].a); //cout << "ss" << k << endl; if (k >= (sum[i]* (node[i + 1].c - node[i].c)) && node[i + 1].c != 0 && node[i + 1].c > node[i].c) { k -= (sum[i] * (node[i + 1].c - node[i].c)); res += node[i + 1].c - node[i].c; //cout << "k = " << k << endl; } else if (k < (sum[i]* (node[i + 1].c - node[i].c)) && node[i + 1].c != 0 && node[i + 1].c > node[i].c) { //if (node[i + 1].c > node[i].c) //{ res += k / (sum[i]); break; //} } else if (node[i + 1].a == 0) { res += k / (sum[i]); //cout << "here" << endl; break; } //else // break; i++; //cout << "res" << res << endl; } cout << res << endl; return 0; }
相关文章推荐
- 矩阵的幂 斐波那契数列
- java获取时间
- hog源码分析
- oracle insert into 和 update
- Freetype字体引擎分析与指南
- 关于 The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine. 异常处理
- Spring+SpringMVC+Mybatis 多数据源整合(转)
- LayoutInflater的简单认识
- Mysql导入sql文件乱码问题
- Cannot change version of project facet Dynamic web
- 路由技术在企业当中的具体应用之二(DHCP)
- 去除 UISearchBar 多余的黑线
- 房间声学原理与Schroeder混响算法实现
- leetcode.275. H-Index II
- .net Remoting 的工作原理是什么?
- C++第五次上机实验作业
- 45
- TortoiseGit使用密钥连接服务器
- Android采用双缓冲技术实现画板
- C++第5次实验