题意:有n个学习领域,每个领域有m个课程,学习第i个领域的第j个课程可以获得sij个技能点,在每个领域中选择一个课程,要求获得的n个技能点的最大值减最小值最小,输出符合要求的策略。

解法:尺取法。将课程的技能点按值进行排序,同时要记录每个值对应的领域,用尺取法选择第一段包含全部领域的区间,区间的边界即为最值,然后将左指针逐步右移,直到出现有的领域没有选课,继续右移右指针,直到结束。当右指针已移到最右的时候需要特判,只移动左指针。

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define LL long long
using namespace std;
int c[205];
struct node
{
int a, b, c;
bool operator < (const node &tmp) const
{
if(a == tmp.a)
return b < tmp.b;
return a < tmp.a;
}
bool operator == (const node &tmp) const
{
return a == tmp.a && b == tmp.b;
}
node(int a, int b, int c) : a(a), b(b), c(c){}
node() {}
};
vector <node> v;
int n;
bool judge(int cnt[])
{
for(int i = 0; i < n; i++)
if(!cnt[i])
return false;
return true;
}
int main()
{
while(~scanf("%d", &n))
{
v.clear();
for(int i = 0; i < n; i++)
{
scanf("%d", &c[i]);
}
if(n == 1 && c[0] == 1) //需要特判否则RE
{
int x;
scanf("%d", &x);
printf("0\n1\n");
continue;
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j < c[i]; j++)
{
int s;
scanf("%d", &s);
v.push_back(node(s, i, j + 1));
}
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
int len = v.size();
int flag = 0;
int now = -1;
int cnt[205] = {0};
int l = 0, r = 0;
cnt[v[0].b] = 1;
int ans = INT_MAX, pl, pr;
for(; l < len;)
{
if(!flag)
{
r++;
cnt[v[r].b]++;
if(judge(cnt))
{
flag = 1;
if(v[r].a - v[l].a < ans)
{
ans = v[r].a - v[l].a;
pl = l, pr = r;
}
continue;
}
}
else
{
if(~now && r < len - 1)
{
r++;
cnt[v[r].b]++;
if(cnt[now])
{
now = -1;
if(v[r].a - v[l].a < ans)
{
ans = v[r].a - v[l].a;
pl = l, pr = r;
}
}
}
else
{
if(r == len - 1)
{
cnt[v[l].b]--;
l++;
if(judge(cnt))
{
if(v[r].a - v[l].a < ans)
{
ans = v[r].a - v[l].a;
pl = l, pr = r;
}
}
continue;
}
cnt[v[l].b]--;
if(!cnt[v[l].b])
{
now = v[l].b;
l++;
}
else
{
l++;
if(v[r].a - v[l].a < ans)
{
ans = v[r].a - v[l].a;
pl = l, pr = r;
}
}
}
}
}
printf("%d\n", ans);
int prt[205];
for(int i = pl; i <= pr; i++)
{
prt[v[i].b] = v[i].c;
}
for(int i = 0; i < n; i++)
{
if(i)
printf(" ");
printf("%d", prt[i]);
}
puts("");
}
return 0;
}

  

CF GYM 100703I Endeavor for perfection的更多相关文章

  1. CF Gym 102028G Shortest Paths on Random Forests

    CF Gym 102028G Shortest Paths on Random Forests 抄题解×1 蒯板子真jir舒服. 构造生成函数,\(F(n)\)表示\(n\)个点的森林数量(本题都用E ...

  2. CF gym 101933 K King's Colors —— 二项式反演

    题目:http://codeforces.com/gym/101933/problem/K 其实每个点的颜色只要和父亲不一样即可: 所以至多 i 种颜色就是 \( i * (i-1)^{n-1} \) ...

  3. cf Gym 101086M ACPC Headquarters : AASTMT (Stairway to Heaven)

    题目: Description standard input/output As most of you know, the Arab Academy for Science and Technolo ...

  4. CF Gym 100685A Ariel

    传送门 A. Ariel time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  5. CF Gym 100685E Epic Fail of a Genie

    传送门 E. Epic Fail of a Genie time limit per test 0.5 seconds memory limit per test 64 megabytes input ...

  6. CF GYM 100703A Tea-drinking

    题意:龙要制作n个茶,每个茶的配方是一个字符串,两个字符串之间有一个差值,这个差值为两个字符串每个对应字母之间差的绝对值的最大值,求制作所有茶时获得的所有差值中的最大值. 解法:克鲁斯卡尔.将茶的配方 ...

  7. CF GYM 100703B Energy Saving

    题意:王子每月买m个灯泡给n个房间换灯泡,如果当前有的灯泡数够列表的第一个房间换的就全换,直到灯泡不够为止,给出q个查询,查询x月已经换好几个房子,手里还剩多少灯泡. 解法:水题……小模拟. 代码: ...

  8. CF GYM 100703F Game of words

    题意:两个人玩n个游戏,给出每人玩每个游戏的时间,两个人需要在n个游戏中挑m个轮流玩,求最短时间. 解法:dp.(这场dp真多啊……话说也可以用最小费用最大流做……然而并不会XD)dp[i][j][k ...

  9. CF GYM 100703G Game of numbers

    题意:给n个数,一开始基数为0,用这n个数依次对基数做加法或减法,使基数不超过k且不小于0,输出最远能运算到的数字个数,输出策略. 解法:dp.dp[i][j]表示做完第i个数字的运算后结果为j的可能 ...

随机推荐

  1. ios 基于CAEmitterLayer的雪花,烟花,火焰,爱心等效果demo(转)

    转载自:http://blog.csdn.net/mad2man/article/details/16898369 分类: cocoa SDK2013-11-23 11:52 388人阅读 评论(0) ...

  2. WebUploader API

    Uploader new Uploader( opts ) ⇒ Uploader 上传入口类. var uploader = WebUploader.Uploader({ swf: 'path_of_ ...

  3. Qt编译postgreSQL驱动

    安装postgreSQL,安装目录下的lib和bin添加到path 打开Qt安装目录,找到src\plugins\sqldrivers\psql编辑psql.pro,添加INCLUDEPATH += ...

  4. 解决Cisco VPN Client:Reason 442: Failed to Enable Virtual Adapter VPN连接问题

    大公司里肯定涉及不同地点的办公问题,这样VPN的使用就频繁了,今天遇到一个VPN连接问题,分享给大家,看一眼,以后不在这问题上耗费太多功夫. 在win7上连接vpn时抛出“failed to enab ...

  5. Flume的Avro Sink和Avro Source研究之一: Avro Source

    问题 : Avro Source提供了怎么样RPC服务,是怎么提供的? 问题 1.1 Flume Source是如何启动一个Netty Server来提供RPC服务. 由GitHub上avro-rpc ...

  6. jsp 获取表单值, 提交类型为multipart/form-data处理

    //tt.jsp<script type="text/javascript"> function doSubmit(){ alert("aaaaaa" ...

  7. 软考类----编码、ASII码等

    淘米2014实习生笔试,今年是淘米第一年招暑期实习生,笔试好大部分考的是软考的题目啊啊啊啊(劳资后悔当年没考软考刷加权),其他是浅而泛的风格,C++,SQL语句,数据结构(哈夫曼树,二叉查找树,栈后缀 ...

  8. highcharts 折线图

    <!doctype html> <html lang="en"> <head> <script type="text/javas ...

  9. ANDROID_MARS学习笔记_S01_008Linear_layout例子

    1.netstone_layout.xml <?xml version="1.0" encoding="utf-8"?> <LinearLay ...

  10. Windows10搭建PHP7开发环境

    原文:Windows10搭建PHP7开发环境 3年前写了一篇<Windows下搭建PHP开发环境>之后就再也没有碰过PHP了,最近新发布了PHP7然后回去看了一下之前写的文章,发现很多配置 ...