来自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. scrapy 修改URL爬取起始位置

    import scrapy from Autopjt.items import myItem from scrapy.http import Request class AutospdSpider(s ...

  2. 从Nest到Nesk -- 模块化Node框架的实践

    文: 达孚(沪江Web前端架构师) 本文原创,转至沪江技术 首先上一下项目地址(:>): Nest:https://github.com/nestjs/nest Nesk:https://git ...

  3. 从PRISM开始学WPF(五)MVVM(一)ViewModel?

    从PRISM开始学WPF(一)WPF? 从PRISM开始学WPF(二)Prism? 从PRISM开始学WPF(三)Prism-Region? 从PRISM开始学WPF(四)Prism-Module? ...

  4. caffe实现GAN

    我实现GAN网络结构比较复杂: 通过建立两个一模一样的网络,他们相对应的层共享权重,一个网络用来跟新D model另一个网络用来更新G model 更新G model的网络,D部分只进行梯度传递,不进 ...

  5. hexo博客图片问题

    hexo博客图片问题 第一步 首先确认_config.yml 中有 post_asset_folder:true. Hexo 提供了一种更方便管理 Asset 的设定:post_asset_folde ...

  6. wamp的mysql设置用户名和密码

    wamp下修改mysql root用户的登录密码 感谢作者:http://www.3lian.com/edu/2014/02-25/131010.html               1.安装好wam ...

  7. kubernetes入门(03)kubernetes的基本概念

    一.Pod 在Kubernetes集群中,Pod是创建.部署和调度的基本单位.一个Pod代表着集群中运行的一个进程,它内部封装了一个或多个应用的容器.在同一个Pod内部,多个容器共享存储.网络IP,以 ...

  8. Docker学习笔记 - Docker的仓库

  9. 新概念英语(1-3)Sorry, sir

    Does the man get his umbrella back? A:My coat and my umbrella please. B:Here is my ticket. A:Thank y ...

  10. python-3.x-基本数据类型

    当前学习版本为: python-3.6-4 代码: """整型 NUMBER""" a = 2 ** 5 b = a + 4 c = a / ...