「POJ1147」The Buses
解题思路
可以首先预处理一下可能成为一条线路的所有方案,然后把这些可能的方案按照长度降序排序,即按照路线上的时间节点从多到少排序。
因为这样我们就可以先确定更多的时刻的状态,减少搜索深度。
然后加一个最优化剪枝:
如果当前花费加上未来的最少的花费不会优,就直接return掉。
然后因为我们把所有的路线都按照路上节点数降序排序,所以我们只要碰到第一个不会更优的就直接return。
细节注意事项
- ans的初始化最好只要开到17,不然会跑得慢一些
- 然后在
vjudge
或POJ
上交的时候,要选的语言是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\)
「POJ1147」The Buses的更多相关文章
- 「译」JUnit 5 系列:条件测试
原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...
- 「译」JUnit 5 系列:扩展模型(Extension Model)
原文地址:http://blog.codefx.org/design/architecture/junit-5-extension-model/ 原文日期:11, Apr, 2016 译文首发:Lin ...
- JavaScript OOP 之「创建对象」
工厂模式 工厂模式是软件工程领域一种广为人知的设计模式,这种模式抽象了创建具体对象的过程.工厂模式虽然解决了创建多个相似对象的问题,但却没有解决对象识别的问题. function createPers ...
- 「C++」理解智能指针
维基百科上面对于「智能指针」是这样描述的: 智能指针(英语:Smart pointer)是一种抽象的数据类型.在程序设计中,它通常是经由类型模板(class template)来实做,借由模板(tem ...
- 「JavaScript」四种跨域方式详解
超详细并且带 Demo 的 JavaScript 跨域指南来了! 本文基于你了解 JavaScript 的同源策略,并且了解使用跨域跨域的理由. 1. JSONP 首先要介绍的跨域方法必然是 JSON ...
- 「2014-5-31」Z-Stack - Modification of Zigbee Device Object for better network access management
写一份赏心悦目的工程文档,是很困难的事情.若想写得完善,不仅得用对工具(use the right tools),注重文笔,还得投入大把时间,真心是一件难度颇高的事情.但,若是真写好了,也是善莫大焉: ...
- 「2014-3-18」multi-pattern string match using aho-corasick
我是擅(倾)长(向)把一篇文章写成杂文的.毕竟,写博客记录生活点滴,比不得发 paper,要求字斟句酌八股结构到位:风格偏杂文一点,也是没人拒稿的.这么说来,arxiv 就好比是 paper 世界的博 ...
- 「2014-3-17」C pointer again …
记录一个比较基础的东东-- C 语言的指针,一直让人又爱又恨,爱它的人觉得它既灵活又强大,恨它的人觉得它太过于灵活太过于强大以至于容易将人绕晕.最早接触 C 语言,还是在刚进入大学的时候,算起来有好些 ...
- 「2014-3-13」Javascript Engine, Java VM, Python interpreter, PyPy – a glance
提要: url anchor (ajax) => javascript engine (1~4 articles) => java VM vs. python interpreter =& ...
随机推荐
- leetcode网解题心得——61. 旋转链表
目录 leetcode网解题心得--61. 旋转链表 1.题目描述 2.算法分析: 3.用自然语言描述该算法 4.java语言实现 5.C语言实现 leetcode网解题心得--61. 旋转链表 1. ...
- ipfs camp course c demo exercise 1
目录 aim: my bugs 解决ipfs 的 cros 问题的方法 result final code for c1 aim: 首先咱们把 broswer 和 自己的api 连接起来(要显示出来自 ...
- 工业现场总线和工业以太网和工业IIOT
IIoT 称为工业物联网 ,包括机器的预测性维护和生产单元的自动化控制.以更高的速度获取和访问更大量的数据,打破数据孤岛,并将所有人员,数据和流程从工厂车间连接到执行办公室.企业领导者可以使用 IIo ...
- 使用YII缓存注意事项
在使用YII自身缓存时,在main.php文件配置中一定要配置keyPrefix,如下图: 'cache' => array( 'class' => 'CFileCache', 'keyP ...
- 计算机基础- 序列化(Serialization)和持久化(Persistence)的区别
参考 https://en.wikipedia.org/wiki/Serialization https://en.wikipedia.org/wiki/Persistence_(computer_s ...
- ADO.Net实体数据模型添加DB-First/Code First报错
Authentication method 'caching_sha2_password' not supported by any of the available plugins. 解决办法: 1 ...
- PAT T1005 Programming Pattern
建立后缀数组,遍历height数组找到连续大于len的最长子序列~ #include<bits/stdc++.h> using namespace std; ; char s[maxn]; ...
- 登陆页面Sql注入(绕过)
如图,看到这道题的时候发觉之前做过一个类似的手工注入: 不过这次手注会失败,后台过滤了sql语句里的一些东西,但我们并不知道过滤了什么 到这里我就基本上没辙了,不过查询了资料以后发现sqlmap可以对 ...
- [转]No configuration found for the specified action 原因及解决方案
转自 报错内容 警告: No configuration found for the specified action: 'login' in namespace: ''. Form action d ...
- IELTS Simon wr task p3