How Many Answers Are Wrong HDU - 3038 并查集,加权(路径)
时间:2019-09-27
本文章向大家介绍How Many Answers Are Wrong HDU - 3038 并查集,加权(路径),主要包括How Many Answers Are Wrong HDU - 3038 并查集,加权(路径)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
TT and FF are ... friends. Uh... very very good friends -________-b
FF is a bad boy, he is always wooing TT to play the following game with him. This is a very humdrum game. To begin with, TT should write down a sequence of integers-_-!!(bored).
Then, FF can choose a continuous subsequence from it(for example the subsequence from the third to the fifth integer inclusively). After that, FF will ask TT what the sum of the subsequence he chose is. The next, TT will answer FF's question. Then, FF can redo this process. In the end, FF must work out the entire sequence of integers.
Boring~~Boring~~a very very boring game!!! TT doesn't want to play with FF at all. To punish FF, she often tells FF the wrong answers on purpose.
The bad boy is not a fool man. FF detects some answers are incompatible. Of course, these contradictions make it difficult to calculate the sequence.
However, TT is a nice and lovely girl. She doesn't have the heart to be hard on FF. To save time, she guarantees that the answers are all right if there is no logical mistakes indeed.
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. And now he is asking for your help~(Why asking trouble for himself~~Bad boy)
FF is a bad boy, he is always wooing TT to play the following game with him. This is a very humdrum game. To begin with, TT should write down a sequence of integers-_-!!(bored).
Then, FF can choose a continuous subsequence from it(for example the subsequence from the third to the fifth integer inclusively). After that, FF will ask TT what the sum of the subsequence he chose is. The next, TT will answer FF's question. Then, FF can redo this process. In the end, FF must work out the entire sequence of integers.
Boring~~Boring~~a very very boring game!!! TT doesn't want to play with FF at all. To punish FF, she often tells FF the wrong answers on purpose.
The bad boy is not a fool man. FF detects some answers are incompatible. Of course, these contradictions make it difficult to calculate the sequence.
However, TT is a nice and lovely girl. She doesn't have the heart to be hard on FF. To save time, she guarantees that the answers are all right if there is no logical mistakes indeed.
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. And now he is asking for your help~(Why asking trouble for himself~~Bad boy)
InputLine 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.
OutputA 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
题意:有大小为n的数列,m次数据,每次数据给出数列中从第a个到第b个的和为c,问这些数据中有多少数据是
错误的。
思路:将每个数想象成一条线段,线段的左边为这个数,右边为下一个数,因此数组要开n+1大小,用并查集给这些一个祖先,开一个数组存放这些数到祖先的距离。具体见代码
代码:
1 #include <cstdio> 2 #include <fstream> 3 #include <algorithm> 4 #include <cmath> 5 #include <deque> 6 #include <vector> 7 #include <queue> 8 #include <string> 9 #include <cstring> 10 #include <map> 11 #include <stack> 12 #include <set> 13 #include <sstream> 14 #include <iostream> 15 #define mod 998244353 16 #define eps 1e-6 17 #define ll long long 18 #define INF 0x3f3f3f3f 19 using namespace std; 20 21 //fa保存下标对应的祖先 22 //num保存下标到祖先节点的距离 23 int fa[200005],num[200005]; 24 //初始化 25 void init(int n) 26 { 27 //因为有n个线段,那么有n+1个点 28 for(int i=0;i<=n+1;i++) 29 { 30 //初始情况下每个点的祖先为自己 31 fa[i]=i; 32 //初始情况下每个点到祖先的距离为0 33 num[i]=0; 34 } 35 } 36 //找到祖先,并进行压缩路径的优化 37 int find(int s) 38 { 39 //如果祖先为自己则返回自己 40 if(fa[s]==s) 41 { 42 return s; 43 } 44 //递归寻找祖先 45 int temp=find(fa[s]); 46 //将自己到祖先的距离加上祖先到祖先的祖先的距离 47 num[s]+=num[fa[s]]; 48 //压缩路径优化 49 fa[s]=temp; 50 //返回祖先 51 return fa[s]; 52 } 53 int main() 54 { 55 int n,m; 56 while(scanf("%d %d",&n,&m)!=EOF) 57 { 58 //初始化数据 59 init(n); 60 int a,b,c; 61 int ans=0; 62 for(int i=0;i<m;i++) 63 { 64 scanf("%d %d %d",&a,&b,&c); 65 //加一是因为从a到b是从a线段的起点到b线段的终点 66 b++; 67 //找到a和b的最远祖先 68 int x=find(a); 69 int y=find(b); 70 //如果两个祖先不相等,合并两个点到各自祖先的距离 71 if(x!=y) 72 { 73 //将b的祖先上再加一个祖先,即为b的祖先的祖先为a的祖先 74 fa[y]=x; 75 //因为将b的祖先加到了a的祖先的上,所以b到a的祖先的距离为 76 //b的a的距离加上a到a祖先的距离,或b到b祖先的距离加上b祖先到a祖先的距离 77 //因此b祖先到a祖先的距离等于b的a的距离加上a到a祖先的距离减去b到b祖先的距离 78 num[y]=num[a]+c-num[b]; 79 } 80 else 81 { 82 //b到b祖先的距离减去a到a祖先的距离即为a到b的距离 83 //判断c是否为a到b的距离 84 //不是则表示这句话的c是错的 85 if(c!=num[b]-num[a]) 86 { 87 ans++; 88 } 89 } 90 } 91 printf("%d\n",ans); 92 } 93 }
原文地址:https://www.cnblogs.com/mzchuan/p/11597348.html
- 远控木马Posion Ivy开始肆虐缅甸和其它亚洲国家
- Slf4j+Logback配置文件变量使用小记
- Storm消息处理可靠性保证
- git+github创建分支&提交并贡献代码(linux环境)
- 使用Nginx代理restful实现SSL链路加密
- 使用Nginx代理thrift NIO实现SSL链路加密
- TThreadedSelectorServer介绍及Direct Memory OOM分析
- 通过Java程序提交通用Mapreduce任务并获取Job信息
- Mapreduce 任务提交源码分析1
- Java分布式神经网络库Deeplearning4j 环境搭建和运行一个例子
- Java分布式神经网络库Deeplearning4j之上手实践手写数字图像识别与模型训练
- 调用腾讯优图开放平台进行人脸识别-Java调用API实现
- Spring的事务传播行为
- Jquery 获取checkbox属性checked为undefined和改变状态 值不变
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- [Python运维]使用cx_Oracle连接Oracle(高级篇)
- [Python运维]使用Python发送邮件
- [Python运维]自动化监控Oracle表空间并发送报警
- [Python运维]自动化监控多个Oracle表空间
- c# 动态生成控件
- C# 10分钟完成百度人脸识别——入门篇
- C#开启线程的四种方式
- C++入门实例:创建工程、opencv引用及简单实例
- C#实例:datagridview单元格合并
- C# 封装实例
- C# Socket编程笔记
- C#实例:串口通讯
- C#中的信号量---Semaphore
- C# excel文件导入导出
- 「变更管理」成功的变更管理—Kotter的8步变更模型