传送门

POJ

Vjudge

解题思路

可以首先预处理一下可能成为一条线路的所有方案,然后把这些可能的方案按照长度降序排序,即按照路线上的时间节点从多到少排序。

因为这样我们就可以先确定更多的时刻的状态,减少搜索深度。

然后加一个最优化剪枝:

如果当前花费加上未来的最少的花费不会优,就直接return掉。

然后因为我们把所有的路线都按照路上节点数降序排序,所以我们只要碰到第一个不会更优的就直接return。

细节注意事项

  • ans的初始化最好只要开到17,不然会跑得慢一些
  • 然后在 vjudgePOJ 上交的时候,要选的语言是 G++ 而不是 C++ 我居然CE了一次
Main.cpp
F:\temp\21004851.198535\Main.cpp(51) : error C2059: syntax error : '{'
F:\temp\21004851.198535\Main.cpp(51) : error C2143: syntax error : missing ';' before '{'
F:\temp\21004851.198535\Main.cpp(51) : error C2065: 'j' : undeclared identifier
F:\temp\21004851.198535\Main.cpp(51) : error C2065: 'j' : undeclared identifier
F:\temp\21004851.198535\Main.cpp(51) : error C2143: syntax error : missing ';' before '}'
  • 题面确实有点点的难捋清楚啊。。。

参考代码

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#include <queue>
#define rg register
using namespace std;
template < typename T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while (!isdigit(c)) f |= c == '-', c = getchar();
while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
s = f ? -s : s;
} const int _ = 1926; int n, cnt[70], ans = 17, X;
struct node { int a, b, c; } t[_];
inline bool cmp(const node& x, const node& y) { return x.c > y.c; } inline bool check(int a, int b) {
for (rg int i = a; i < 60; i += b)
if (!cnt[i]) return 0;
return 1;
} inline void dfs(int i, int num) {
if (n == 0) { ans = min(ans, num); return ; }
for (rg int j = i; j <= X; ++j) {
if (num + n / t[j].c >= ans) return ;
if (check(t[j].a, t[j].b)) {
for (rg int k = t[j].a; k < 60; k += t[j].b) --n, --cnt[k];
dfs(j, num + 1);
for (rg int k = t[j].a; k < 60; k += t[j].b) ++n, ++cnt[k];
}
}
} int main() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
read(n);
for (rg int x, i = 1; i <= n; ++i) read(x), ++cnt[x];
for (rg int i = 0; i < 30; ++i) {
if (!cnt[i]) continue;
for (rg int j = i + 1; i + j < 60; ++j)
if (check(i, j)) t[++X] = (node) { i, j, (59 - i) / j + 1 };
}
sort(t + 1, t + X + 1, cmp);
dfs(1, 0);
printf("%d\n", ans);
return 0;
}

完结撒花 \(qwq\)

最新文章

  1. 现在创业做App,先做 Android 还是 iOS?
  2. Jquery表单序列化和AJAX全局事件
  3. Python中super函数的用法
  4. Python: 解决pip安装源被墙的问题
  5. Java线程池的原理及几类线程池的介绍
  6. 大嫂的HTML
  7. 被 Windows 10 SDK 坑了
  8. Google Code Pretiffy 代码 着色 高亮 开源 javascript(JS)库
  9. selenium python (六)定位一组对象
  10. [Lua]入门教程
  11. opencv china 网站,好1376472449
  12. Java读取一个文件并打印到控制台上
  13. designated initializer和secondary initializer是什么?
  14. codeforces 375D . Tree and Queries 启发式合并 || dfs序+莫队
  15. Java中Volatile的作用
  16. #if defined和#if !defined(c语言的宏定义)
  17. 201521123024 《Java程序设计》第11周学习总结
  18. 如何在Intellij IDEA中拉svn分支?
  19. JS实现回到Top(顶部)--JavaScript
  20. Linux时间子系统之(三):用户空间接口函数

热门文章

  1. Springboot学习:日志
  2. 【知识学习】PHP实现批量替换字典后缀
  3. win10使用L2TP连接失败,报远程服务器未响应错误解决办法,亲测可用!
  4. JS vue 组件创建过程
  5. CSS创意与视觉表现
  6. [03] Recursive Function递归应用
  7. P4710 平抛运动
  8. PIP安装模块下载慢或者无法下载
  9. 解决:使用 swiper 自动轮播图片,当拖动过 swiper 内的内容时,导致不继续自动轮播
  10. 【摘录自MDN】对事件冒泡和捕捉的解释