HDU-3038-How Many Answers Are Wrong【 带权并查集 】题解
What’s more, if FF finds an answer to be wrong, he will ignore it when judging next answers.
But there will be so many questions that poor FF can’t make sure whether the current answer is right or wrong in a moment. So he decides to write a program to help him with this matter. The program will receive a series of questions from FF together with the answers FF has received from TT. The aim of this program is to find how many answers are wrong. Only by ignoring the wrong answers can FF work out the entire sequence of integers. Poor FF has no time to do this job. An 《一线大厂 Java 面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》开源 d now he is asking for your help~(Why asking trouble for himself~~Bad boy)
Input
Line 1: Two integers, N and M (1 <= N <= 200000, 1 <= M <= 40000). Means TT wrote N integers and FF asked her M questions.
Line 2…M+1: Line i+1 contains three integer: Ai, Bi and Si. Means TT answered FF that the sum from Ai to Bi is Si. It’s guaranteed that 0 < Ai <= Bi <= N.
You can assume that any sum of subsequence is fit in 32-bit integer.
Output
A single line with a integer denotes how many answers are wrong.
Sample Input
10 5
1 10 100
7 10 28
1 3 32
4 6 41
6 6 1
Sample Output
1
[](()2.题意
有 M 个数,不知道它们具体的值,但是知道某两个数之间(包括这两个数)的所有数之和,现在给出 N 个这样的区间和信息,需要判断有多少个这样的区间和与前边已知的区间和存在矛盾。
[](()3.思路
Java 开源项目【ali1024.coding.net/public/P7/Java/git】
带权并查集,将闭区间的某一端变成开区间。如果给定区间的两个端点属于同一个并查集,判断这个区间的值是否与计算得到的值相等;如果给定区间的两个端点不属于同一个并查集,将这两个并查集合并。并查集边的权值就等于题干中的区间和。
[](()4.代码
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn = 2e5 + 10;
int Sum[maxn], Fa[maxn];
void init(int n) //初始化
{
for (int i = 0; i <= n; i++)
{
Fa[i] = i;
Sum[i] = 0;
}
}
int find(int x) //查询
{
if (x != Fa[x])
{
int tmp = Fa[x];
Fa[x] = find(Fa[x]); //将 x 的父节点设置为根节点
Sum[x] += Sum[tmp];
}
return Fa[x];
结局:总结+分享
看完美团、字节、腾讯这三家的一二三面试问题,是不是感觉问的特别多,可能咱们真的又得开启面试造火箭、工作拧螺丝的模式去准备下一次的面试了。
开篇有提及我可是足足背下了 Java 互联网工程师面试 1000 题,多少还是有点用的呢,换汤不换药,不管面试官怎么问你,抓住本质即可!能读到此处的都是真爱
Java 互联网工程师面试 1000 题
而且从上面三家来看,算法与数据结构是必备不可少的呀,因此我建议大家可以去刷刷这本左程云大佬著作的 《程序员代码面试指南 IT 名企算法与数据结构题目最优解》,里面近 200 道真实出现过的经典代码面试题。
程序员代码面试指南--IT 名企算法与数据结构题目最优解
其余像设计模式,建议可以看看下面这 4 份 PDF(已经整理)
更多的 Java 面试学习笔记如下,关于面试这一块,我额外细分出 Java 基础-中级-高级开发的面试+解析,以及调优笔记等等等。。。
以上所提及的全部 Java 面试学习的 PDF 及笔记,如若皆是你所需要的,那么都可发送给你!
评论