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 ...
随机推荐
- 深入理解JVM - 线程安全与锁优化 - 第十三章
线程安全 当多个线程访问一个对象时,如果不用考虑这些线程在运行时环境下的调度和交替执行,也不需要进行额外的同步,或者在调用方法进行任何其他的协调操作,调用这个对象的行为都可以获得正确的结果,那么这个对 ...
- Linux 文本的^M问题
很多人在windows中使用文本编辑器编辑好文本后,传送到linux系统后,使用vi工具打开后发现每一行文本最后都有一个^M号,原因是: 在DOS使用的换行符为 \r\n,我们称为CR(\r)与LF( ...
- HihoCoder1641 : 热门号码([Offer收割]编程练习赛37)(模拟)
描述 1 2 3 ABC DEF 4 5 6 GHI JKL MNO 7 8 9 PQRS TUV WXYZ * 0 # 我们知道电话拨号盘上数字会有若干字母对应,例如2对应ABC,7对应PQRS. ...
- POJ 3258 最小值最大化 二分搜索
题意:牛要到河对岸,在与河岸垂直的一条线上,河中有N块石头,给定河岸宽度L,以及每一块石头离牛所在河岸的距离, 现在去掉M块石头,要求去掉M块石头后,剩下的石头之间以及石头与河岸的最小距离的最大值. ...
- Divide Two Integers-不用'/' '*' '%'操作实现整数的除法
题目描述: 不用 '*' '/' 和 '%' 运算实现两个整数的除法 题目来源:http://oj.leetcode.com/problems/divide-two-integers/ 题目分析: 例 ...
- centos7 install python3
1. 过程 # 1. root权限, 安装依赖 yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-dev ...
- python-pprint打印函数
#!/usr/bin/env python # -*- coding:utf-8 -*- import sys,pprint pprint.pprint(sys.path)
- 如何增加新的PointT类型
博客转载自:http://www.pclcn.org/study/shownews.php?lang=cn&id=286 为了增加新的point类型,首先需要进行定义,例如: struct M ...
- 《精通Spring4.X企业应用开发实战》读后感第四章(BeanFactory和ApllicationContext)
- 6、scala面向对象-对象
一.对象 1.object object,相当于class的单个实例,通常在里面放一些静态的field或者method,第一次调用object的方法时,就会执行object的constructor, ...