贪心 Codeforces Round #289 (Div. 2, ACM ICPC Rules) B. Painting Pebbles
/*
题意:有 n 个piles,第 i 个 piles有 ai 个pebbles,用 k 种颜色去填充所有存在的pebbles,
使得任意两个piles,用颜色c填充的pebbles数量之差 <= 1。
如果不填充某种颜色,就默认数量为0。
1. 贪心:如果个数之间超过k个,那么填充什么颜色都会大于1,巧妙地思维
详细解释:http://blog.csdn.net/haoliang94/article/details/43672617
2. 比较每种填充颜色在n组里使用最多和最少的,若差值<=1,则YES,否则NO
详细解释:http://www.cnblogs.com/windysai/p/4265469.html
*/
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
using namespace std; const int MAXN = + ;
const int INF = 0x3f3f3f3f;
int a[MAXN]; int main(void)
{
#ifndef ONLINE_JUDGE
freopen ("B.in", "r", stdin);
#endif int n, k;
while (~scanf ("%d%d", &n, &k))
{
int mx = -; int mn = INF;
for (int i=; i<=n; ++i)
{
scanf ("%d", &a[i]);
if (mx < a[i]) mx = a[i];
if (mn > a[i]) mn = a[i];
} if (mx - mn <= k)
{
puts ("YES");
for (int i=; i<=n; ++i)
{
int t = ;
for (int j=; j<=a[i]; ++j)
{
printf ("%d%c", ++t, (j==a[i]) ? '\n' : ' ');
if (t == k) t = ;
}
}
}
else
puts ("NO");
} return ;
}
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <vector>
using namespace std; const int MAXN = + ;
const int INF = 0x3f3f3f3f;
int a[MAXN];
int cnt[MAXN];
vector <int> v[MAXN]; int main(void)
{
#ifndef ONLINE_JUDGE
freopen ("B.in", "r", stdin);
#endif int n, k;
while (~scanf ("%d%d", &n, &k))
{
for (int i=; i<=MAXN; ++i)
v[i].clear (); for (int i=; i<=n; ++i)
{
scanf ("%d", &a[i]);
memset (cnt, , sizeof (cnt));
int t = ;
for (int j=; j<=a[i]; ++j)
{
cnt[++t]++;
if (t == k) t = ;
}
for (int j=; j<=k; ++j)
{
v[j].push_back (cnt[j]);
}
} bool flag = true;
vector<int>:: iterator it1, it2;
for (int i=; i<=k; ++i)
{
sort (v[i].begin (), v[i].end ());
it1 = v[i].end () - ;
it2 = v[i].begin ();
if (*it1 - *it2 > )
{
flag = false; break;
}
} if (flag)
{
puts ("YES");
for (int i=; i<=n; ++i)
{
int t = ;
for (int j=; j<=a[i]; ++j)
{
printf ("%d%c", ++t, (j==a[i]) ? '\n' : ' ');
if (t == k) t = ;
}
}
}
else
puts ("NO");
} return ;
}
解法2
贪心 Codeforces Round #289 (Div. 2, ACM ICPC Rules) B. Painting Pebbles的更多相关文章
- codeforces水题100道 第十八题 Codeforces Round #289 (Div. 2, ACM ICPC Rules) A. Maximum in Table (brute force)
题目链接:http://www.codeforces.com/problemset/problem/509/A题意:f[i][1]=f[1][i]=1,f[i][j]=f[i-1][j]+f[i][j ...
- Codeforces Round #289 (Div. 2, ACM ICPC Rules) E. Pretty Song 算贡献+前缀和
E. Pretty Song time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 递推水题 Codeforces Round #289 (Div. 2, ACM ICPC Rules) A. Maximum in Table
题目传送门 /* 模拟递推水题 */ #include <cstdio> #include <iostream> #include <cmath> #include ...
- Codeforces Round #289 (Div. 2, ACM ICPC Rules) A. Maximum in Table【递推】
A. Maximum in Table time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #622 (Div. 2) B. Different Rules(数学)
Codeforces Round #622 (Div. 2) B. Different Rules 题意: 你在参加一个比赛,最终按两场分赛的排名之和排名,每场分赛中不存在名次并列,给出参赛人数 n ...
- 贪心 Codeforces Round #288 (Div. 2) B. Anton and currency you all know
题目传送门 /* 题意:从前面找一个数字和末尾数字调换使得变成偶数且为最大 贪心:考虑两种情况:1. 有偶数且比末尾数字大(flag标记):2. 有偶数但都比末尾数字小(x位置标记) 仿照别人写的,再 ...
- 贪心 Codeforces Round #301 (Div. 2) B. School Marks
题目传送门 /* 贪心:首先要注意,y是中位数的要求:先把其他的都设置为1,那么最多有(n-1)/2个比y小的,cnt记录比y小的个数 num1是输出的1的个数,numy是除此之外的数都为y,此时的n ...
- 贪心 Codeforces Round #297 (Div. 2) C. Ilya and Sticks
题目传送门 /* 题意:给n个棍子,组成的矩形面积和最大,每根棍子可以-1 贪心:排序后,相邻的进行比较,若可以读入x[p++],然后两两相乘相加就可以了 */ #include <cstdio ...
- 贪心 Codeforces Round #304 (Div. 2) B. Soldier and Badges
题目传送门 /* 题意:问最少增加多少值使变成递增序列 贪心:排序后,每一个值改为前一个值+1,有可能a[i-1] = a[i] + 1,所以要 >= */ #include <cstdi ...
随机推荐
- MyBatis之代理开发模式
1 mybatis-Dao的代理开发模式 Dao:数据访问对象 原来:定义dao接口,在定义dao的实现类 dao的代理开发模式 只需要定义dao接口,由mybatis产生dao接口的实现类. 1.1 ...
- grep(Global Regular Expression Print)
.grep -iwr --color 'hellp' /home/weblogic/demo 或者 grep -iw --color 'hellp' /home/weblogic/demo/* (-i ...
- vector reserve与resize区别
vector 的reserve增加了vector的capacity,但是它的size没有改变!而resize改变了vector的capacity同时也增加了它的size!原因如下:reserve是容器 ...
- Java动态代理一Proxy
什么是动态代理? 动态代理可以提供对另一个对象的访问,同时隐藏实际对象的具体事实.代理一般会实现它所表示的实际对象的接口.代理可以访问实际对象,但是延迟实现实际对象的部分功能,实际对象实现系统的实际功 ...
- NYOJ题目57 6174问题
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAscAAAJLCAIAAACE5qzaAAAgAElEQVR4nO3dMXKrutvH8XcT6bOQ1C ...
- JavaScript基础——数据类型
JavaScript使用数据类型来确定如何处理被分配给一个变量的数据.变量的类型决定了你可以对变量进行什么操作,如循环或者执行.下面描述了最常用的变量类型. 字符串(String):此数据类型将字符数 ...
- php 复习
<?php 一.php基础语法1.输出语句:echo print print_r var_dump() 2.php是弱类型语言强制转换类型: (类型)变量 settype(变量,类型) 3.变量 ...
- slf4i + logback 配置
一.所需jar包: slf4j-api-1.6.1.jar logback-classic-0.9.24.jar logback-core-0.9.24.jar 二.logback.xml配置示例: ...
- php开发(CI框架使用)
年前接了一个外包项目,要求使用PHP,琢磨来琢磨去,感叹道PHP框架实在是太多了!去知乎搜索一轮,最后决定使用CI, 相关议论如下:https://www.zhihu.com/question/216 ...
- IBM Rational AppScan 无法记录登录序列 分类: 数据安全 2015-03-18 16:46 158人阅读 评论(0) 收藏
为了测试漏洞,我在本地部署了一个站点,为http://localhost/app,并且有登录页面. 但是尝试多次,都无法记录登录页面.此时尝试了在hosts文件中,自定义了一个域名 127.0.0.1 ...