来自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的更多相关文章

  1. Clash Credenz 2014 Wild Card Round题解

    A题 简单模拟. /************************************************************************* > File Name: ...

  2. DP VK Cup 2012 Qualification Round D. Palindrome pairs

    题目地址:http://blog.csdn.net/shiyuankongbu/article/details/10004443 /* 题意:在i前面找回文子串,在i后面找回文子串相互配对,问有几对 ...

  3. VK CUP2017 ROUND 1

    来自FallDream的博客.未经允许,请勿转载,谢谢. ---------------------------------------------------- 和ditoly组队打vkcup,原来 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. codeforce vk cup2017

    D. k-Interesting Pairs Of Integers time limit per test 2 seconds memory limit per test 256 megabytes ...

  9. VK Cup 2016 - Qualification Round 2 B. Making Genome in Berland

    今天在codeforces上面做到一道题:http://codeforces.com/contest/638/problem/B 题目大意是:给定n个字符串,找到最短的字符串S使得n个字符串都是这个字 ...

随机推荐

  1. js定时刷新页面.

    //页面定时刷新.2017.09.27 $(document).ready(function () { self.setInterval(function () { var d = new Date( ...

  2. PHP冒泡排序、选择排序、插入排序

    $arr = [1, 8, 7, 5, 4, 2, 11, 9, 20]; 冒泡排序: for ($i = 0; $i < count($arr); $i ++) { for ($j = 0; ...

  3. JS 上传图片时实现预览

    网页中一张图片可以这样显示: <img src="http://www.letuknowit.com/images/wg.png"/>也可以这样显示:<img s ...

  4. BlueMix - IBM的Paas云计算平台

    Bluemix,2015年年中,IBM推出了名为Bluemix的云计算平台.这一"平台即服务"的PaaS云将帮助开发者更快的进行应用开发和部署.   Bluemix正是IBM回应这 ...

  5. 如何修改chrome记住密码后自动填充表单的黄色背景 ?

    input:-webkit-autofill, textarea:-webkit-autofill, select:-webkit-autofill { background-color: rgb(2 ...

  6. linux下安装 配置 redis数据库

    通过终端命令安装(推荐): 1 确保更新源服务器能正常使用 如果没有更换更新源服务器,那么可能一直都下不了软件.欢迎参考我之前的博文来更换成国内的镜像服务器http://www.cnblogs.com ...

  7. HashMap就是这么简单【源码剖析】

    前言 声明,本文用得是jdk1.8 前面已经讲了Collection的总览和剖析List集合以及散列表.Map集合.红黑树的基础了: Collection总览 List集合就这么简单[源码剖析] Ma ...

  8. Java8新特性第3章(Stream API)

    Stream作为Java8的新特性之一,他与Java IO包中的InputStream和OutputStream完全不是一个概念.Java8中的Stream是对集合功能的一种增强,主要用于对集合对象进 ...

  9. Android 6.0 以后webview不加载图片的问题

    /** * Webview在安卓5.0之前默认允许其加载混合网络协议内容 * 在安卓5.0之后,默认不允许加载http与https混合内容,需要设置webview允许其加载混合网络协议内容 */if ...

  10. www的构建技术

    www的构建技术分别是: html超文本标记语言,页面的文本显示 http超文本传输协议,信息传输转移的约定 url统一资源定位符,客户端浏览超文本的地址集合