CF GYM 100703I Endeavor for perfection
题意:有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的更多相关文章
- CF Gym 102028G Shortest Paths on Random Forests
CF Gym 102028G Shortest Paths on Random Forests 抄题解×1 蒯板子真jir舒服. 构造生成函数,\(F(n)\)表示\(n\)个点的森林数量(本题都用E ...
- CF gym 101933 K King's Colors —— 二项式反演
题目:http://codeforces.com/gym/101933/problem/K 其实每个点的颜色只要和父亲不一样即可: 所以至多 i 种颜色就是 \( i * (i-1)^{n-1} \) ...
- 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 ...
- CF Gym 100685A Ariel
传送门 A. Ariel time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- 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 ...
- CF GYM 100703A Tea-drinking
题意:龙要制作n个茶,每个茶的配方是一个字符串,两个字符串之间有一个差值,这个差值为两个字符串每个对应字母之间差的绝对值的最大值,求制作所有茶时获得的所有差值中的最大值. 解法:克鲁斯卡尔.将茶的配方 ...
- CF GYM 100703B Energy Saving
题意:王子每月买m个灯泡给n个房间换灯泡,如果当前有的灯泡数够列表的第一个房间换的就全换,直到灯泡不够为止,给出q个查询,查询x月已经换好几个房子,手里还剩多少灯泡. 解法:水题……小模拟. 代码: ...
- CF GYM 100703F Game of words
题意:两个人玩n个游戏,给出每人玩每个游戏的时间,两个人需要在n个游戏中挑m个轮流玩,求最短时间. 解法:dp.(这场dp真多啊……话说也可以用最小费用最大流做……然而并不会XD)dp[i][j][k ...
- CF GYM 100703G Game of numbers
题意:给n个数,一开始基数为0,用这n个数依次对基数做加法或减法,使基数不超过k且不小于0,输出最远能运算到的数字个数,输出策略. 解法:dp.dp[i][j]表示做完第i个数字的运算后结果为j的可能 ...
随机推荐
- PDF ITextSharp
示例源码 //Document:(文档)生成pdf必备的一个对象,生成一个Document示例 Document document = new Document(PageSize.A4, 30, 30 ...
- MyEcpilise引入Maven项目目录不正常,无JRE,无Maven Dependencies
右键项目--> Maven4MyEclipse --> Update Project Configuration
- Sql查询除ID以外相同的数据
id NAME AGE1 n1 12 n1 13 n2 24 n2 25 n22 ...
- [转载]LINQ 中的 select
下面通过一些例子来说明怎样使用select,参考自:LINQ Samples 1. 可以对查询出来的结果做一些转换,下面的例子在数组中查找以"B"开头的名字,然后全部转成小写输出 ...
- 酷摄影:关于梦 - Miki takahashi
这组摄影来自于日本东京摄影师 Miki takahashi 是一组双重曝光摄影,分开看也许很平常,但是结合在一起却非常有韵味. [gallery]
- Nagios : Verifying Your Configuration
Every time you modify your configuration files, you should run a sanity check on them. It is importa ...
- Tomcat多次部署
http://blog.csdn.net/knityster/article/details/6300804
- Kerbose
http://blog.csdn.net/wulantian/article/details/42418231
- *****正则表达式匹配URL
最近将匹配URL的正则替换了下 之前的是: ((http|ftp|https)://)(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3 ...
- 扩展DJANGO的LISTVIEW
不用MODEL,不用QUERYSET,而用get_queryset方法来扩展LISTVIEW, 从而实现特定过滤或搜索功能. class DVListView(ListView): template_ ...