把a[i], b[i]分开来排序

对应位置上的点连边

感性理解这是最小的

会连出若干个环

要使得若干个环连成大环

令a[i]向b[i - 1] 连边

易证一定能使图联通

感性理解这也是最小的

#include<cstdio>
#include<algorithm>
using namespace std;
const int P = 32767;
int n, x, y, z, F[1000005];
struct node
{
int val, id;
}a[1000005], b[1000005], e[1000005];
bool cmp(node a, node b)
{
return a.val < b.val;
}
int find(int x)
{
if (F[x] != x) F[x] = find(F[x]);
return F[x];
}
int main()
{
scanf("%d", &n);
scanf("%d%d%d%d%d", &a[1].val, &a[2].val, &x, &y, &z);
for (int i = 3; i <= n; i++) a[i].val = (1ll * x * a[i - 1].val + 1ll * y * a[i - 2].val + z) % P;
scanf("%d%d%d%d%d", &b[1].val, &b[2].val, &x, &y, &z);
for (int i = 3; i <= n; i++) b[i].val = (1ll * x * b[i - 1].val + 1ll * y * b[i - 2].val + z) % P;
for (int i = 1; i <= n; i++) a[i].id = b[i].id = i;
sort(a + 1, a + n + 1, cmp);
sort(b + 1, b + n + 1, cmp);
for (int i = 1; i <= n; i++) F[i] = i;
int ans = 0;
for (int i = 1; i <= n; i++)
{
int x = a[i].id, y = b[i].id;
int fx = find(x), fy = find(y);
F[fy] = fx;
ans = max(ans, a[i].val * a[i].val - b[i].val * b[i].val);
}
for (int i = 2; i <= n; i++) e[i - 1] = (node){a[i].val * a[i].val - b[i - 1].val * b[i - 1].val, i};
sort(e + 1, e + n, cmp);
for (int i = 1; i < n; i++)
{
int x = a[e[i].id].id, y = b[e[i].id - 1].id;
int fx = find(x), fy = find(y);
if (fx != fy)
{
F[fy] = fx;
ans = max(ans, e[i].val);
}
}
printf("%d\n", ans);
return 0;
}

  

2139: road的更多相关文章

  1. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  2. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  3. POJ 3204 Ikki's Story I - Road Reconstruction

    Ikki's Story I - Road Reconstruction Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 7 ...

  4. Codeforces #380 div2 C(729C) Road to Cinema

    C. Road to Cinema time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  5. dp or 贪心 --- hdu : Road Trip

    Road Trip Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users: 29 ...

  6. HDU 1598 find the most comfortable road(最小生成树之Kruskal)

    题目链接: 传送门 find the most comfortable road Time Limit: 1000MS     Memory Limit: 32768 K Description XX ...

  7. 三分 --- CSU 1548: Design road

    Design road Problem's Link:   http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1548 Mean: 目的:从(0,0)到 ...

  8. hdu 5861 Road 两棵线段树

    传送门:hdu 5861 Road 题意: 水平线上n个村子间有 n-1 条路. 每条路开放一天的价格为 Wi 有 m 天的操作,每天需要用到村子 Ai~Bi 间的道路 每条路只能开放或关闭一次. ( ...

  9. HDU4081 Qin Shi Huang's National Road System(次小生成树)

    枚举作为magic road的边,然后求出A/B. A/B得在大概O(1)的时间复杂度求出,关键是B,B是包含magic road的最小生成树. 这么求得: 先在原图求MST,边总和记为s,顺便求出M ...

随机推荐

  1. 计算结构体、数组、指针的sizeof

    1. 结构体的sizeof 题目: sturct aa{ in num; char name[10];}; struct bb{ int a; float b; struct aa c;}; stru ...

  2. MySQL入门很简单: 10 mysql运算符

    1. 算术运算符 例子: 将t1表中字段a的值进行加法,减法和乘法 2. 比较运算符 注:LIKE经常和通配符"_"和"%"一起使用,"_" ...

  3. 修改jupyter notebook的工作路径

    两种方法 一 修改jupyter notebook快捷方式的属性 ①根据下图找到jupyter的快捷方式:jupyter notebook→更多→打开文件位置 ②右键打开属性-将目标一栏中最后的%US ...

  4. linux lnmp搭建

    1.安装nginx: yum install gcc -y yum install -y pcre pcre-devel yum install -y zlib zlib-devel yum inst ...

  5. UVA-674 Coin Change---完全背包

    题目链接: https://vjudge.net/problem/UVA-674 题目大意: 有5种硬币, 面值分别为1.5.10.25.50,现在给出金额,问可以用多少种方式组成该面值. 思路: 每 ...

  6. LA 3938 动态最大连续和

    题目链接:https://vjudge.net/contest/146667#problem/C 题意:动态的求一个区间的最大连续和. 分析: 看上去可以RMQ去做,但是,当分成两个部分,原来的部分的 ...

  7. 空间最短路径,BFS(POJ3278)

    题目链接:http://poj.org/problem?id=3278 #include <cstdio> #include <queue> #include <stri ...

  8. Poj (3239),m皇后问题

    题目链接:http://poj.org/problem?id=3239 构造法很牛逼啊,把这个搜索的题直接变成了打表. 我用dfs写了一下. 构造法公式(序列):一.当n mod 6 != 2 或 n ...

  9. 模拟猜数(POJ2328)

    题目链接:http://poj.org/problem?id=2328 解题报告: 缩短区间,soeasy, #include <stdio.h> #include <stdlib. ...

  10. http长链接

    之前说过http的请求是再tcp连接上面进行发送的,那么tcp连接就分为长连接 和 短连接这样的概念,那么什么是长链接呢?http请求发送的时候要先去创建一个tcp的连接,然后在tcp的连接上面发送h ...