Gym 100703I---Endeavor for perfection(尺取)
题目链接
http://codeforces.com/problemset/gymProblem/100703/I
Description
Statements
As a matter of fact, Dragon knows what Prince is interested in now. Prince uses to spend his rare off days learning different courses and trainings. But Dragon doubts whether he should tell Princess about it.
Prince decided that he needs some extra knowledge and skills. He chose n fields in which he wanted to gain knowledge and skills most of all. After this, he learned that m1, m2, ..., mn courses and trainings of each of fields exist.
Prince took a close look at descriptions of courses and trainings and drew a table, in which sij is a value by which ith skill is increased after studying jth course (j = 1, 2, ..., mi).
Prince believes that his basic knowledge and skills in all these fields are negligible, so they can be considered zero. He wants to evolve his knowledge and skills harmonically. In his opinion, he will reach the greatest harmony if he chooses one course for each field in such a way that difference between the highest and the lowest their increases would be as minimum as possible.
Your task is to find the courses which Prince should choose.
Input
The first line contains integer n (1 ≤ n ≤ 200) — the number of fields which Prince is interested in.
The second line contains n integers m1, m2, ..., mn (1 ≤ mj ≤ 1000, j = 1, 2, ..., n) — the number of courses for each of fields.
The next n lines contain values sij (1 ≤ sij ≤ 109) — knowledges and skills, which Prince would gain at the courses. The first of thesen lines contains values s11, s12, ..., s1m1, the second — values s21, s22, ..., s2m2, etc.
The values sij are listed in the numerical order of courses for each of the fields.
Output
In the first line print one integer — minimum difference between the highest and the lowest numbers of increase.
In the second line print n integers — numbers of courses which Prince should choose. List the numbers in the same order in which the fields are listed.
If there is more than one answer — choose any of them.
Sample Input
2
2 3
4 3
3 1 2
0
2 1
4
3 5 4 5
8 7 15
3 10 4 8 5
4 4 4 5
1 2 12 8 9
3
2 5 4 4 题意:输入一个n,然后输入n个数,表示接下来输入的n行每行的数的个数,求在每行中选择一个数使得这n个数的最大值与最小值的差最小,输出最小的差值和每行选择的数的列号; 思路:尺取,将n行的数放在一起从小到大排序,定义s=0和e=0,表示s~e的一段区间,e向右移动,直到这个区间包含n行的数,那么node[e].x-node[s].x便是从这个区间选择的n行数的最小差值,然后s++,再让e右移,计算区间n行数最小差值...... 代码如下:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <bitset>
using namespace std;
const int M=1e9+;
const int maxn=2e5+;
int A[],cnt[],vis[];
struct Node
{
int x,h,l;
}node[maxn],ans[];
bool cmp1(const Node s1,const Node s2)
{
return s1.x<s2.x;
}
bool cmp2(const Node s1,const Node s2)
{
return s1.h<s2.h;
} int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
memset(cnt,,sizeof(cnt));
memset(vis,,sizeof(vis));
for(int i=;i<n;i++)
scanf("%d",&A[i]);
int tot=;
for(int i=;i<n;i++)
for(int j=;j<=A[i];j++)
{
scanf("%d",&node[tot].x);
node[tot].h=i;
node[tot++].l=j;
}
sort(node,node+tot,cmp1);
int tmp=M,s=,e=,sum=;
int pos1,pos2;
while()
{
while(e<tot&&sum<n){
cnt[node[e].h]++;
if(cnt[node[e].h]==) sum++;
e++;
}
if(sum<n) break;
if(node[e-].x-node[s].x<tmp){
tmp=node[e-].x-node[s].x;
pos1=s;
pos2=e-;
}
if(tmp==) break;
if(cnt[node[s].h]==) sum--;
cnt[node[s].h]--;
s++;
}
int p=;
for(int i=pos1;i<=pos2;i++)
{
if(vis[node[i].h]==)
{
vis[node[i].h]=;
ans[p].h=node[i].h;
ans[p++].l=node[i].l;
}
}
sort(ans,ans+n,cmp2);
printf("%d\n",tmp);
for(int i=;i<n;i++)
printf("%d%c",ans[i].l,(i+==n)?'\n':' ');
}
return ;
}
Gym 100703I---Endeavor for perfection(尺取)的更多相关文章
- CF GYM 100703I Endeavor for perfection
题意:有n个学习领域,每个领域有m个课程,学习第i个领域的第j个课程可以获得sij个技能点,在每个领域中选择一个课程,要求获得的n个技能点的最大值减最小值最小,输出符合要求的策略. 解法:尺取法.将课 ...
- Gym 101257G:24(尺取)
http://codeforces.com/gym/101257/problem/GGym 101257G 题意:给出n个人,和一个数s,接下来给出每个人当前的分数和输掉的概率.当一个人输了之后就会掉 ...
- NOJ 1072 The longest same color grid(尺取)
Problem 1072: The longest same color grid Time Limits: 1000 MS Memory Limits: 65536 KB 64-bit in ...
- hdu 4123 Bob’s Race 树的直径+rmq+尺取
Bob’s Race Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Probl ...
- Codeforces Round #116 (Div. 2, ACM-ICPC Rules) E. Cubes (尺取)
题目链接:http://codeforces.com/problemset/problem/180/E 给你n个数,每个数代表一种颜色,给你1到m的m种颜色.最多可以删k个数,问你最长连续相同颜色的序 ...
- poj2566尺取变形
Signals of most probably extra-terrestrial origin have been received and digitalized by The Aeronaut ...
- poj2100还是尺取
King George has recently decided that he would like to have a new design for the royal graveyard. Th ...
- hdu 6231 -- K-th Number(二分+尺取)
题目链接 Problem Description Alice are given an array A[1..N] with N numbers. Now Alice want to build an ...
- Codeforces 939E Maximize! (三分 || 尺取)
<题目链接> 题目大意:给定一段序列,每次进行两次操作,输入1 x代表插入x元素(x元素一定大于等于之前的所有元素),或者输入2,表示输出这个序列的任意子集$s$,使得$max(s)-me ...
随机推荐
- Android笔记——permission权限大全
访问登记属性 android.permission.ACCESS_CHECKIN_PROPERTIES ,读取或写入登记check-in数据库属性表的权限 获取错略位置 android.permiss ...
- Text文档编码识别方法
Text文档编码识别方法 在做文档读取的时候,时常碰到编码格式不正确的问题,而要怎么样正确识别文档的编码格式,成了很多程序员的一块心病,今天我就要试着治好这块心病,这段代码的浓缩来自上千万文档的数据分 ...
- oc集合
本人之前学习过一年半ios开发 由于行情太过凄惨,故转前端.心在前端,苹果亦难忘!把我平时的笔记作出给大家总结! 回顾之前的知识 便利初始化函数:框架类库中的一些类有一系列的以init开头的方法,这些 ...
- jQuery插件:jqGrid使用(一)
1. Loading Data Load from JavaScript Array BundleConfig.cs using System.Web; using System.Web.Optimi ...
- Unity 特殊文件夹 : 位置不能随便放
有以下几个文件夹: Assets 用来存放资源的文件夹,包括各种材质.模型等 Editor 编辑器类等脚本 Editor Default Resources Editor scripts can ma ...
- .cn根服务器被攻击之后
如果是互联网行业的人员应该知道,8月25日凌晨,大批的“.cn”域名的网站都无法访问,当然包括weibo.cn等大型网站.个人比较奇怪的一件事情是,微博PC网页版是:www.weibo.com,而mo ...
- MyCAT全局序列号
在实现分库分表的情况下,数据库自增主键已无法保证自增主键的全局唯一.为此,MyCat 提供了全局sequence,并且提供了包含本地配置和数据库配置等多种实现方式. 本地文件方式 原理:此方式MyCA ...
- geotrellis使用(十七)使用缓冲区分析的方式解决单瓦片计算边缘值问题
Geotrellis系列文章链接地址http://www.cnblogs.com/shoufengwei/p/5619419.html 目录 前言 需求分析 实现方案 总结 一.前言 最 ...
- 【Java心得总结六】Java容器中——Collection
在[Java心得总结五]Java容器上——容器初探这篇博文中,我对Java容器类库从一个整体的偏向于宏观的角度初步认识了Java容器类库.而在这篇博文中,我想着重对容器类库中的Collection容器 ...
- 1Z0-053 争议题目解析419
1Z0-053 争议题目解析419 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 419.In Oracle 11g, by default which one of the fo ...