How to resolve time-exceeded error(codechef) for stable-marraige? - java

I am trying to implement the stable marriage using lists and arrays. When i try to submit it on codechef it gives runtime exceeded errors.If someone could help but i want to use the very same data-structure i am using.
import java.util.*;
public class Main {
private int size;
private static LinkedList<Integer>[] menlist;
private static int[][] womatrix;
private static int[][] invwomatrix;
private static int[] womatch;
private static int[] menmatch;
public static void main(String[] args){
Scanner Input = new Scanner(System.in);
int TestCases = Input.nextInt();
while(TestCases-- > 0) {
int size = Input.nextInt();
womatrix=new int[size][size];
invwomatrix=new int[size][size];
menlist=new LinkedList[size];
womatch=new int[size];
menmatch=new int[size];
for(int i = 0; i < size; i++){
int temp=Input.nextInt();
for(int j = 0; j < size; j++)
{
womatrix[i][j] = Input.nextInt();
invwomatrix[i][womatrix[i][j]-1]=j+1;
}
}
for(int i = 0; i < size; i++){
int temp=Input.nextInt();
LinkedList<Integer> menpref = new LinkedList<Integer>();
for(int j = 0; j < size; j++)
{
int a=Input.nextInt();
menpref.add(a);
}
menlist[i]=menpref;
}
LinkedList<Integer> ll = new LinkedList<Integer>();
for(int i=0;i<size;i++){
womatch[i]=0;
menmatch[i]=0;
ll.add(i+1);
}
while((ll.isEmpty()!=true) || menlist[ll.getFirst()-1].isEmpty()!=true ){
int newman=ll.removeFirst();
//System.out.print("freeman "+newman+" "+menlist[newman-1].isEmpty()+"\n");
int women=(menlist[newman-1].removeFirst());
//System.out.print("highrank "+women+"intial_pair "+womatch[women-1]+"\n");
if(womatch[women-1]==0){
womatch[women-1]=newman;
menmatch[newman-1]=women;
//int a=ll.removeFirst();
}
else{
int oldman=womatch[women-1];
if(invwomatrix[women-1][newman-1]<invwomatrix[women-1][oldman-1]){
womatch[women-1]=newman;
menmatch[newman-1]=women;
ll.addFirst(oldman);
}
else{
ll.addFirst(newman);
}
}
/*for(int i=0; i<this.size; i++) {
System.out.print((i+1)+" "+ menmatch[i] +"\n");
}*/
if(ll.isEmpty()==true){break;}
}
for(int k=0;k<size;k++){
System.out.print((k+1)+" "+menmatch[k] +"\n");
}
}
}
}

Related

How to find the total of an 2d array?

Have to input numbers of students & subjects. (Using random marks) I want to print the total marks of students & the total marks of subjects. Here is the code given below.
import java.util.*;
class Example{
public static void readMarks(int[][] x){
Random r=new Random();
for(int i=0; i<x.length; i++){
for(int j=0; j<x[i].length; j++){
x[i][j]=r.nextInt(101);
}
}
}
public static void printMarks(int[][] x){
for(int i=0; i<x.length; i++){
for(int j=0; j<x[i].length; j++){
System.out.print(x[i][j]+" ");
}
System.out.println();
}
}
public static int[] findStudenTot(int[][] x){
int tot[]=new int[x.length];
for(int i=0; i<x.length; i++){
for (int j = 0; j<x[i].length; j++){
tot[i]+=x[i][j];
}
}
return tot;
}
public static int[] findSubjectTot(int[][] x){
int tot1[]=new int[x.length];
for(int i=0; i<x.length; i++){
for (int j = 0; j<x[i].length; j++){
tot1[i]+=x[i][j];
}
}
return tot1;
}
public static void main(String args[]){
Scanner input=new Scanner(System.in);
System.out.print("Input no of students : ");
final int N=input.nextInt();
System.out.print("Input no of subject : ");
final int S=input.nextInt();
int[][] marks=new int[N][S];
readMarks(marks);
int[] stTotal=findStudenTot(marks);
int[] subTotal=findSubjectTot(marks);
System.out.println("total is : "+stTotal);
System.out.println("total is : "+subTotal);
}
}
How to print stTotal , subTotal?
If we assume enter 5 for both inputs this output total is : [I#3d4eac69 ...why?
what are these characters & how can I avoid these?
How to print stTotal , subTotal?
Do it as follows:
System.out.println("total is : " + Arrays.toString(stTotal));
System.out.println("total is : " + Arrays.toString(subTotal));
[Update]
Replace the definition of findSubjectTot with the following:
public static int[] findSubjectTot(int[][] x) {
int tot[] = new int[x[0].length];
for (int i = 0; i < x[0].length; i++) {
for (int j = 0; j < x.length; j++) {
tot[i] += x[j][i];
}
}
return tot;
}
i think the issue lies in findStudentTot Method, you seem to have return type of 2D array but is returning a 1D array so this should solve it
public static int[] findStudenTot(int[][] x){
int tot[]=new int[x.length];
for(int i=0; i<x.length; i++){
for (int j = 0; j<x[i].length; j++){
tot[i]+=x[i][j];
}
}
return tot;
}
Not certain what you want but try this. Change int[][] to int[] as the return type.
public static int[] findStudenTot(int[][] x){
int tot[]=new int[x.length];
// rest of code here.

Inputting adjacency matrix from a file

I am attempting to input the following file into my program in the form of an adjacency matrix.
16
-1,1075,716,792,1425,1369,740,802,531,383,811,2211,661,870,999,772
1075,-1,1015,1770,2403,1662,870,1858,941,1426,1437,3026,1486,211,1463,314
716,1015,-1,928,1483,646,390,1085,185,749,530,2034,1377,821,471,772
792,1770,928,-1,633,1089,1111,246,908,409,495,1447,1317,1565,672,1470
1425,2403,1483,633,-1,9999,1630,752,1432,9999,931,814,1938,2198,1016,2103
1369,1662,646,1089,9999,-1,820,1335,832,9999,605,1839,2030,1468,421,1419
740,870,390,1111,1630,820,-1,1224,360,965,690,2197,1480,750,630,705
802,1858,1085,246,752,1335,1224,-1,1021,442,737,1566,1190,1653,918,1558
531,941,185,908,1432,832,360,1021,-1,685,496,2088,1192,736,616,656
383,1426,749,409,9999,9999,965,442,685,-1,738,1858,1938,1221,926,1126
811,1437,530,495,931,605,690,737,496,738,-1,1631,1472,1232,188,1152
2211,3026,2034,1447,814,1839,2197,1566,2088,1858,1631,-1,2752,2824,1563,2744
661,1486,1377,1317,1938,2030,1480,1190,1192,1938,1472,2752,-1,1281,1660,1183
870,211,821,1565,2198,1468,750,1653,736,1221,1232,2824,1281,-1,1269,109
999,1463,471,672,1016,421,630,918,616,926,188,1563,1660,1269,-1,1220
772,314,772,1470,2103,1419,705,1558,656,1126,1152,2744,1183,109,1220,-1
However, I think I have something wrong with my logic or I'm not using the Scanner correctly. This is my code:
public class Tour
{
public static final int N = 16;
public static final int INF = Integer.MAX_VALUE;
public static void printGrid(int[][] adjMat)
{
for(int i = 0; i < 16; i++)
{
for(int j = 0; j < 16; j++)
{
if(adjMat[i][j] == INF)
System.out.printf("%5s", 0);
else
System.out.printf("%5d", adjMat[i][j]);
}
System.out.println();
}
}
public static void main(String[] args) throws IOException
{
File file = new File("american_tour.dat");
Scanner scanner = new Scanner(file);
int[][] adjMat = new int[N][N];
for(int i = 0, n = scanner.nextInt(); i < n; i++)
for(int j = 0; j < n; j++)
adjMat[i][j] = n;
scanner.close();
printGrid(adjMat);
}
}
Could someone show me how to properly input the data from the file into an adjacency matrix?
Improving Mouad's answer, using the scanner's built in support for custom delimiters:
Scanner scanner = new Scanner(file);
scanner.useDelimiter("[\\s,]+");
int N = scanner.nextInt();
int[][] adjMat = new int[N][N];
for(int i=0; i < N; i++) {
for (int j=0; j < N; j++) {
adjMat[i][j] = scanner.nextInt();
}
}
scanner.close();
As your data does not respect a specific pattern for delimiters, try this instead :
public static void main(String[] args) throws FileNotFoundException {
File file = new File("E:\\american_tour.dat");
Scanner scanner = new Scanner(file);
//N in the example equals 16
int N = scanner.nextInt();
//skip the first line
scanner.nextLine();
int[][] adjMat = new int[N][N];
for(int i = 0; i < N; i++){
String[] lines = scanner.nextLine().split(",");
for (int j=0; j<lines.length; j++) {
adjMat[i][j] = Integer.parseInt(lines[j]);
}
}
scanner.close();
}

Condensing an adjacency matrix for shortest tour

I have a program that reads a data file and inputs the data as a 16 x 16 adjacency matrix. I need to find 10 nodes of the graph out of the 16 for which there is a minimal tour. Could someone show me how to implement a traveling salesperson algorithm that accomplishes this? I know how to do it for all 16 nodes, but I don't know how to condense it to only 10.
public class Tour
{
public static final int N = 16;
public static final int INF = Integer.MAX_VALUE;
public static void printGrid(int[][] adjMat)
{
for(int i = 0; i < 16; i++)
{
for(int j = 0; j < 16; j++)
{
if(adjMat[i][j] == INF)
System.out.printf("%5s", 0);
else
System.out.printf("%5d", adjMat[i][j]);
}
System.out.println();
}
}
public static void TSP(int[][] adjMat)
{
adjMat = new int[N][N];
}
public static void main(String[] args) throws FileNotFoundException
{
File file = new File("american_tour.dat");
Scanner scanner = new Scanner(file);
scanner.useDelimiter("[\\s,]+");
int N = scanner.nextInt();
int[][] adjMat = new int[N][N];
for(int i=0; i < N; i++)
for (int j=0; j < N; j++)
adjMat[i][j] = scanner.nextInt();
printGrid(adjMat);
scanner.close();
}
}

Java Selection Sort Confusion

I don't understand what is happening in this code. Re-post from before with code included. Can someone please explain what is happening here? I understand conceptually that the list is being re-ordered one item at a time, but I just cant grasp this code.
import java.io*;
public class Example {
public static void main(String[] args) throws IOException {
int age[] = new int[10];
int i, j;
int smallest;
int temp;
String line;
BufferedReader in;
in = new BUfferedReader(new InputStreamReader(System.in));
for(i = 0; i<= 9; i++)
{
System.out.println("Enter an age: ");
line = in.readline();
age[i] = Integer.valueOf(line).intValue();
}
for(i = 0; i<= 9, i++) {
smallest = i;
for(j = 1; j<=9; j++)
{
if(age[j] < age[smallest]) {
smallest = j;
}
}
for (i = 0; i<=9; i++)
{
System.out.println(age[i]);
}
}
}
}
This looks like an implementation of the Bubble Sort. There is a wide wealth of information on the topic of this classic (and inefficient!) algorithm both on The Internet and in books on the subject of fundamental algorithms.
import java.io*;
public class Example {
public static void main(String[] args) throws IOException {
int age[] = new int[10];
int i, j;
int smallest;
int temp;
String line;
BufferedReader in;
in = new BUfferedReader(new InputStreamReader(System.in));
for(i = 0; i<= 9; i++)
{
System.out.println("Enter an age: ");
line = in.readline();
age[i] = Integer.valueOf(line).intValue();
}
for(i = 0; i<= 9, i++) {
smallest = i;
for(j = 1; j<=9; j++)// MISTAKE IS IN THIS LINE; YOU SHOULD START THE VALUE OF J FROM I+1(for(j = i+1; j<=9; j++)))
{
if(age[j] < age[smallest]) {
smallest = j;
}
}
//ALSO AFTER FINDING THE SMALLEST ELEMENT YOU HAVE TO SWAP THE SMALLEST ELEMENT WITH I ELEMENT
/*int temp=age[i];
age[i]=age[smallest];
age[smallest]=temp*/
for (i = 0; i<=9; i++)
{
System.out.println(age[i]);
}
}
}
}

Sorting arrays in method and returning to main method

Read an array of 5 integer values in the main method. Create a separate function that will sort the array and return the array back to the main method and print it there.
So far I have done:
import java.util.*;
public class arraysort {
public static void main(String[]args) {
Scanner in = new Scanner (System.in);
System.out.println("Enter 5 integers: ");
int [] x = new int [5];
for (int i=0; i<5; i++) {
x[i] = in.nextInt();
}
}
public static int sortarray(int [] value) {
int max = value[0];
for (int i = 1; i < value.length; i++) {
//I am not sure after this point
//I just did the rest
int [] y = new int [5];
Scanner in = new Scanner (System.in);
value[i] = in.nextInt();
Arrays.sort(y);
System.out.println(y);
}
}
}
or you can use this sortArray if you don't want to use built in methods:
public static int[] sortarray(int [] value)
{
for (int arrayIterator = 0; arrayIterator < value.length; arrayIterator++)
{
for(int iterator2=arrayIterator+1;iterator2<value.length;iterator2++){
if(value[iterator2]<value[arrayIterator]){
int temp=value[arrayIterator];
value[arrayIterator]=value[iterator2];
value[iterator2]=temp;
}
}
} return value;
}
Code sponsored by Arrays.toString() and Arrays.sort():
public class ArraySort
{
public static void main(String args[])
{
int t[] = {3, 5, 2}; // your reading part is fine, so I will skip it
sortArrayWithoutCheating(t);
System.out.println(Arrays.toString(t));
}
public static void sortArrayWithoutCheating(int[] t)
{
// simplest O(n^2) sort
for (int i = 0; i < t.length; i++)
{
for (int j = 0; j < i; j++)
{
if (t[i] < t[j])
{
// swap t[i] and t[j]
int temp = t[i];
t[i] = t[j];
t[j] = temp;
}
}
}
}
public static void sortArray(int[] t)
{
Arrays.sort(t);
}
}
Output:
[2, 3, 5]

Categories

Resources