HDU3038How Many Answers Are Wrong

来源:互联网 发布:博卡软件 编辑:程序博客网 时间:2024/06/10 14:24
F - How Many Answers Are Wrong
Crawling in process...Crawling failedTime Limit:1000MS    Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
SubmitStatus Practice HDU 3038

Description

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)       
              

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 51 10 1007 10 281 3 324 6 416 6 1
              

Sample Output

1
        

 

java代码:真不知道,而且没有解决方案,就是超内存。

import java.util.Scanner;import java.math.BigInteger;import java.math.BigDecimal;import java.util.Arrays;import java.util.ArrayList;public class Main{    final static int maxn = 200000 + 5;    static int [] par = new int [maxn];    static int [] val = new int [maxn];    static int N,M,a,b,c,ans;    public static void init()    {        Arrays.fill(par,-1);        Arrays.fill(val,0);    }    public static int find(int x)    {        /******************************************/        /*this result is get max(类似于后辍和,将他的范围扩大,以至于减少比较的次数)*/        /******************************************/        if(par[x] < 0)return x;        int t = par[x];        par[x] = find(par[x]);        val[x] += val[t];        return par[x];    }    public static void unite(int u,int v,int z)    {        int x = find(u);        int y = find(v);        par[y] = x;        val[y] = val[u] - val[v] - z;    }    public static void main(String [] agrs)    {        Scanner cin=new Scanner(System.in);        while(cin.hasNext())        {            ans=0;            init();            N=cin.nextInt();            M=cin.nextInt();            while(M-->0)            {                a=cin.nextInt();                b=cin.nextInt();                c=cin.nextInt();                int x=find(a);                int y=find(b+1);                if(x==y)                {                    if(val[a]-val[b+1]!=c)                    {                        ans++;                    }                }                else                {                    unite(a,b+1,c);                }            }            System.out.println(ans);        }    }}


 

C++代码

对于这道题目,主要是对情况的思考问题,将对一个区间处理,我最先是想用线段树做的,但是感觉极有可能超时,所以放弃了,然后发现这个像我们平时做的前辍和以及后辍和,于是就开始分析情况,对于其中的一个特别重要的部分,便是unite()函数里面的处理,当然,有些人可能觉得Find()函数里面的东西也有些深奥,那是你没有仔细思考,只是简单的用一个递归将后面的后辍和计算出来而已。

现在主要讲unite()部分的内容,为什么是这样了其实就是一个简单的合并过程:

[[[[[]]]]]]]][[[[]]]]],这个代表着一个个小区间,在你之前的操作,一定会是num[u]>num[v]+z;至于为什么,就是Find起的作用,他讲你之前的一些操作的区间全部链接到了一起,所以后面增加的一定是比他小的,或者是从来没有在已知区间出现过的区间。

#pragma comment(linker, "/STACK:1024000000,1024000000")#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn=200000+;int par[maxn],num[maxn];//num[i] indicate [i,par[i]) is minus.void init(){    memset(par,-1,sizeof(par));    memset(num,0,sizeof(num));}int find(int x){    if(par[x]<0)return x;    int n=par[x];    par[x]=find(par[x]);    num[x]+=num[n];    return par[x];}void uinte(int u,int v,int z){    int x=find(u);    int y=find(v);    par[y]=x;    num[y]=num[u]-num[v]-z;    return;}int main(){    int N,M;    int a,b,s,ans,c;    while(~scanf("%d%d",&N,&M))    {        init();        ans=0;        for(int i=0; i<M; i++)        {            scanf("%d %d %d",&a,&b,&c);            int x=find(a);            int y=find(b+1);            if(x==y)  //If par[a]==par[b+1] .........[a,b]<->[b+1,par[b+1]]            {                if(num[a]-num[b+1]!=c)   //If the event conflict,two number are not equal,so add a record.                {                    ans++;                }            }            else    //Add data into the array.            {                uinte(a,b+1,c);            }        }        printf("%d\n",ans);    }    return 0;}


 

 

0 0
原创粉丝点击