「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 =& ...
随机推荐
- mock数据时,http://localhost:8080/#/api/goods 无法访问到数据
最近学习一个vue-cli的项目,需要与后台进行数据交互,这里使用本地json数据来模仿后台数据交互流程.然而发现build文件夹下没有dev-server.js文件了,因为新版本的vue-webpa ...
- Educational Codeforces Round 81 (Rated for Div. 2)
A 0~9需要多少笔画,自取7和1,判奇偶 #include<bits/stdc++.h> using namespace std; #define ll long long #defin ...
- shell脚本部署apache并能通过浏览器访问!
第一步:导入httpd-2.2.17.tar包 第二步:创建一个test.sh文件(可在/root下) 第三步编写shell脚本 > 会重写文件,如果文件里面有内容会覆盖 >>这个是 ...
- selected中第一项 请选择,隐藏
如何做到selected 类似input的提示语 placeholder效果. <select class="wyj_dbfs"> <option style= ...
- redhat7.6 httpd 匿名目录 目录加密 域名跳转
配置文件/etc/httpd/conf/httpd.conf 监听80端口和8080端口 1.80端口 2.域名 3.index.html目录 4.网站目录 options Indexes //代 ...
- leetCode练题——21. Merge Two Sorted Lists(照搬大神做法)
1.题目 21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new l ...
- java8新特性1:lambda表达式和函数式接口
1.lambda的介绍: 1.1.为什么java语言需要引入lambda表达式? java语言诞生于1995年,历史时间已经相对较长了.在其后的各种新型编程语言中,都有着lambda表达式的内容,并且 ...
- Faster-RCNN Pytorch实现的minibatch包装
实际上faster-rcnn对于输入的图片是有resize操作的,在resize的图片基础上提取feature map,而后generate一定数量的RoI. 我想首先去掉这个resize的操作,对每 ...
- C++代码书写规范——给新手程序员的一些建议
代码就是程序员的面子,无论是在工作中在电脑上写程序代码还是在面试时在纸上写演示代码我们都希望写出整洁,优雅的代码.特别在工作中当我们碰到需要维护别人的代码,或者是多人参与一个项目大家一起写代码的时候, ...
- MessageBox函数
<Windows程序设计>(第五版)(美Charles Petzold著) https://docs.microsoft.com/zh-cn/windows/desktop/apiinde ...