把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. MyEclipse导入JAVA工程显示红色叉叉的解决方法

    当我们有时候导入一个新的工程的时候可能会出现以下这种情况,基本上是因为jar包路径的问题. 解决方法如下: 1.右击工程,选择properties 2.选择 Java Build Path -> ...

  2. IOS NSThread 线程间通信

    @interface HMViewController () @property (weak, nonatomic) IBOutlet UIImageView *imageView; @end @im ...

  3. IOS ScrollView的使用 and delegate

    ScrollView常用的属性设置 //设置内容尺寸 // CGFloat contentH=self.lastBtn.frame // .origin.y+self.lastBtn.frame.si ...

  4. MySQL 相关文章参考

    MySQL 中隔离级别 RC 与 RR 的区别http://www.cnblogs.com/digdeep/p/4968453.html MySQL+InnoDB semi-consitent rea ...

  5. bzoj3882 [Wc2015]K小割

    Description Input Output Sample Input 3 3 1 3 100 1 2 3 2 3 4 1 3 5 Sample Output 8 9 12 -1   正解:暴搜+ ...

  6. ABI (应用程序二进制接口)

    http://baike.baidu.com/link?url=ybErtZo0zAB5_P-kKZmT_nd9FdxaAAPqW4jqtwN5Tmu_q8weayOOFOJBrXw1RLbR20sK ...

  7. Breaking Biscuits(模板题-求凸边形的宽)

    Breaking Biscuits 时间限制: 1 Sec  内存限制: 128 MB  Special Judge提交: 70  解决: 26[提交] [状态] [讨论版] [命题人:admin] ...

  8. Linux(二) - Unix&Linux 基本概念

    主机 = 内核 + 实用工具 内核(kernel) 当计算机启动时,计算机要经历一系列动作,这些动作构成了引导过程.引导过程的最后一个动作是启动一个非常复杂的程序,该程序就被称为内核(Kernel) ...

  9. javaweb基础(38)_事务

    一.事务的概念 事务指逻辑上的一组操作,组成这组操作的各个单元,要不全部成功,要不全部不成功. 例如:A——B转帐,对应于如下两条sql语句  update from account set mone ...

  10. 统计函数运行时间-CPU端

    C/C++中的计时函数是clock(),而与其相关的数据类型是clock_t.在MSDN中,查得对clock函数定义如下:  clock_t clock( void ); 这个函数返回从“开启这个程序 ...