I.Algorithm Choosing Mushrooms
链接:https://ac.nowcoder.com/acm/contest/908/I
题意:
Baby bear and his good friends are very fond of mushrooms. On this day, they go to 402 mushroom field together. Kuangyeye, the owner of the mushroom field, is very happy to see the children. He is going to give the children some mushrooms, so he takes them to a warehouse.
In the warehouse, there are N baskets in a row, numbered from 1 to N, with some mushrooms in each basket. Baby bear and his friends can take several baskets of mushrooms, but Kuangyeye has several requirements:
·Kuangyeye likes to be neat and tidy. He asks the children to take away only the consecutively numbered baskets when they take the mushrooms. This means that if the children choose the 4th, 5th and 6th baskets of mushrooms, they can't choose the 9th basket unless they also choose the 7th and 8th baskets.
·Kuangyeye likes all of them, so he asks each child to get the same number of mushrooms. This means that the total number of mushrooms the children take away must be P = k * M, where k is an integer and M is the total number of children.
·Kuangyeye made a record of the number of mushrooms in each basket. He asks the children not to put some mushrooms in other baskets or throw them away. This means that when the children take a basket of mushrooms, they must take away all the mushrooms in the basket.
Now given the number of mushrooms in a row of N baskets, please answer:
1. The maximum number of mushrooms that baby bear and his friends can take away.
2. The maximum number of baskets that baby bear and his friends can take away.
Please note that the answers to questions 1 and 2 May not come from the same situation!!!
思路:
用一个二维数组记录原数组的前缀和数组modm的值。
如果sum[j]%m == sum[i]%m 则(sum[j]-sum[i])%m == 0说明j-(i-1)篮子里的蘑菇是m的倍数。(sum为前缀和数组)
找到%m的每个余数去最左和最右的两个位置。%m为0时只用取最右的。
代码:
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long LL;
- const int MAXN = 2e6 + 10;
- const int MOD = 1e9 + 7;
- int n, m, k, t;
- LL fsum[MAXN];
- int Po[MAXN][2];
- int main()
- {
- memset(Po, -1, sizeof(Po));
- scanf("%d%d", &n ,&m);
- LL sum = 0;
- for (int i = 1;i <= n;i++)
- {
- scanf("%lld", &fsum[i]);
- fsum[i] += fsum[i-1];
- }
- for (int i = 1;i <= n;i++)
- {
- int lef = fsum[i] % m;
- if (lef == 0)
- {
- Po[lef][0] = i;
- continue;
- }
- if (Po[lef][0] == -1)
- Po[lef][0] = i;
- else
- Po[lef][1] = i;
- }
- LL res1 = 0;
- int res2 = 0;
- for (int i = 1;i <= m;i++)
- {
- if (Po[i][1] == -1)
- continue;
- res1 = max(res1, fsum[Po[i][1]]-fsum[Po[i][0]]);
- res2 = max(res2, Po[i][1]-Po[i][0]);
- }
- if (Po[0][0] != -1)
- {
- res1 = max(res1, fsum[Po[0][0]]);
- res2 = max(res2, Po[0][0]);
- }
- printf("%lld %d", res1, res2);
- return 0;
- }
I.Algorithm Choosing Mushrooms的更多相关文章
- 吴恩达机器学习笔记47-K均值算法的优化目标、随机初始化与聚类数量的选择(Optimization Objective & Random Initialization & Choosing the Number of Clusters of K-Means Algorithm)
一.K均值算法的优化目标 K-均值最小化问题,是要最小化所有的数据点与其所关联的聚类中心点之间的距离之和,因此 K-均值的代价函数(又称畸变函数 Distortion function)为: 其中
- CF219D. Choosing Capital for Treeland [树形DP]
D. Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes i ...
- Evolutionary Computing: 2. Genetic Algorithm(1)
本篇博文讲述基因算法(Genetic Algorithm),基因算法是最著名的进化算法. 内容依然来自博主的听课记录和教授的PPT. Outline 简单基因算法 个体表达 变异 重组 选择重组还是变 ...
- Study on Algorithm of Selecting Safe Landing Area on Ground During Asteroid Soft Landing (EEIC2013 +161)
OUATTARA Sie, RUAN Xiaogang, Yan yan Institute of Artificial Intelligence and Robots, School of Elec ...
- A Gentle Introduction to the Gradient Boosting Algorithm for Machine Learning
A Gentle Introduction to the Gradient Boosting Algorithm for Machine Learning by Jason Brownlee on S ...
- HDU 4422 The Little Girl who Picks Mushrooms ( 模拟)
Problem Description It's yet another festival season in Gensokyo. Little girl Alice planned to pick ...
- 【codeforce 219D】 Choosing Capital for Treeland (树形DP)
Choosing Capital for Treeland Description The country Treeland consists of n cities, some pairs of t ...
- TIFF6 Packbit algorithm
“Packbits” from ISO 12369 参考TIFF 6.0 Specification,点击TIFF, Version 6.0: @Section 9: PackBits Compres ...
- Shine we together: A innovative dating site using 2012 Nobel Laureate Roth's algorithm
Abstract Our dating site introduced scoring and its related functionalities innovatively, conforming ...
随机推荐
- Linux三种网络-vmware三种网络模式
Host-Only 桥接 NAT VMware虚拟机三种联网方法及原理 一.Brigde——桥接:默认使用VMnet0 1.原理: Bridge 桥"就是一个主机,这个机器拥有两块网卡,分别 ...
- LoadRunner监控图表与配置(三)对系统与网络资源进行监控
1.Windows可监控的性能计数器 2.UNIX可监控的性能计数器 3.性能计数器信息说明 Windows 性能对象 计数器 数值说明 System %Total Processor Time 系统 ...
- 监听输入框变化(oninput,onpropertychange,onchange)
oninput,onpropertychange,onchange: oninput是onpropertychange的非IE浏览器版本,支持firefox和opera等浏览器,但有一点不同,它绑定于 ...
- Ajax动态切换按钮
function changeAjax(str, obj) { var idx = $(obj).parent().parent().index(); if(confirm('确定执行操作么?')) ...
- linux 进程学习笔记-等待子进程结束
<!--[if !supportLists]-->Ÿ <!--[endif]-->等待子进程结束 pid_t waitpid(pid_t pid, int *stat_loc, ...
- aoj 0118 Property Distribution
タナカ氏が HW アールの果樹園を残して亡くなりました.果樹園は東西南北方向に H × W の区画に分けられ.区画ごとにリンゴ.カキ.ミカンが植えられています.タナカ氏はこんな遺言を残していました. ...
- Oracle表空间维护总结
1. 概念:表空间:最大的逻辑存储文件,与物理上的一个或多个数据文件对应,每个数据库至少拥有一个表空间,表空间的大小等于构成表空间的所有数据文件的大小总和,用于存储用户在数据库中存储的所有内容. 2. ...
- POJ1006Biorhythms——中国剩余定理
题目:http://poj.org/problem?id=1006 用扩展欧几里得算法求逆元,使用中国剩余定理: 本题较简单,可以手算直接写出,不过我仍使用了模板. 代码如下: #include< ...
- poj2392磊石头——排序后背包
题目: 首先按限制高度从小到大排序,不会影响可行解,而不排序可能卡掉正确的情况: 用%2滚动数组时一定注意每次复制上一种情况,因为这个WA了好几次. 代码如下: #include<iostrea ...
- Django admin有用的自定义功能
引用园友 无名小妖 的博客 https://www.cnblogs.com/wumingxiaoyao/p/6928297.html 写的很好,但是博客园不能转载,不过我已经点赞了~