Christy is interning at HackerRank. One day she has to distribute some chocolates to her colleagues. She is biased towards her friends and may have distributed the chocolates unequally. One of the program managers gets to know this and orders Christy to make sure everyone gets equal number of chocolates.

But to make things difficult for the intern, she is ordered to equalize the number of chocolates for every colleague in the following manner,

For every operation, she can choose one of her colleagues and can do one of the three things.

  1. She can give one chocolate to every colleague other than chosen one.
  2. She can give two chocolates to every colleague other than chosen one.
  3. She can give five chocolates to every colleague other than chosen one.

Calculate minimum number of such operations needed to ensure that every colleague has the same number of chocolates.

Input Format

First line contains an integer  denoting the number of testcases.  testcases follow. 
Each testcase has  lines. First line of each testcase contains an integer  denoting the number of colleagues. Second line contains N space separated integers denoting the current number of chocolates each colleague has.

Constraints

Number of initial chocolates each colleague has <

Output Format

lines, each containing the minimum number of operations needed to make sure all colleagues have the same number of chocolates.

Sample Input

1
4
2 2 3 7

Sample Output

2

Explanation

1st operation: Christy increases all elements by 1 except 3rd one 
2 2 3 7 -> 3 3 3 8
2nd operation: Christy increases all element by 5 except last one
3 3 3 8 -> 8 8 8 8

 
 
题意:
  给你n个数
  你每次操作可以选n-1个位置,每个位置+(1,2,5)
  问你最少多少次操作使得n个数一致
题解:
  选n-1个位置+上(1,2,5)
  也就是选一个位置-(1,2,5)
  按照这个找到最佳的一致的那个数就行了
#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const double Pi = acos(-1.0);
const int N = 6e3+, M = 1e3+, mod = ,inf = 2e9; int dp[N][N],a[];
int main() {
for(int i = ;i <= ; ++i)
for(int j = ; j <= ; ++j) dp[i][j] = inf; for(int i = ; i <= ; ++i) {
dp[i][i] = ;
for(int j = ; j < i; ++j) {
if(i->=j)
dp[i][j] = min(dp[i-][j]+,dp[i][j]);
if(i->=j)
dp[i][j] = min(dp[i-][j]+,dp[i][j]);
if(i->=j)
dp[i][j] = min(dp[i-][j]+,dp[i][j]);
}
}
int T,n;
scanf("%d",&T);
while(T--) {
scanf("%d",&n);
int mi = inf;
for(int i = ; i <= n; ++i) scanf("%d",&a[i]),mi = min(mi,a[i]+);
int ans = ;
for(int i = ; i <= n; ++i) {
ans += dp[a[i]+][mi];
}
for(int i = ; i <= mi; ++i) {
int sum = ;
for(int j = ; j <= n; ++j) {
sum += dp[a[j]+][i];
}
ans = min(ans,sum);
}
printf("%d\n",ans);
}
return ;
}

Hackrank Equal DP的更多相关文章

  1. Codeforces 1188D Make Equal DP

    题意:给你个序列,你可以给某个数加上2的幂次,问最少多少次可以让所有的数相等. 思路(官方题解):我们先给序列排序,假设bit(c)为c的二进制数中1的个数,假设所有的数最后都成为了x, 显然x &g ...

  2. Hackrank Candies DP

    题目链接:传送门 题意: n个学生站一行,老师给每个学生发至少一个糖 相邻学生,a[i] > a[i-1] 的话,那么右边学生的糖一定要发得比左边学生的糖多 问你满足条件这个老师总共最少的发多少 ...

  3. HDU 1217 Arbitrage(Floyd的应用)

    给出一些国家之间的汇率,看看能否从中发现某些肮脏的......朋友交易. 这是Floyd的应用,dp思想,每次都选取最大值,最后看看自己跟自己的.....交易是否大于一.... #include< ...

  4. HDU 6595 Everything Is Generated In Equal Probability (期望dp,线性推导)

    Everything Is Generated In Equal Probability \[ Time Limit: 1000 ms\quad Memory Limit: 131072 kB \] ...

  5. UvaLive6661 Equal Sum Sets dfs或dp

    UvaLive6661 PDF题目 题意:让你用1~n中k个不同的数组成s,求有多少种组法. 题解: DFS或者DP或打表. 1.DFS 由于数据范围很小,直接dfs每种组法统计个数即可. //#pr ...

  6. Codeforces 622C Not Equal on a Segment 【线段树 Or DP】

    题目链接: http://codeforces.com/problemset/problem/622/C 题意: 给定序列,若干查询,每个查询给定区间和t,输出区间内任意一个不等于t的元素的位置. 分 ...

  7. Hackrank Kingdom Division 树形DP

    题目链接:传送门 题意: 给你一棵树,n个点 每个点可以染成红色和蓝色 但是红色的点与其相邻的点中必须有红色节点,蓝色也是 问你有多少种染色的方案 题解: 树形dp 先转化为有根树,取1为根 设定dp ...

  8. [UVALive 6661 Equal Sum Sets] (dfs 或 dp)

    题意: 求从不超过 N 的正整数其中选取 K 个不同的数字,组成和为 S 的方法数. 1 <= N <= 20  1 <= K<= 10  1 <= S <= 15 ...

  9. 【HDOJ6595】Everything Is Generated In Equal Probability(期望DP)

    题意:给定一个N,随机从[1,N]里产生一个n, 然后随机产生一个n个数的全排列,求出n的逆序数对的数量并累加ans, 然后随机地取出这个全排列中的一个子序列,重复这个过程,直到为空,求ans在模99 ...

随机推荐

  1. redis:安装配置主从

    1.安装依赖包 yum install gcc gcc-c++ -y 2.下载安装包,解压 cd /usr/local/src/wget http://download.redis.io/releas ...

  2. js事件默认行为

    事件默认行为: 当一个事件发生的时候浏览器自己默认做的事情 怎么阻止? 当前这个行为是什么事件触发的,然后在这个事件的处理函数中使用 return false; 但是return false 阻止的是 ...

  3. npm start问题

    问题:在执行命令npm start 是出现下列问题: npm [] WARN invalid config loglevel="notice" [] npm WARN invali ...

  4. 【URAL 1989】 Subpalindromes(线段树维护哈希)

    Description You have a string and queries of two types: replace i'th character of the string by char ...

  5. 【HDU 6153】A Secret (KMP)

    Problem Description Today is the birthday of SF,so VS gives two strings S1,S2 to SF as a present,whi ...

  6. Codeforces Round #388 (Div. 2) A+B+C!

    A. Bachgold Problem 任何一个数都可以由1和2组成,由于n是大于等于2的,也就是可以由2和3组成.要求最多的素数即素数越小越好,很明显2越多越好,如果n为奇数则再输出一个3即可. i ...

  7. 人生第一场CTF的解题报告(部分)

    解题报告 濮元杰部分: 王者归来: 120 场景 小王入职了一段时间,最近有点无聊.Web安全项目不多,白天看着其他项目组的同事忙得热火朝天,小王有点坐不住了,这也许是新人都会有的想法,乐于助人.想到 ...

  8. 【思维+贪心】codeforces Game of the Rows

    http://codeforces.com/contest/839/problem/B [题意] 给定n组人,告诉每组人的人数,这些人要在飞机上坐座位 飞机上座位的分布看图可以知道,12  3456 ...

  9. 【并查集】F.find the most comfortable road

    https://www.bnuoj.com/v3/contest_show.php?cid=9146#problem/F [题意] 给定n个城市和m条带权边,q次查询,问某两个城市之间的所有路径中最大 ...

  10. bzoj5108 [CodePlus2017]可做题 位运算dp+离散

    [CodePlus2017]可做题 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 87  Solved: 63[Submit][Status][Dis ...