VK-Cup2017 Wild Card Round 2
来自FallDream的博客,未经允许,请勿转载,谢谢。
Cf的Vkcup外卡赛2 上次round2和ditoly翻车了 所以只好来打打了 规则是给一道比较特殊的题目,你要找出较优的解
University Schedule/大学课程
有n个学生,m个教授,一周有6天,每天有7节课
告诉你每个学生这一周要和每个教授上多少节课 但是只有a个教室 也就是同一时间最多只能有a节课
定义学生和教授的疲劳度 假设一个学生/教授在一天上的第一节课是第x节,最后一节是第y节 那么它的疲劳度是(y-x+3)^2 如果没上课就没有疲劳度
你要合理的安排上课方案 使得教师和学生的疲劳度之和最小 n,m,a<=60 时间限制10s 有100个pretest
一开始写了一个贪心 让学生的课程平均分配 教授不管他 分数有点低...
很显然这样并不是特别优秀 而且浪费了非常多的时间 所以考虑换个做法
时间比较长 所以直接上了模拟退火 贪心之后 随机交换两个课程 另外贪心不一定优 所以加入一些空的课程一起交换
卡卡时 效果还可以 最后排到了第8位 实际上还有很多优化的空间 比如记下一天的最早最晚课程 答案 减少计算量等等
前几名写的算法不是很懂 (这种比赛的代码都奇奇怪怪 ) 还能找到几个退火的 但是感觉我和ditoly的代码最好看233
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<ctime>
#define Rep(a,b,c) for(int a=b;a<=c;++a)
using namespace std;
inline int read()
{
int x = ; char ch = getchar();
while(ch< '' || ch > '') ch = getchar();
while(ch >= '' && ch <= ''){x = x * + ch - '';ch = getchar();}
return x;
} int n , m , a , Plan[][] , Class[][] , cnt= , Xs_ans[][] , Js_ans[][] , NewXs[][] , NewJs[][];
int ans , Xs[][][] , Js[][][] , Best , Best_Xs[][][];
struct Lesson{int xs,js,day,num;}L[];
inline int Sqr(int x){return x * x;} inline int Calc(int*Schedule)
{
int Early = , Last = ;
Rep(k , , )
if(Schedule[k])
(!Early) ? (Early = k) : , Last = k;
return Early ? Sqr(Last - Early + ) : ;
} void CalcAns()
{
ans = ;
Rep(i , , n)
Rep(j , , )
ans += (Xs_ans[i][j] = Calc( Xs[i][j] ));
Rep(i , , m)
Rep(j , , )
ans += (Js_ans[i][j] = Calc( Js[i][j] ));
} void Print()
{
printf("%d\n" , Best);
Rep(i , , n)
{
puts("");
Rep(j , , )
{
Rep(k , , )
printf("%d " , Best_Xs[i][k][j]);
puts("");
}
}
} void Copy()
{
Best = ans;
memcpy(Best_Xs , Xs , sizeof(Xs));
} inline int Ran()
{
return ((rand()<<)|rand()) % cnt + ;
} inline double Random()
{
return rand() / (double) RAND_MAX;
} bool check(int x , int y)
{
if( L[x].xs == L[y].xs)
{
if(L[x].js == L[y].js) return false;
if(Js[ L[x].js ][ L[y].day][ L[y].num ] || Js[ L[y].js ][ L[x].day][ L[x].num ]) return false;
return true;
}
if( L[x].js == L[y].js)
{
if(Xs[ L[x].xs ][ L[y].day ][ L[y].num ] || Xs[ L[y].xs ][ L[x].day ][ L[x].num ]) return false;
return true;
}
if(Xs[ L[x].xs ][ L[y].day ][ L[y].num ] || Js[ L[x].js ][ L[y].day][ L[y].num ]) return false;
if(Xs[ L[y].xs ][ L[x].day ][ L[x].num ] || Js[ L[y].js ][ L[x].day][ L[x].num ]) return false;
return true;
} int main()
{
srand( 413U );
n = read(); m = read(); a = read();
Rep(i , , n)
Rep(j , , m)
Plan[i][j] = read();
Rep(i , , n)
{
int j = , k = ;
for( ; ; )
{
for( ; !Plan[i][j] && j <= m ; ++j);
if( j > m ) break;
Rep(l , , )
if(Class[k][l] < a && !Xs[i][k][l] && !Js[j][k][l])
{
++Class[k][l];
Xs[i][k][l] = j;
Js[j][k][l] = i;
--Plan[i][j];
L[ ++cnt ] = (Lesson) {i , j , k , l};
break;
}
(++k == ) ? ( k = ) : ;
}
}
CalcAns();
Copy(); Rep(i , , )
Rep(j , , )
Rep(k , Class[i][j]+ , a)
L[++cnt] = (Lesson) { , , i , j}; double Temp = , delta = 0.99; time_t Beg = clock();
for( ; ; )
{
if(clock() - Beg > ) break;
Rep(it , , )
{
int x = Ran() , y = Ran();
if(check(x , y))
{
int Newans = ans;
Newans -= Xs_ans[ L[x].xs ][ L[x].day ];
Newans -= Xs_ans[ L[y].xs ][ L[x].day ];
Newans -= Js_ans[ L[x].js ][ L[x].day ];
Newans -= Js_ans[ L[y].js ][ L[x].day ];
if( L[x].day != L[y].day )
Newans -= Xs_ans[ L[x].xs ][ L[y].day ],
Newans -= Xs_ans[ L[y].xs ][ L[y].day ],
Newans -= Js_ans[ L[x].js ][ L[y].day ],
Newans -= Js_ans[ L[y].js ][ L[y].day ]; Xs[ L[x].xs ][ L[x].day ][ L[x].num ] = ;
Xs[ L[y].xs ][ L[y].day ][ L[y].num ] = ;
Js[ L[x].js ][ L[x].day ][ L[x].num ] = ;
Js[ L[y].js ][ L[y].day ][ L[y].num ] = ; Xs[ L[x].xs ][ L[y].day ][ L[y].num ] = L[x].js;
Xs[ L[y].xs ][ L[x].day ][ L[x].num ] = L[y].js;
Js[ L[x].js ][ L[y].day ][ L[y].num ] = L[x].xs;
Js[ L[y].js ][ L[x].day ][ L[x].num ] = L[y].xs; Newans += (NewXs[ L[x].xs ][ L[x].day ] = Calc(Xs[ L[x].xs ][ L[x].day ]));
Newans += (NewXs[ L[y].xs ][ L[x].day ] = Calc(Xs[ L[y].xs ][ L[x].day ]));
Newans += (NewJs[ L[x].js ][ L[x].day ] = Calc(Js[ L[x].js ][ L[x].day ]));
Newans += (NewJs[ L[y].js ][ L[x].day ] = Calc(Js[ L[y].js ][ L[x].day ]));
if( L[x].day != L[y].day )
Newans += (NewXs[ L[x].xs ][ L[y].day ] = Calc(Xs[ L[x].xs ][ L[y].day ])),
Newans += (NewXs[ L[y].xs ][ L[y].day ] = Calc(Xs[ L[y].xs ][ L[y].day ])),
Newans += (NewJs[ L[x].js ][ L[y].day ] = Calc(Js[ L[x].js ][ L[y].day ])),
Newans += (NewJs[ L[y].js ][ L[y].day ] = Calc(Js[ L[y].js ][ L[y].day ])); if(Newans < ans || Random() < exp((ans-Newans)/Temp))
{
if(Newans < ans) ans = Newans , Copy();
ans = Newans;
swap(L[x].day , L[y].day);
swap(L[x].num , L[y].num);
Xs_ans[ L[x].xs ][ L[x].day ] = NewXs[ L[x].xs ][ L[x].day ];
Xs_ans[ L[y].xs ][ L[x].day ] = NewXs[ L[y].xs ][ L[x].day ];
Js_ans[ L[x].js ][ L[x].day ] = NewJs[ L[x].js ][ L[x].day ];
Js_ans[ L[y].js ][ L[x].day ] = NewJs[ L[y].js ][ L[x].day ];
if( L[x].day != L[y].day )
Xs_ans[ L[x].xs ][ L[y].day ] = NewXs[ L[x].xs ][ L[y].day ],
Xs_ans[ L[y].xs ][ L[y].day ] = NewXs[ L[y].xs ][ L[y].day ],
Js_ans[ L[x].js ][ L[y].day ] = NewJs[ L[x].js ][ L[y].day ],
Js_ans[ L[y].js ][ L[y].day ] = NewJs[ L[y].js ][ L[y].day ];
}
else
{
Xs[ L[x].xs ][ L[y].day ][ L[y].num ] = ;
Xs[ L[y].xs ][ L[x].day ][ L[x].num ] = ;
Js[ L[x].js ][ L[y].day ][ L[y].num ] = ;
Js[ L[y].js ][ L[x].day ][ L[x].num ] = ; Xs[ L[x].xs ][ L[x].day ][ L[x].num ] = L[x].js;
Xs[ L[y].xs ][ L[y].day ][ L[y].num ] = L[y].js;
Js[ L[x].js ][ L[x].day ][ L[x].num ] = L[x].xs;
Js[ L[y].js ][ L[y].day ][ L[y].num ] = L[y].xs; }
}
}
Temp *= delta;
}
Print();
return ;
}
VK-Cup2017 Wild Card Round 2的更多相关文章
- Clash Credenz 2014 Wild Card Round题解
A题 简单模拟. /************************************************************************* > File Name: ...
- DP VK Cup 2012 Qualification Round D. Palindrome pairs
题目地址:http://blog.csdn.net/shiyuankongbu/article/details/10004443 /* 题意:在i前面找回文子串,在i后面找回文子串相互配对,问有几对 ...
- VK CUP2017 ROUND 1
来自FallDream的博客.未经允许,请勿转载,谢谢. ---------------------------------------------------- 和ditoly组队打vkcup,原来 ...
- Codeforces Round 623(Div. 2,based on VK Cup 2019-2020 - Elimination Round,Engine)D. Recommendations
VK news recommendation system daily selects interesting publications of one of n disjoint categories ...
- VK Cup 2012 Qualification Round 1 C. Cd and pwd commands 模拟
C. Cd and pwd commands Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...
- VK Cup 2012 Qualification Round 2 C. String Manipulation 1.0 字符串模拟
C. String Manipulation 1.0 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 codeforces.com/problemset/pr ...
- VK Cup 2012 Qualification Round 1---C. Cd and pwd commands
Cd and pwd commands time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- codeforce vk cup2017
D. k-Interesting Pairs Of Integers time limit per test 2 seconds memory limit per test 256 megabytes ...
- VK Cup 2016 - Qualification Round 2 B. Making Genome in Berland
今天在codeforces上面做到一道题:http://codeforces.com/contest/638/problem/B 题目大意是:给定n个字符串,找到最短的字符串S使得n个字符串都是这个字 ...
随机推荐
- Ubuntu下tomcat或eclipse启动提示没有java环境问题
tomcat和eclipse默认使用了openjdk,通过压缩包安装的jdk无法被识别,通过修改tomcat/bin下的catalina.sh添加jdk和jre路径即可 sudo gedit cata ...
- Scrum 冲刺 第一日
Scrum 冲刺 第一日 站立式会议 燃尽图 Alpha 阶段认领任务 明日任务安排 项目预期任务量 成员贡献值计算规则 今日贡献量 参考资料 站立式会议 返回目录 燃尽图 返回目录 Alpha 阶段 ...
- bzoj千题计划288:bzoj1876: [SDOI2009]SuperGCD
http://www.lydsy.com/JudgeOnline/problem.php?id=1876 高精压位GCD 对于 GCD(a, b) a>b 若 a 为奇数,b 为偶数,GCD ...
- git cherry-pick 整理
git cherry-pick可以选择某一个分支中的一个或几个commit(s)来进行操作.例如,假设我们有个稳定版本的分支,叫v2.0,另外还有个开发版本的分支v3.0,我们不能直接把两个分支合并, ...
- 02-移动端开发教程-CSS3新特性(中)
1. 新的背景 背景在CSS3中也得到很大程度的增强,比如背景图片尺寸.背景裁切区域.背景定位参照点.多重背景等. 1.1 background-size设置背景图片的尺寸 cover会自动调整缩放比 ...
- 解决IE8下opacity属性失效问题
由于opacity属性存在兼容性问题,所以在IE8下,用opacity来设置元素的透明度,会失效,从而导致页面的样式问题. 在IE8及其更早的浏览器下,我们可以使用filter属性,来代替opacit ...
- Python内置函数(16)——ord
英文文档: ord(c) Given a string representing one Unicode character, return an integer representing the U ...
- Python内置函数(4)——min
英文文档: min(iterable, *[, key, default]) min(arg1, arg2, *args[, key]) Return the smallest item in an ...
- http缓存浅谈
我们在访问百度首页的时候,会发现不管怎么刷新页面,静态资源基本都是返回 200(from cache): 随便点开一个静态资源是酱的: 哎哟有Response报头数据呢,看来服务器也正常返回了etag ...
- 离线Chrome插件安装文件(crx)的安装方法
离线Chrome插件安装文件(crx)的安装方法 一.正常安装方法 1.开发谷歌浏览器,设置->扩展程序 在打开的谷歌浏览器的扩展管理器中用户可以看到一些已经安装程序的Chrome插件,或者一个 ...