Submission details
Task:Kirjat
Sender:hello_world
Submission time:2015-10-01 10:37:53 +0300
Language:C++
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
Test results
testverdicttimegroup
#1--1details
#2--1details
#3--1details
#4--1details
#5--1details
#6--1details
#7--1details
#8--1details
#9--1details
#10--1details
#11--1details
#12--1details
#13--1details
#14--1details
#15--1details
#16--1details
#17--1details
#18ACCEPTED0.06 s1details
#19ACCEPTED0.06 s1details
#20--1details
#21--2details
#22--2details
#23--2details
#24--2details
#25--2details
#26--2details
#27--2details
#28--2details
#29--2details
#30--2details
#31--2details
#32--2details
#33--2details
#34--2details
#35--2details
#36ACCEPTED0.05 s2details
#37ACCEPTED0.05 s2details
#38ACCEPTED0.05 s2details
#39ACCEPTED0.06 s2details
#40ACCEPTED0.05 s2details
#41--3details
#42--3details
#43--3details
#44--3details
#45--3details
#46--3details
#47--3details
#48--3details
#49--3details
#50--3details
#51--3details
#52--3details
#53--3details
#54--3details
#55ACCEPTED0.07 s3details
#56ACCEPTED0.12 s3details
#57ACCEPTED0.11 s3details
#58ACCEPTED0.13 s3details
#59ACCEPTED0.13 s3details
#60ACCEPTED0.13 s3details

Code

#include <stdio.h>
#include <iostream>
#include <string>
#include <string.h>
#include <stdlib.h>

using namespace std;

struct List{
	unsigned int x;
	List *next;
	List *last;
};

void freeMemory(List *list);
void printMemory(List *list);

int main(void){
	
	List *a, *b, *a2;
	a = NULL;
	a2 = NULL;
	b = NULL;
	
	List *ap, *bp, *ap2;
	unsigned int count;
	
	cin >> count;
	
	unsigned int countMinusOne = count - 1;
	unsigned int c = count;
	
	a = (struct List *)malloc(sizeof(struct List));
	a->last = NULL; a->next = NULL;
	ap = a;
	while(c--){
		cin >> ap->x;
		//cout << c << " : " << ap->x << endl;
		if (c){
			ap->next = (struct List *)malloc(sizeof(struct List));
			(ap->next)->last = ap;
			(ap->next)->next = NULL;
			ap = ap->next;
		}
	}
	
	a2 = (struct List *)malloc(sizeof(struct List));
	a2->last = NULL; a2->next = NULL;
	ap2 = a2;
	c = count;
	while(c--){
		cin >> ap2->x;
		//cout << c << " : " << ap->x << endl;
		if (c){
			ap2->next = (struct List *)malloc(sizeof(struct List));
			(ap2->next)->last = ap2;
			(ap2->next)->next = NULL;
			ap2 = ap2->next;
		}
	}  
	
	//swapataan a ja a2
	
	List *x1 = a;	//aloitetaan lista a alusta, s?ilytet??n ap kuitenkin, nimet??n uusi pointer x1
	List *x2 = a2;
	unsigned int swap_c[2];
	
	
	c = count;
	unsigned int temp;
	
	//tarkistetaan kahden viimeisen alkion muuttujat, eli että onko järjeä swapata, ja sen jälkeen testataan voidaanko swapata
	
	bool tryToSwap[2] = {false, false};
	bool swap[2] = {0, 0};					//tästä katsotaan myöhemmin, mitkä swapattiin
	
	if (ap->x >= countMinusOne || ap2->x >= countMinusOne) tryToSwap[1] = true;
	else if ((ap->last)->x >= countMinusOne || (ap2->last)->x >= countMinusOne) tryToSwap[0] = true;
	
	if (tryToSwap[0] || tryToSwap[1]){
		while(1);
		unsigned int trySwap;	//katsotaan swapataanko kaksi, vai yksi(ja kumpi)
	
		if (tryToSwap[0] && tryToSwap[1]) trySwap = 2;
		else if (tryToSwap[1]) trySwap = 1;
		else trySwap = 0;
	
		while (c--) {	//selataan a:ta ja a:sta eteenpäin, kunnes kumpikin on alle countMinusOne
			if (x1->x < countMinusOne && x2->x < countMinusOne) {
				
				if (!swap[1] && trySwap != 0){	//swap num 1:stä ei ole tehty, eli viimeistä ei ole swapattu
					
					temp = x1->x;
					x1->x = ap->x;
					ap->x = temp;
					
					temp = x2->x;
					x2->x = ap2->x;
					ap2->x = temp;
					
					swap_c[1] = c;
						
					//tallennetaan c, jotta voidaan sen avulla tehdä takaisin swappaus
					
					swap[1] = 1;
					
					if (trySwap == 1) break;
					
				} else {	//swap[0], tryswap 2, 0
					//while(1);
					temp = x1->x;
					x1->x = (ap->last)->x;
					(ap->last)->x = temp;
					
					temp = x2->x;
					x2->x = (ap2->last)->x;
					(ap2->last)->x = temp;
					
					swap_c[0] = c;
					swap[0] = 1;
					
					break;	//molemmat swap:it on tehty, voidaan poistua
				}
			}
			
			x1 = x1->next;
			x2 = x2->next;
		}
	}
	
	
	
	
	
	c = count;
	//unsigned int x2;		//b-lukujono, tyyli1 hankalampi swap
	unsigned int m[2] = {0, 0};		//memory, size 2, voidaan k?ytt?? lukua 0 tyhj?n?, t?ytet??n indeksist? nolla eteenp?in
	unsigned int cur = 1;	//menneill??n oleva luku, jota halutaan kokeilla laittaa uuteen lukujonoon
	
	b = (struct List *)malloc(sizeof(struct List));	//b
	b->last = NULL; b->next = NULL;
	bp = b;
	x1 = a;	//aloitetaan lista a alusta, s?ilytet??n ap kuitenkin, nimet??n uusi pointer x1
	x2 = a2;
	while(c--){
		
		//otetaan muistista 0:sta jos mahdollista, jos ei otetaan 1:st? jos mahdollista
		//muutoin nostetaan cur:ia yhdell?:
			//jos se voidaan laittaa, laitetaan se
			//jos sit? ei voida laittaa, lis?t??n muistiin, jos muisti 0 on k?ytetty lis?t??n paikalle 1, muutoin paikalle nolla
		//if (m[1] && m[0] > m[1]) ei pidä koskaan paikaansa
		if (m[0]) {	//m[0] on aina pienempi kuin m[1]
			if (m[0] != x2->x && m[0] != x1->x){					//mem 0 voidaanko k?ytt??, tarkistetaan t?m? ensin, sill? t?m? on pienempi kuin m1
				bp->x = m[0];
				//cout << "yea1";
				if (m[1]) {
					m[0] = m[1];		//m[1] on nyt v?kistenkin pienin
					m[1] = 0;//cur;			//joten vaihdetaan se m[0]:n tilalle ja tallennetaan uusi cur m[1]:seen
				} else m[0] = 0;
			} else if (m[1] && (m[1] != x2->x && m[1] != x1->x)) {	//mem 1 voidaanko k?ytt??
				bp->x = m[1];
				
				//cout << "yea2(" << m[1] << " " << x2->x << " " << x1->x << ")";
				m[1] = 0;//cur;		//m[0] pysyy edelleen pienimp?n?, vaihdetaan cur m[1]:sen tilalle
				//verrataan cur:ia m[0]:aan ja m[1:seen]
			} 	//nyt muistissa on joko m[0] tai m[0] ja m[1], sill? cur:ia ei voitu k?ytt?? ja se vaihdettiin toisen m:n tilalle
			else {	//muistia ei voitu k?ytt??, siirryt??n valintaan(goto)
				//cur++;
				//cout << "goto";
				goto valinta;
			} //cout << "yea";
			//cout << "muistiin" << cur << " ";
		} else {	//jos muistissa ei ole lukuja, voidaanko cur k?ytt??
			while(cur == x2->x || cur == x1->x) {	//cur:ia ei voida laittaa, laitetaan se muistiin, sill? muisti on tyhj?
				if (!m[0]) m[0] = cur;	//jos nollapaikkaa ei ole t?ytetty t?ytet??n se
				else m[1] = cur;		//muutoin t?ytet??n paikka 1
				cur++;		//aina lis?t??n v?h.1 kun tullaan t?nne, jotta voidaan tarkistaa mem uudelleen(m)
			}	//nyt voitaisiin laittaa, nyt voidaan laittaa, muistista ei voida edelleenk??n laittaa, joten laitetaan cur
			valinta:
			bp->x = cur; 
			cur++;
			//cout << "valitaan" << cur << " ";
		}
		//cout << bp->x;
		if (c){
			bp->next = (struct List *)malloc(sizeof(struct List));
			(bp->next)->last = bp;
			(bp->next)->next = NULL;
			bp = bp->next;
		}
		x1 = x1->next;
		x2 = x2->next;
	}
	//cout << "asdf";
	
	//kaikki on nyt muuten järjestyksessä, paitsi että pitää vielä swapatut alkiot vaihtaa takaisin omille paikoilleen
	//cout << "asdfasdf";
	//x1_swap[1]->x ^= (ap->last)->x ^= x1_swap[1]->x ^= (ap->last)->x;
	//cout << "\nasdfasdf";
	//x1_swap[0]->x ^= ap->x ^= x1_swap[0]->x ^= ap->x;
	
	//x2_swap[1]->x ^= (ap2->last)->x ^= x2_swap[1]->x ^= (ap2->last)->x;
	//x2_swap[0]->x ^= ap2->x ^= x2_swap[0]->x ^= ap2->x;
	//cout << "\nasdfasdf";
	
	List *bx = b;
	
	//cout << endl;cout << endl;
	//cout << endl;printMemory(b);
	c = count;
	x1 = a;
	x2 = a2;
	bx = b;
	
	/*cout << endl;
	cout << endl;printMemory(b);
	cout << endl;printMemory(a);
	cout << endl;printMemory(a2);
	cout << endl;
	*/
	
	while (x1->next != NULL){
		if (bx->x == x1->x || bx->x == x2->x){
			//cout << bx->x << " " << x1->x << " " << x2->x << endl;
			while(1);	//menee ainakin joskus tänne, eli jokin on pielessä
		}
		x1 = x1->next;
		x2 = x2->next;
		bx = bx->next;
	}
	
	c = count;
	bx = b;
	
	if (swap[0] || swap[1]) {
		
		while(c--){	//voidaan tehdä c:llä, koska nämä ovat viimeistään c= count - 3 ja c = .. 4
			if (swap[1] && swap_c[1] == c){
				
				//cout << endl << " c: " << c << " " << b->x << " -> " << bp->x << endl;
				
				temp = bx->x;
				bx->x = bp->x;
				bp->x = temp;
				
				
				//cout << endl;printMemory(b);
				
				if (!swap[0]) break;
				
			} else if (swap[0] && swap_c[0] == c){
				
				//List *temp_list = bp->last;
				
				//cout << endl << " c2: " << c << " " << b->x << " -> " << (bp->last)->x << endl;
				
				temp = bx->x;
				bx->x = (bp->last)->x;
				(bp->last)->x = temp;
				
				
				//cout << endl;printMemory(b);
				break; 
			} 
			bx = bx->next;
		}
	}
	
	//tarkistetaan meneekö swap oikein
	
	
	
	cout << endl;printMemory(b);
	//cout << endl;printMemory(a);
	//cout << endl;printMemory(a2);
	
	//käydään muodostettu lista läpi, katsotaan onko se oikein rakennettu, jos ei ole tehdään runtime-error
	
	
	
		
	freeMemory(ap2);
	freeMemory(bp);
	freeMemory(ap);
	
	
	return 0;
	
	
	
}

void printMemory(List *list){
	List *l = list;
	while (l->next != NULL){
		cout << l->x << " ";
		l = l->next;
	}
	cout << l->x << " ";
}

void freeMemory(List *list){
	
	/*while (list->next != NULL){
		list = list->next;
	}*/
	List *l;
	do {
		l = list;
		list = list->last;
		free(l);
	} while(list->last != NULL);
}

Test details

Test 1

Group: 1

Verdict:

input
3
2 1 3
3 2 1

correct output
1 3 2 

user output
(empty)

Test 2

Group: 1

Verdict:

input
4
2 1 4 3
1 4 3 2

correct output
4 3 2 1 

user output
(empty)

Test 3

Group: 1

Verdict:

input
4
4 3 2 1
3 1 4 2

correct output
1 2 3 4 

user output
(empty)

Test 4

Group: 1

Verdict:

input
4
3 4 2 1
2 3 1 4

correct output
1 2 4 3 

user output
(empty)

Test 5

Group: 1

Verdict:

input
4
4 1 3 2
2 3 1 4

correct output
1 4 2 3 

user output
(empty)

Test 6

Group: 1

Verdict:

input
5
5 1 3 2 4
3 4 2 1 5

correct output
2 3 4 5 1 

user output
(empty)

Test 7

Group: 1

Verdict:

input
5
4 2 3 5 1
3 5 2 1 4

correct output
1 4 5 2 3 

user output
(empty)

Test 8

Group: 1

Verdict:

input
5
1 4 3 2 5
4 3 1 5 2

correct output
3 2 5 1 4 

user output
(empty)

Test 9

Group: 1

Verdict:

input
5
5 3 2 1 4
4 2 1 3 5

correct output
1 4 5 2 3 

user output
(empty)

Test 10

Group: 1

Verdict:

input
5
4 3 5 1 2
5 1 3 2 4

correct output
2 5 1 4 3 

user output
(empty)

Test 11

Group: 1

Verdict:

input
5
5 1 3 2 4
2 5 4 3 1

correct output
3 4 2 1 5 

user output
(empty)

Test 12

Group: 1

Verdict:

input
5
5 4 2 1 3
2 3 5 4 1

correct output
3 1 4 5 2 

user output
(empty)

Test 13

Group: 1

Verdict:

input
5
1 5 2 4 3
5 1 4 3 2

correct output
3 2 5 1 4 

user output
(empty)

Test 14

Group: 1

Verdict:

input
5
5 3 4 2 1
3 5 2 1 4

correct output
1 2 3 4 5 

user output
(empty)

Test 15

Group: 1

Verdict:

input
5
4 5 3 2 1
3 2 1 4 5

correct output
5 3 2 1 4 

user output
(empty)

Test 16

Group: 1

Verdict:

input
10
4 9 1 10 6 8 7 2 3 5
8 5 7 3 1 6 4 10 2 9

correct output
3 1 9 2 4 7 8 6 5 10 

user output
(empty)

Test 17

Group: 1

Verdict:

input
10
6 10 8 2 3 5 7 1 4 9
4 1 2 10 5 6 9 3 8 7

correct output
5 7 1 3 9 2 4 10 6 8 

user output
(empty)

Test 18

Group: 1

Verdict: ACCEPTED

input
10
8 2 9 10 1 6 4 7 3 5
3 8 4 5 6 7 9 10 1 2

correct output
1 6 8 9 5 4 10 3 2 7 

user output

1 3 2 4 5 8 6 9 7 10 

Test 19

Group: 1

Verdict: ACCEPTED

input
10
6 3 8 9 5 7 4 10 1 2
8 9 10 1 3 6 2 7 4 5

correct output
5 1 6 2 8 10 7 3 9 4 

user output

1 2 3 4 6 5 7 8 9 10 

Test 20

Group: 1

Verdict:

input
10
8 3 5 6 2 10 4 7 1 9
4 7 8 10 5 2 6 1 9 3

correct output
1 5 7 3 10 6 9 4 2 8 

user output
(empty)

Test 21

Group: 2

Verdict:

input
3
3 2 1
1 3 2

correct output
2 1 3 

user output
(empty)

Test 22

Group: 2

Verdict:

input
4
2 3 1 4
1 4 3 2

correct output
3 2 4 1 

user output
(empty)

Test 23

Group: 2

Verdict:

input
4
2 4 3 1
4 1 2 3

correct output
3 2 1 4 

user output
(empty)

Test 24

Group: 2

Verdict:

input
4
4 1 2 3
1 3 4 2

correct output
3 2 1 4 

user output
(empty)

Test 25

Group: 2

Verdict:

input
4
2 1 3 4
4 3 2 1

correct output
3 4 1 2 

user output
(empty)

Test 26

Group: 2

Verdict:

input
5
2 5 3 1 4
4 2 1 5 3

correct output
5 4 2 3 1 

user output
(empty)

Test 27

Group: 2

Verdict:

input
5
1 4 3 2 5
5 2 4 1 3

correct output
4 5 2 3 1 

user output
(empty)

Test 28

Group: 2

Verdict:

input
5
1 4 2 3 5
2 3 1 5 4

correct output
4 5 3 1 2 

user output
(empty)

Test 29

Group: 2

Verdict:

input
5
4 5 2 3 1
5 3 1 2 4

correct output
1 2 3 4 5 

user output
(empty)

Test 30

Group: 2

Verdict:

input
5
3 2 1 5 4
5 4 3 1 2

correct output
4 5 2 3 1 

user output
(empty)

Test 31

Group: 2

Verdict:

input
5
5 3 1 2 4
3 2 4 1 5

correct output
4 5 2 3 1 

user output
(empty)

Test 32

Group: 2

Verdict:

input
5
5 4 1 2 3
1 5 3 4 2

correct output
2 3 4 5 1 

user output
(empty)

Test 33

Group: 2

Verdict:

input
5
1 4 5 3 2
3 5 2 4 1

correct output
5 1 3 2 4 

user output
(empty)

Test 34

Group: 2

Verdict:

input
5
3 4 2 1 5
1 5 3 4 2

correct output
2 3 4 5 1 

user output
(empty)

Test 35

Group: 2

Verdict:

input
5
2 3 1 5 4
5 4 2 1 3

correct output
1 2 3 4 5 

user output
(empty)

Test 36

Group: 2

Verdict: ACCEPTED

input
1000
63 72 78 267 740 551 517 698 6...

correct output
26 926 267 321 385 444 968 690...

user output

1 2 3 4 5 6 7 8 9 10 11 12 13 ...

Test 37

Group: 2

Verdict: ACCEPTED

input
1000
954 273 839 263 331 161 938 51...

correct output
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

user output

1 2 3 4 5 6 7 8 9 10 11 12 13 ...

Test 38

Group: 2

Verdict: ACCEPTED

input
1000
740 142 781 837 759 392 582 14...

correct output
111 291 702 70 561 469 707 897...

user output

1 2 3 4 5 6 7 8 9 10 11 12 13 ...

Test 39

Group: 2

Verdict: ACCEPTED

input
1000
960 550 210 529 691 277 63 975...

correct output
716 604 535 519 27 204 574 592...

user output

1 2 3 4 5 6 7 8 9 10 11 12 13 ...

Test 40

Group: 2

Verdict: ACCEPTED

input
1000
371 772 197 202 504 931 4 46 6...

correct output
26 926 267 321 385 444 968 690...

user output

1 2 3 4 5 6 7 8 9 10 11 12 13 ...

Test 41

Group: 3

Verdict:

input
3
1 2 3
3 1 2

correct output
2 3 1 

user output
(empty)

Test 42

Group: 3

Verdict:

input
4
4 2 3 1
2 3 1 4

correct output
1 4 2 3 

user output
(empty)

Test 43

Group: 3

Verdict:

input
4
2 1 4 3
4 3 1 2

correct output
1 2 3 4 

user output
(empty)

Test 44

Group: 3

Verdict:

input
4
1 4 2 3
2 3 4 1

correct output
3 2 1 4 

user output
(empty)

Test 45

Group: 3

Verdict:

input
4
2 1 4 3
1 3 2 4

correct output
4 2 3 1 

user output
(empty)

Test 46

Group: 3

Verdict:

input
5
3 1 5 2 4
5 4 2 1 3

correct output
1 2 3 4 5 

user output
(empty)

Test 47

Group: 3

Verdict:

input
5
2 1 5 3 4
5 3 2 4 1

correct output
4 5 3 1 2 

user output
(empty)

Test 48

Group: 3

Verdict:

input
5
5 1 4 3 2
3 5 1 2 4

correct output
1 2 3 4 5 

user output
(empty)

Test 49

Group: 3

Verdict:

input
5
2 4 1 3 5
3 5 4 1 2

correct output
5 1 3 2 4 

user output
(empty)

Test 50

Group: 3

Verdict:

input
5
5 2 3 4 1
2 1 4 3 5

correct output
1 4 5 2 3 

user output
(empty)

Test 51

Group: 3

Verdict:

input
5
4 1 5 3 2
2 4 1 5 3

correct output
1 2 3 4 5 

user output
(empty)

Test 52

Group: 3

Verdict:

input
5
3 1 5 2 4
1 4 2 3 5

correct output
5 2 1 4 3 

user output
(empty)

Test 53

Group: 3

Verdict:

input
5
1 4 5 3 2
4 2 3 5 1

correct output
5 3 2 1 4 

user output
(empty)

Test 54

Group: 3

Verdict:

input
5
1 4 5 2 3
4 2 3 1 5

correct output
2 3 4 5 1 

user output
(empty)

Test 55

Group: 3

Verdict: ACCEPTED

input
5
4 5 3 2 1
5 3 4 1 2

correct output
1 2 5 3 4 

user output

1 2 5 3 4 

Test 56

Group: 3

Verdict: ACCEPTED

input
100000
74620 99226 537 63830 13777 69...

correct output
44158 25720 84658 90057 99607 ...

user output

1 2 3 4 5 6 7 8 9 10 11 12 13 ...

Test 57

Group: 3

Verdict: ACCEPTED

input
100000
67665 19864 90761 58104 38796 ...

correct output
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

user output

1 2 3 4 5 6 7 8 9 10 11 12 13 ...

Test 58

Group: 3

Verdict: ACCEPTED

input
100000
63021 24161 40379 69157 89616 ...

correct output
4913 70683 13897 99969 66725 3...

user output

1 2 3 4 5 6 7 8 9 10 11 12 13 ...

Test 59

Group: 3

Verdict: ACCEPTED

input
100000
31500 70052 90949 56812 73871 ...

correct output
47064 17335 15460 80797 56435 ...

user output

1 2 3 4 5 6 7 8 9 10 11 12 13 ...

Test 60

Group: 3

Verdict: ACCEPTED

input
100000
39127 4446 57817 67459 53741 8...

correct output
96591 75698 82505 59416 72144 ...

user output

1 2 3 4 5 6 7 8 9 10 11 12 13 ...