ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1837. Isenbaev's Number

I get wa 7, please give me test 7
Posted by JAVATAR 18 Apr 2012 01:10
Hello,

What is on test 7?I get wa7 but tried all cases.please help.

here is my program.


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

/**
 *
 * @author Imran
 */
public class IsenbaevsNumber {

    public static void main(String args[]) throws IOException{

        Map m1=new HashMap();
        Map m2=new HashMap();
        List<Group> participantGroups=new ArrayList<Group>();;

        InputStreamReader isr=new InputStreamReader(System.in);
        BufferedReader bfr=new BufferedReader(isr);

        int n=Integer.parseInt(bfr.readLine());
        int i=0;
        StringBuilder sb;
        int id=0;

        while(i<n){
            i++;
            //read the string
            sb=new StringBuilder(bfr.readLine());

            //get first participant
            String p1=sb.substring(0, sb.indexOf(" "));
            sb.delete(0,sb.indexOf(" ")+1);
            //get second participant
            String p2=sb.substring(0, sb.indexOf(" "));
            sb.delete(0,sb.indexOf(" ")+1);
            //get third partisipant
            String p3=sb.toString();

            Participant po1=(Participant)m1.get(p1);
            int mm=4;

            if(p1.equals("Isenbaev")||p2.equals("Isenbaev")||p3.equals("Isenbaev"))
                mm=2;

            if(po1==null){
                if(p1.equals("Isenbaev"))
                    po1=new Participant(p1, 0,id);
                else
                    po1=new Participant(p1, mm,id);
                    m1.put(p1, po1);
                    m2.put(po1.id, po1);
                    id++;
            }

            Participant po2=(Participant)m1.get(p2);
            if(po2==null){
                if(p2.equals("Isenbaev"))
                    po2=new Participant(p2, 0,id);
                else
                    po2=new Participant(p2, mm,id);
                    m1.put(p2, po2);
                    m2.put(po2.id, po2);
                    id++;
          }

            Participant po3=(Participant)m1.get(p3);
            if(po3==null){
                if(p3.equals("Isenbaev"))
                    po3=new Participant(p3, 0,id);
                else
                    po3=new Participant(p3, mm,id);
                     m1.put(p3, po3);
                     m2.put(po3.id, po3);
                     id++;
          }

            Group g=new Group();
            g.p1id=po1.id;
            g.p2id=po2.id;
            g.p3id=po3.id;

            participantGroups.add(g);
        }

        int[][] graph=new int[id][id];
        for(int j=0;j<participantGroups.size();j++){

            graph[participantGroups.get(j).p1id][participantGroups.get(j).p2id]=1;
            graph[participantGroups.get(j).p2id][participantGroups.get(j).p3id]=1;
            graph[participantGroups.get(j).p1id][participantGroups.get(j).p3id]=1;

            graph[participantGroups.get(j).p2id][participantGroups.get(j).p1id]=1;
            graph[participantGroups.get(j).p3id][participantGroups.get(j).p2id]=1;
            graph[participantGroups.get(j).p3id][participantGroups.get(j).p1id]=1;
        }

        for(int j=0;j<id;j++){
            Participant pt=(Participant)m2.get(j);
            for(int k=0;k<id;k++){
                Participant pt2=(Participant)m2.get(k);
                if(graph[j][k]==1&&pt2.value<pt.value)
                    pt.value=pt2.value+1;
            }

        }

        List<Participant> participantsByName = new ArrayList<Participant>(m1.values());
        Collections.sort(participantsByName,new NameComparator());

         for( Participant par : participantsByName){
            if(par.value==4)
                System.out.println(par.name+" undefined");
            else
                System.out.println(par.name+" "+par.value);
        }
    }
}

class Participant{
    int value=4;
    String name;
    int id;

    Participant(String name,int value,int id){
        this.name=name;
        this.value=value;
        this.id=id;
    }

    @Override
    public String toString(){
        return this.name;
    }

    public void setValue(int value){
        this.value=value;
    }

}

//Comparator
class NameComparator implements Comparator{

    @Override
    public int compare(Object o1, Object o2)throws UnsupportedOperationException {
        Participant p1=(Participant)o1;
        Participant p2=(Participant)o2;

            if(p1.name.compareTo(p2.name)>0)
                return 1;
            else
                return 0;



    }
}

class Group{
    int p1id;
    int p2id;
    int p3id;
}








Re: I get wa 7, please give me test 7
Posted by diviator 29 Apr 2012 18:30
Ошибка в сортировке по алфавитному порядку !! тест не знаю, но ошибка точно в сортировке
Re: I get wa 7, please give me test 7
Posted by JAVATAR 6 May 2012 14:13
Thank you very much! I did not pay attention to sorting will try to fix and see.
Re: I get wa 7, please give me test 7
Posted by JAVATAR 6 May 2012 14:44
I tried this http://www.albeesonline.com/blog/2008/10/16/sorting-an-arraylist-of-objects/ JAVA's built in sorting technique. Still no luck.

I wonder if the names are Aa and Aaa which one should come first.
My program outputs,
Aa
Aaa

Thanks in advance
Re: I get wa 7, please give me test 7
Posted by Andrew Shmig aka SyFyKid [Vladimir SU] 7 May 2012 16:54
I have used something like:

Person[] data = new Person[1000];

class Person implements Comparable{
   // some code

   public int compareTo(Object o){
      // here you write when to return <, =, >
   }
}

after that:
Arrays.sort(data);

or if you have Collection:
Collections.sort(data);

Edited by author 07.05.2012 16:58