来自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. Flask 学习 十五 性能

    记录影响性能的数据库查询 app/main/views.py from flask_sqlalchemy import get_debug_queries @main.after_app_reques ...

  2. 如何书写高效的css样式

    如何书写高效的css样式? 有以下四个关键要素: 1.高效的css 2.可维护的css 3.组件化的css 4.hack-free  css 书写高效的css: 1.使用外联样式替代行间样式或内嵌样式 ...

  3. 第二篇:利用shell脚本执行webservice请求——基于soap

    1. 项目背景 以往我们在开发基于webservice的项目中,我们总习惯于直接使用webservice的一些框架,如Axis,axis2和Xfire等.框架的好处是将webservice所涉及到的s ...

  4. 15-TypeScript策略模式

    在前面的简单工厂模式中,通常将每个类.接口定义到不同的文件中.在面向对象开发思想中有一个重要的原则就是封装变化点,在实际操作过程中, 通常被调用方的代码不要去更改,而是增加,这是面向对象的开闭原则.在 ...

  5. 《深入实践Spring Boot》阅读笔记之三:核心技术源代码分析

    刚关注的朋友,可以回顾前两篇文章: 基础应用开发 分布式应用开发 上篇文章总结了<深入实践Spring Boot>的第二部分,本篇文章总结第三部分,也是最后一部分.这部分主要讲解核心技术的 ...

  6. kubernetes入门(01)kubernetes是什么?

    一.kubernetes是什么? Kubernetes是Google开源的一个容器编排引擎,它支持自动化部署.大规模可伸缩.应用容器化管理.在生产环境中部署一个应用程序时,通常要部署该应用的多个实例以 ...

  7. Mac里安装Jmeter

    前提是需要安装jdk,参见http://www.cnblogs.com/fun0623/p/4703456.html 1.解压包 (双击apache-jmeter-2.13) 2.进去到解压后的bin ...

  8. 日推20单词 Day03

    1.occur v. 发生,发现 2.harvest n.收获,丰收 vt.收割,得到 3.crop n.庄稼,收成 4.yield n.产量 v.产出,屈服 5.field n.田野 6.featu ...

  9. 2018 php的flush和ob_flush不起作用 整理解决

    2018 php的flush和ob_flush不起作用 整理解决成功解决. 要点 : 使用函数 str_repeat2处配置:检查php.ini.Nginx 中有下面两个设置使用方式:echo str ...

  10. hive:导出数据记录中null被替换为\n的解决方案

    在hive中,一般情况下通过 use my_hive_db; set hive.merge.mapfiles=true; set hive.merge.mapredfiles=true; ; ; in ...