题意:M×N的矩阵 让你保持每行每列的大小对应关系不变,将矩阵重写,重写后的最大值最小. 思路:离散化思想+并查集,详见代码 好题! #include <iostream> #include <string.h> #include <stdio.h> #include <algorithm> #include <cmath> #include <cstdlib> #include <bits/stdc++.h> using…
题目链接: http://codeforces.com/problemset/problem/650/C 题意: 给定n*m的矩阵,要求用最小的数表示每个元素,其中各行各列的大小关系保持不变. 分析: 将所有元素从小到大排序,然后找到每个元素相应位置进行填充,由于题目要求是每行每列的大小关系保持不变,所以填充的元素应为所在行和所在列中最大元素+1,保存好各行各列当前的最大值,并根据最后填充的元素不断更新就好啦. 问题是如何处理相同元素以及他们所在行列的更新. 这里使用并查集,将相同元素值的位置保…
E. Table Compression 题目连接: http://www.codeforces.com/contest/651/problem/E Description Little Petya is now fond of data compression algorithms. He has already studied gz, bz, zip algorithms and many others. Inspired by the new knowledge, Petya is now…
E. Table Compression time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output Little Petya is now fond of data compression algorithms. He has already studied gz, bz, zipalgorithms and many others.…
Little Petya is now fond of data compression algorithms. He has already studied gz, bz, zip algorithms and many others. Inspired by the new knowledge, Petya is now developing the new compression algorithm which he wants to name dis. Petya decided to…
C. Table Compression time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard output Little Petya is now fond of data compression algorithms. He has already studied gz, bz, zip algorithms and many others. Ins…
传送门 time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output Little Petya is now fond of data compression algorithms. He has already studied gz, bz, zip algorithms and many others. Inspired by the…
Spies Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Description In the aftermath of Canada’s annexation of Pittsburgh tensions have been pretty high between Canada and the US. You have personally been hired…
题目链接:http://codeforces.com/contest/859/problem/E 题意:有N个人.2N个座位.现在告诉你这N个人它们现在的座位.以及它们想去的座位.每个人可以去它们想去的座位或者就站在原地不动.新的座位和旧的座位,都不允许一个座位被两个人占据的情况.问你安排的方案数. 解法:对于这N个点,N条边构成的图,我们应该对每个连通块独立计算答案,最后乘起来.如果n个点,n-1条边答案显然为n.如果n个点n条边,会出现一个环,且恰好只有一个环.如果是一个自环,那么答案是1,…
题目链接:http://codeforces.com/contest/828/problem/C C. String Reconstruction time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Ivan had string s consisting of small English letters. However, hi…
Codeforces 题目传送门 & 洛谷题目传送门 看到集合的合并,可以本能地想到并查集. 不过这题的操作与传统意义上的并查集不太一样,传统意义上的并查集一般是用来判断连通性的,而此题还需支持赋值.集合内整体加等操作,似乎就有些难处理. 我们不妨考虑此题的弱化版:没有第二类集合的版本.也就是要求支持合并两个集合,集合整体加某个数 \(x\),单点查询三个操作. 很明显集合整体加某个数 \(x\) 就相当于在并查集根节点处打一个 \(+x\) 的标记,查询就暴力跳父亲求出待询问点到根节点路径上所…
Civilization 题目链接: http://acm.hust.edu.cn/vjudge/contest/121334#problem/B Description Andrew plays a game called "Civilization". Dima helps him. The game has n cities and m bidirectional roads. The cities are numbered from 1 to n. Between any pa…
题目大意:给出n个数,要求将n个数分配到两个集合中,集合0中的元素x,要求A-x也再0中,同理1集合. 写了几个版本号,一直WA在第8组数据...最后參考下ans,写了并查集过了 学到:1.注意离散的逻辑思维,官方答案的 从条件推逆否命题 2.并查集做法:fa[find(i)]=mp[a-p[i]] ? find(a-p[i]) : find(n+2); 3.离散化然后hash的方法,用map时间还是承受得住的,写起来也简单 //#pragma comment(linker, "/STACK:1…
<题目链接> 题目大意: 有n个人,其中有m对朋友,现在你有一个秘密你想告诉所有人,第i个人愿意出价a[i]买你的秘密,获得秘密的人会免费告诉它的所有朋友(他朋友的朋友也会免费知道),现在他们想出最少的价钱买秘密,那么你最少能得到多少钱? 解题分析: 题意很明显,就是求出每个块最低的价格,然后将这些价格相加就行,处理的时候,我们对并查集根的值进行更新,记录该根所对应块中的最低价格. #include <cstdio> #include <iostream> #inclu…
题意:给定 3 种操作, 第一种 1 u v 把 u 和 v 合并 第二种 2 l r 把 l - r 这一段区间合并 第三种 3 u v 判断 u 和 v 是不是在同一集合中. 析:很容易知道是用并查集来做,但是如果单纯的用并查集,肯定是要超时的,所以要用链表,如果合并了,就把链表指向, 这样就搞定了这个题. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #i…
好久没有写过图论的东西了,居然双向边要开两倍空间都忘了,不过数组越界cf居然给我报MLE??这个题题意特别纠结,一开始一直不懂添加的边长是多长... 题意:给你一些点,然后给一些边,注意没有重边 环,接着给你两种操作: 1 x :求出 x 的集合中最长的边权值 2 x y:合并 x 的集合和 y的集合变成一个集合,并且将 x 集合中任意一个点与 y 集合中任意一个点相连,使合成的集合的任意两个点的最大权值最小,其中两个点相连的权值为1 开始边是固定的,因而建图并两次dfs遍历找树直径,但是不一定…
#include<bits/stdc++.h>using namespace std;long long w[100007],sum[100007];int fa[100007],degree[100007],dp[100007],depth[100007];int flag[100007];int vis[100007]; int find_x(int x)//非递归并查集{    int k, j, r;    r = x;    while(r != dp[r])     //查找根节点…
String Reconstruction Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun of him and hid the string s. Ivan preferred making a new string to finding the old one. Ivan knows some information about the s…
Description Arseniy is already grown-up and independent. His mother decided to leave him alone for m days and left on a vacation. She have prepared a lot of food, left some money and washed all Arseniy's clothes. Ten minutes before her leave she real…
题意:给定 n 个数,然后每次破坏一个位置的数,那么剩下的连通块的和最大是多少. 析:用并查集来做,从后往前推,一开始什么也没有,如果破坏一个,那么我们就加上一个,然后判断它左右两侧是不是存在,如果存在,那么就合并起来, 然后不断最大值,因为这个最大值肯定是不递减,所以我们一直更新就好. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <s…
题意:给你一颗树,树边的权值可能是0或1,问先走0边,再走1边,或者只走1边的路径有多少条? 思路:对于一个点,假设通过0边相连的点一共有x个(包括自己),通过1边相连的有y个(包括自己),那么对答案的贡献为x * y - 1,意思是以x个点为起点,以y个点为终点了路径条数,-1是因为自己到自己的不算路径,用带权并查集去统计x和y即可. 代码: #include <bits/stdc++.h> #define LL long long #define db double #define INF…
题目链接:戳这里 转自:参考博客 题意:给一个n*m的矩阵,放入q个点,这q个点之间的关系是,若已知这样三个点(x1,y1),(x2,y1),(x1,y2),可以在(x2,y2)处生成一个新的点,对于新的点和被使用过的点都能重复使用,问你最少需要在矩阵汇总添加多少个点是的整个矩阵被点铺满? 解题思路:通过画图可以发现我们对于一个N*M的空矩阵,我们最少填加n+m-1个点就能再通过题意给出的操作不断生成新的点铺满整个图.而且对于题目给出的操作,我们可以发现有这样的规律: 当插入点(x1,y1) 时…
题意抽象出来就是求联通块的个数吧,然后添加最少边使图联通. 注意所有人都不会任何语言的时候,答案是n而不是n-1. #include<algorithm> #include<iostream> #include<cstring> #include<fstream> #include<sstream> #include<vector> #include<string> #include<cstdio> #incl…
Restructuring Company Even the most successful company can go through a crisis period when you have to make a hard decision — to restructure, discard and merge departments, fire employees and do other unpleasant stuff. Let's consider the following mo…
链接 B. Two Sets 题意 给两个集合A B,两个数a b,n个数x,分配n个数到两个集合,要求x , a-x在同一个集合,x , b-x在同一个集合,属于A集合的数输出0,B的输出1,无解输出NO,空集也算一个集合,所以存在所有数都在一个集合. 做法 这有一个样例 input 6 5 9 1 2 3 4 5 8 output YES 1 0 0 1 1 1 [1 4]A,[4 5]B,[1,8]B,所以只能把1 4都放进B集合 首先,能在A里面处理的就放入A,否则放入B,能在B里面处理…
题意 https://vjudge.net/problem/CodeForces-1243D 有一张完全图,n个节点 有m条边的边权为1,其余的都为0 这m条边会给你 问你这张图的最小生成树的权值 思路 很简单的思路就是将权值为0的边构成的子图缩成若干个连通块,那么答案就是连通块个数-1了. 但是,边数太多了,有n*(n-1)/2-m条权值为0的边. 考虑对于每一个点,计算这个点到其连的点所在集合的边数,用map存,然后和对应集合大小(点数)比较,如果前者小于后者,那么以为着这个点到这个集合还有…
题目链接: E. Table Compression time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output Little Petya is now fond of data compression algorithms. He has already studied gz, bz, zip algorithms and many o…
题目链接: http://codeforces.com/problemset/problem/650/C C. Table Compression time limit per test4 secondsmemory limit per test256 megabytes 问题描述 Little Petya is now fond of data compression algorithms. He has already studied gz, bz, zip algorithms and m…
C. Table Compression Little Petya is now fond of data compression algorithms. He has already studied gz, bz, zip algorithms and many others. Inspired by the new knowledge, Petya is now developing the new compression algorithm which he wants to name d…
题意:给你一个n*m的矩阵,需要在不改变每一行和每一列的大小关系的情况下压缩一个矩阵,压缩后的矩阵所有数的总和尽量的小. 思路:我们有这样的初步设想:对于在一行或一列的数x,y,若x<y,则建立一条x的位置到y的位置的边.之后进行拓扑排序的DP即可.然而会被卡边数卡掉,所以需要其它的解法. 新思路:我们把所有的数排个序,这样方便选对所有相同的数赋值.我们从小到大对所有的数赋值,合并这个数所在的行和列,选取相关的行和列中的最大值+1作为作为这个数的新值. 为什么这样做正确呢?可以类比拓扑排序的dp…