贪心 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 ...
随机推荐
- php数据访问(查询)
查询:常用关键字查询 和 准确查询 单条件查询 创建添加查询元素 <br /> <form action="main.php" method="post ...
- (转)Delphi工程文件说明
1.DPR: Delphi Project文件,包含了Pascal代码.应用系统的工程文件2.PAS: Pascal文件,Pascal单元的源代码,可以是与窗体有关的单元或是独立的单元.3.DFM:D ...
- ios开发函数(数学函数应用)
今天在计算collectionView存放最小间距的时候用到一函数 " ABS " 顺便就查了一下这些数学函数在代码中的使用.. //----------------------- ...
- eclipse maven tomcat7 热部署
.配置tomcat a.配置jdk b.CATALINA_HOME=c:\tomcat CATALINA_BASE=c:\tomcat .tomcat配置密码 C:\Program Files\oth ...
- [网络流24题]餐巾(cogs 461)
[问题描述] 一个餐厅在相继的N天里,第i天需要Ri块餐巾(i=l,2,-,N).餐厅可以从三种途径获得餐巾. (1)购买新的餐巾,每块需p分: (2)把用过的餐巾送到快洗部,洗一块需m天,费用需f分 ...
- Linux(Ubuntu)之设定开机自启动
分两种:对自建脚本,对已安装服务 对已安装服务, 只需在/etc/rc.local中添加相应的启动命令,如: 重启后,即可自启动服务. 对自建脚本自启动: 创建脚本test,修改其权限为755,如: ...
- iosTest
NSString * url = @"http://192.168.0.11:8000/InterfaceApp/Login?UserName=15995858188&Passwor ...
- NYOJ题目1102Fibonacci数列
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAsQAAAKFCAIAAABEM2gdAAAgAElEQVR4nO3dP1Iqy98H4HcT5i7EmI ...
- 二、JavaScript语言--JS基础--JavaScript进阶篇--DOM对象 控制HTML元素
1.认识DOM 文档对象模型DOM(Document Object Model)定义访问和处理HTML文档的标准方法.DOM 将HTML文档呈现为带有元素.属性和文本的树结构(节点树). 先来看看下面 ...
- c++11中的static
超赞的线程安全的初始化 static plsa::PLSAModel& model() { static plsa::PLSAModel _model = ([&]() { plsa: ...