「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 =& ...
随机推荐
- ElementUI el-table 表格 行选择框改为单选
实现方法 首先,表格加一列 <el-table-column type="selection" width="55"></el-table-c ...
- Bugku-CTF加密篇之easy_crypto(0010 0100 01 110 1111011 11 11111 010 000 0 001101 1010 111 100 0 001101 01111 000 001101 00 10 1 0 010 0 000 1 01111 10 11110 101011 1111101)
easy_crypto 0010 0100 01 110 1111011 11 11111 010 000 0 001101 1010 111 100 0 001101 01111 000 00110 ...
- 用for循环写这段代码
之前用while循环写了一段代码,现在改为用for循环来写,代码如下: hongtao_age = 38 for i in range(5): guess_age = int(input(" ...
- GO TIME
#go语言的time包 ##组成 time.Duration(时长,耗时) time.Time(时间点) time.C(放时间点的管道)[ Time.C:=make(chan time.Time) ] ...
- 进程作业管理2-kill,前后台作业,并行执行
kill命令:向进程发送控制信号,以实现对进程管理,每个信号对应一个数字,信号名称以SIG开 头(可省略),不区分大小写 显示当前系统可用信号: kill –l 或者 trap -l 常用信号: ...
- 常用SQL语句大全
一些常用SQL语句大全 一.基础1.说明:创建数据库CREATE DATABASE database-name2.说明:删除数据库drop database dbname3.说明:备份sql se ...
- Educational Codeforces Round 82 C. Perfect Keyboard
Polycarp wants to assemble his own keyboard. Layouts with multiple rows are too complicated for him ...
- Update(Stage4):sparksql:第5节 SparkSQL_出租车利用率分析案例
目录: 1. 业务2. 流程分析3. 数据读取5. 数据清洗6. 行政区信息 6.1. 需求介绍 6.2. 工具介绍 6.3. 具体实现7. 会话统计 导读 本项目是 SparkSQL 阶段的练习项目 ...
- Educational Codeforces Round 68 (Rated for Div. 2)D(SG函数打表,找规律)
#include<bits/stdc++.h>using namespace std;int sg[1007];int main(){ int t; cin>>t; while ...
- 导入jeesite 项目
1:从开源中国用git方式下载jeesite源码 链接https://gitee.com/thinkgem/jeesite gti 地址:https://gitee.com/thinkgem/j ...