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 ...
随机推荐
- cache4j轻量级java内存缓存框架,实现FIFO、LRU、TwoQueues缓存模型
简介 cache4j是一款轻量级java内存缓存框架,实现FIFO.LRU.TwoQueues缓存模型,使用非常方便. cache4j为java开发者提供一种更加轻便的内存缓存方案,杀鸡焉用EhCac ...
- 内存提取SWF,破解doswf方法概述
参考文献: http://blog.ceflash.com/%E5%86%99%E7%82%B9swf%E4%BA%8C%E8%BF%9B%E5%88%B6%E7%9A%84%E4%B8%9C%E8% ...
- iOS-几大框架的介绍
1.Objective-C之Foundation框架 概述 我们前面的章节中就一直新建Cocoa Class,那么Cocoa到底是什么,它和我们前面以及后面要讲的内容到底有什么关系呢?Objectiv ...
- 在微软伪静态处理机制下action导致伪静态的地址重现的问题
伪静态前的地址:/sc/ProductList.aspx?pClass=0&descType=2&minPrice=1&maxPrice=11 伪静态后的地址:/product ...
- 手把手教你用nodejs+SQL Server2012做增删改查
1.开发工具WebStorm 10.0.4 2.打开WebStorm 10.0.4新建项目:
- 【原创】开源.NET排列组合组件KwCombinatorics使用(三)——笛卡尔积组合
本博客所有文章分类的总目录:本博客博文总目录-实时更新 本博客其他.NET开源项目文章目录:[目录]本博客其他.NET开源项目文章目录 KwCombinatorics组件文章目录: 1. ...
- MongoDB学习系列(1)--入门介绍
MongoDB是一款为Web应用程序设计的面向文档结构的数据库系统. MongoDB贡献者是10gen公司.地址:http://www.10gen.com 1.MongoDB主要特性: 1.1文档数据 ...
- 关于SubSonic3.0插件更新字符串过长引发的System.Data.SqlClient.SqlException的异常修复
最近公司客服提交了个BUG,说是更新产品详细信息时,有的可以有的更新不了,前段时间一直没空所以暂时放下,刚才又出现这个问题,所以马上处理了一下. 打开项目解决方案,进入DEBUG模式,拿到操作的数据提 ...
- 通过setTimeout来取消因大量计算造成的网页卡顿
js是单线程的,所以有些大量计算的操作会占用线程资源,导致页面卡住. 今天遇到这样一个场景,选择一个下拉框之后,对数据进行筛选,这个过程中有大量计算,点了selecte的option之后,option ...
- linux自定义系统调用
1 Linux3.10.21内核系统调用设置 以前看的内核版本时2.6.11的,里面的系统调用设置一目了然啊!在文件entry.S中直接定义了sys_call_table表,并在这个文件中用各个系统调 ...