[POI 2018] Plan Metra
[题目链接]
https://www.lydsy.com/JudgeOnline/problem.php?id=5100
[算法]
首先分两类考虑 :
1. 1 -> N的路径不经过其它节点 , 我们只需判断(d1i - d2i)的绝对值是否全部相等
2. 1 -> N的路径经过了其它节点 , 那么显然 , 1 -> N这条链的长度为min{ d1i + d2i } , 所有d1i + d2i等于链长的节点都在链上 , 将其余节点的d1i和d2i作差 , 即可O(1)判断出这个节点是挂在链上的哪个节点的
时间复杂度 : O(N)
[代码]
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int N = 1e6 + ;
const int inf = 2e9;
const int V = 1e7 + ; struct edge {
int to , w , nxt;
} e[N << ]; int n , m , tot;
int head[N] , d1[N] , d2[N] , mp[V]; template <typename T> inline void chkmax(T &x , T y) { x = max(x , y); }
template <typename T> inline void chkmin(T &x , T y) { x = min(x , y); }
template <typename T> inline void read(T &x) {
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
inline void addedge(int u , int v , int w)
{
++tot;
e[tot] = (edge){v , w , head[u]};
head[u] = tot;
}
inline void add(int x , int y , int w) {
addedge(x , y , w);
addedge(y , x , w);
}
inline void dfs(int u , int par) {
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to , w = e[i].w;
if (v != par) {
printf("%d %d %d\n" , u , v , w);
dfs(v , u);
}
}
}
inline bool check()
{
int val = abs(d1[] - d2[]);
if (val == ) return false;
for (int i = ; i < n; ++i)
if (abs(d1[i] - d2[i]) != val) return false;
printf("TAK\n");
printf("%d %d %d\n" , , n , val);
for (int i = ; i < n; ++i)
if (d1[i] >= d2[i]) printf("%d %d %d\n" , n , i , d2[i]);
else printf("%d %d %d\n" , , i , d1[i]);
return true;
}
int main() { read(n);
if (n == )
{
printf("TAK\n");
printf("%d %d %d\n" , , , );
return ;
}
for (int i = ; i < n; ++i) read(d1[i]);
for (int i = ; i < n; ++i) read(d2[i]);
if (check())
return ;
int line = inf;
for (int i = ; i < n; ++i) chkmin(line , d1[i] + d2[i]);
mp[] = ;
mp[line] = n;
for (int i = ; i < n; ++i) {
if (d1[i] + d2[i] == line) {
if (mp[d1[i]] != ) {
printf("NIE\n");
return ;
}
mp[d1[i]] = i;
}
}
int pre = ;
for (int i = ; i <= line; ++i) {
if (mp[i]) {
add(mp[pre] , mp[i] , i - pre);
pre = i;
}
}
for (int i = ; i < n; ++i) {
if (d1[i] + d2[i] - line != ) {
int tmp = d1[i] + d2[i] - line;
if (tmp & ) {
printf("NIE\n");
return ;
}
int len = d1[i] - tmp / ;
if (len < || mp[len] == ) {
printf("NIE\n");
return ;
}
add(i , mp[len] , tmp / );
}
}
puts("TAK");
dfs( , ); return ;
}
[POI 2018] Plan Metra的更多相关文章
- bzoj5100 [POI2018]Plan metra 构造
5100: [POI2018]Plan metra Time Limit: 40 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 189 Sol ...
- bzoj千题计划249:bzoj5100: [POI2018]Plan metra
http://www.lydsy.com/JudgeOnline/problem.php?id=5100 1.找到d1[i]+dn[i] 最小的点,作为1到n链上的点 2.令链长为D,若abs(d1[ ...
- 【BZOJ5100】[POI2018]Plan metra 构造
[BZOJ5100][POI2018]Plan metra Description 有一棵n个点的无根树,每条边有一个正整数权值,表示长度,定义两点距离为在树上的最短路径的长度. 已知2到n-1每个点 ...
- BZOJ5100 : [POI2018]Plan metra
若$1$到$n$之间没有其它点,则$1$到$n$的距离为任意一点到它们距离的差值,按照距离关系判断每个点是挂在$1$上还是挂在$n$上即可. 否则$1$到$n$的距离只可能为任意一点到它们距离和的最小 ...
- 解题:POI 2018 Prawnicy
题面 网上好像都是堆的做法啊......我这个不算离散化是$O(n)$的说(虽然有一坨vector可能不开O2会爆炸) 题目即是让我们求是否存在一个最长的是不少于$k$个给出区间子集的区间,如果存在输 ...
- POI 2018.10.20
[POI2005]BANK-Cash Dispenser 有多少个4位字符串是所有操作序列的子串. 10^4枚举字符串.暴力判断会TLE 发现,我们就是在每个操作序列中不断找第一个出现的c字符. 预处 ...
- POI 2018.10.27
[POI2015]LOG 维护一个长度为n的序列,一开始都是0,支持以下两种操作:1.U k a 将序列中第k个数修改为a.2.Z c s 在这个序列上,每次选出c个正数,并将它们都减去1,询问能否进 ...
- POI 2018.10.22
[POI2015]ODW 喵锟讲过.分块. N>=blo,那就暴力倍增往上跳.O(N/blo*logN) N<blo,预处理,f[i][j]表示,i往上跳,每次跳j步,到根节点为止,权值和 ...
- POI 2018.10.21
[POI2008]TRO-Triangles https://www.cnblogs.com/GXZlegend/p/7509699.html 平面上有N个点. 求出所有以这N个点为顶点的三角形的面积 ...
随机推荐
- memcache 使用方法
Memcache::add // 添加一个值,如果已经存在,则返回falseMemcache::addServer // 添加Memcache地址Memcache::close // 关闭一个Memc ...
- 模型层TP框架数据库的操作
在shop入口的文件下的HOME文件夹中使用模型层 第一步修改配置模块把数据库的各种链接做好,打开HOME中的conf文件夹中的config.php,找到Thinkphp文件加下的conf文件打开co ...
- 【BZOJ4930】棋盘 拆边费用流
[BZOJ4930]棋盘 Description 给定一个n×n的棋盘,棋盘上每个位置要么为空要么为障碍.定义棋盘上两个位置(x,y),(u,v)能互相攻击当前仅 当满足以下两个条件: 1:x=u或y ...
- 九度OJ 1345:XXX定律之画X (递归)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:361 解决:157 题目描述: 给你一个n,然后让你输出F(n) 规则是这样的,F(n)的输出结果是: F(n-1) F(n-1) ...
- 【题解】POJ2279 Mr.Young′s Picture Permutations dp
[题解]POJ2279 Mr.Young′s Picture Permutations dp 钦定从小往大放,然后直接dp. \(dp(t1,t2,t3,t4,t5)\)代表每一行多少人,判断边界就能 ...
- Bootstrap 第一天
Bootstrap第一天 1.什么是Bootstrap? Bootstrap是由两位设计开发的. Bootstrap主要是前端的框架(HTML.CSS.JS). 2.为什么使用Boot ...
- ALV调用的几个函数
转 ALV的调用主要由以下几个标准函数实现,所有函数的输入输出参数必须大写,否则系统会出现异常中止,相关函数如下: 1)REUSE_ALV_FIENDCATALOG_MERGE:根据内表结构返回FI ...
- easy_install和pip的安装及使用
在终端输入命令报错后,在网上找到了这篇博客,用easy_install命令安装pip,问题解决 Fatal error in launcher: Unable to create process us ...
- rails 运维常用命令
创建生产环境数据库并执行迁移 $ RAILS_ENV=production rake db:create$ RAILS_ENV=production rake db:migrate RAILS_ENV ...
- POJ 2230 Watchcow 【欧拉路】
Watchcow Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 6336 Accepted: 2743 Specia ...