Random Permutation and counting how many positions a[i] < a[i+1] in permutation
The first line contains the number of marathons t < 100
. Each marathon is specified by three lines. The first line contains the number of runners 1 < n <= 40000
. The second line is a permutation of the starting numbers 1,...,n
which represents the order in which the runners passed the starting line. Finally, the third line is a permutation which represents the finishing order. For each marathon output one line which contains the minimal number of overtakings that have happend during the race.
So for example
I really don't know how I can create a random permutation 1 < n
and how to find out then how many overtakings took place. For the latter I would check how many numbers are bigger than the next, i.e. if 4 > 3
then I increase an int overtaking++;
import java.util.Scanner;
public class MarathonMovement {
public static void main(String args) {
Scanner NoM = new Scanner(System.in); // Number of marathons
int t = NoM.nextInt();
Scanner NoR = new Scanner(System.in); // First line: Number of runners
int n = NoR.nextInt();
// Second line: Permutation of starting numbers representing the order in which the runners passed the starting line
// Third line: Permutation which represents finishing order
int overtakings = 0;
for (int i = 0; i < n; i++){
if {
// logic
overtakings++;
}
}
for(int x = 0; x < t; x++){
System.out.println("at least" + overtakings + " overtaking(s)");
}
}
}
java permutation
add a comment |
The first line contains the number of marathons t < 100
. Each marathon is specified by three lines. The first line contains the number of runners 1 < n <= 40000
. The second line is a permutation of the starting numbers 1,...,n
which represents the order in which the runners passed the starting line. Finally, the third line is a permutation which represents the finishing order. For each marathon output one line which contains the minimal number of overtakings that have happend during the race.
So for example
I really don't know how I can create a random permutation 1 < n
and how to find out then how many overtakings took place. For the latter I would check how many numbers are bigger than the next, i.e. if 4 > 3
then I increase an int overtaking++;
import java.util.Scanner;
public class MarathonMovement {
public static void main(String args) {
Scanner NoM = new Scanner(System.in); // Number of marathons
int t = NoM.nextInt();
Scanner NoR = new Scanner(System.in); // First line: Number of runners
int n = NoR.nextInt();
// Second line: Permutation of starting numbers representing the order in which the runners passed the starting line
// Third line: Permutation which represents finishing order
int overtakings = 0;
for (int i = 0; i < n; i++){
if {
// logic
overtakings++;
}
}
for(int x = 0; x < t; x++){
System.out.println("at least" + overtakings + " overtaking(s)");
}
}
}
java permutation
I wonder why 3rd example's answer is 21?
– oleg.cherednik
Nov 12 '18 at 7:31
If you want to generate a random number between 1...n then you can use int num = (int) (n * Math.random()). Not exactly sure how you expect to make a logical series of overtakings from a pseudo-random sequence of numbers though
– PumpkinBreath
Nov 12 '18 at 7:38
1
please post text rather than an image
– Maurice Perry
Nov 12 '18 at 7:48
@oleg.cherednik Because 7 is also greater than 5 etc. and 4 is greater than 2, 1 ...
– JavaTeachMe2018
Nov 12 '18 at 8:07
add a comment |
The first line contains the number of marathons t < 100
. Each marathon is specified by three lines. The first line contains the number of runners 1 < n <= 40000
. The second line is a permutation of the starting numbers 1,...,n
which represents the order in which the runners passed the starting line. Finally, the third line is a permutation which represents the finishing order. For each marathon output one line which contains the minimal number of overtakings that have happend during the race.
So for example
I really don't know how I can create a random permutation 1 < n
and how to find out then how many overtakings took place. For the latter I would check how many numbers are bigger than the next, i.e. if 4 > 3
then I increase an int overtaking++;
import java.util.Scanner;
public class MarathonMovement {
public static void main(String args) {
Scanner NoM = new Scanner(System.in); // Number of marathons
int t = NoM.nextInt();
Scanner NoR = new Scanner(System.in); // First line: Number of runners
int n = NoR.nextInt();
// Second line: Permutation of starting numbers representing the order in which the runners passed the starting line
// Third line: Permutation which represents finishing order
int overtakings = 0;
for (int i = 0; i < n; i++){
if {
// logic
overtakings++;
}
}
for(int x = 0; x < t; x++){
System.out.println("at least" + overtakings + " overtaking(s)");
}
}
}
java permutation
The first line contains the number of marathons t < 100
. Each marathon is specified by three lines. The first line contains the number of runners 1 < n <= 40000
. The second line is a permutation of the starting numbers 1,...,n
which represents the order in which the runners passed the starting line. Finally, the third line is a permutation which represents the finishing order. For each marathon output one line which contains the minimal number of overtakings that have happend during the race.
So for example
I really don't know how I can create a random permutation 1 < n
and how to find out then how many overtakings took place. For the latter I would check how many numbers are bigger than the next, i.e. if 4 > 3
then I increase an int overtaking++;
import java.util.Scanner;
public class MarathonMovement {
public static void main(String args) {
Scanner NoM = new Scanner(System.in); // Number of marathons
int t = NoM.nextInt();
Scanner NoR = new Scanner(System.in); // First line: Number of runners
int n = NoR.nextInt();
// Second line: Permutation of starting numbers representing the order in which the runners passed the starting line
// Third line: Permutation which represents finishing order
int overtakings = 0;
for (int i = 0; i < n; i++){
if {
// logic
overtakings++;
}
}
for(int x = 0; x < t; x++){
System.out.println("at least" + overtakings + " overtaking(s)");
}
}
}
java permutation
java permutation
edited Nov 12 '18 at 7:34
asked Nov 12 '18 at 7:24
JavaTeachMe2018
1407
1407
I wonder why 3rd example's answer is 21?
– oleg.cherednik
Nov 12 '18 at 7:31
If you want to generate a random number between 1...n then you can use int num = (int) (n * Math.random()). Not exactly sure how you expect to make a logical series of overtakings from a pseudo-random sequence of numbers though
– PumpkinBreath
Nov 12 '18 at 7:38
1
please post text rather than an image
– Maurice Perry
Nov 12 '18 at 7:48
@oleg.cherednik Because 7 is also greater than 5 etc. and 4 is greater than 2, 1 ...
– JavaTeachMe2018
Nov 12 '18 at 8:07
add a comment |
I wonder why 3rd example's answer is 21?
– oleg.cherednik
Nov 12 '18 at 7:31
If you want to generate a random number between 1...n then you can use int num = (int) (n * Math.random()). Not exactly sure how you expect to make a logical series of overtakings from a pseudo-random sequence of numbers though
– PumpkinBreath
Nov 12 '18 at 7:38
1
please post text rather than an image
– Maurice Perry
Nov 12 '18 at 7:48
@oleg.cherednik Because 7 is also greater than 5 etc. and 4 is greater than 2, 1 ...
– JavaTeachMe2018
Nov 12 '18 at 8:07
I wonder why 3rd example's answer is 21?
– oleg.cherednik
Nov 12 '18 at 7:31
I wonder why 3rd example's answer is 21?
– oleg.cherednik
Nov 12 '18 at 7:31
If you want to generate a random number between 1...n then you can use int num = (int) (n * Math.random()). Not exactly sure how you expect to make a logical series of overtakings from a pseudo-random sequence of numbers though
– PumpkinBreath
Nov 12 '18 at 7:38
If you want to generate a random number between 1...n then you can use int num = (int) (n * Math.random()). Not exactly sure how you expect to make a logical series of overtakings from a pseudo-random sequence of numbers though
– PumpkinBreath
Nov 12 '18 at 7:38
1
1
please post text rather than an image
– Maurice Perry
Nov 12 '18 at 7:48
please post text rather than an image
– Maurice Perry
Nov 12 '18 at 7:48
@oleg.cherednik Because 7 is also greater than 5 etc. and 4 is greater than 2, 1 ...
– JavaTeachMe2018
Nov 12 '18 at 8:07
@oleg.cherednik Because 7 is also greater than 5 etc. and 4 is greater than 2, 1 ...
– JavaTeachMe2018
Nov 12 '18 at 8:07
add a comment |
2 Answers
2
active
oldest
votes
For the first part of your question (generate a random permutation), you can use the method Collections.shuffle:
List<Integer> start = new ArrayList<>();
for (int i = 0; i < n; ++i) {
start.add(i + 1);
}
List<Integer> finish = new ArrayList<>(start);
Collections.shuffle(finish);
To count the overtakings:
int overtakings = 0;
for (int i = 1; i < n; ++i) {
if (finish.get(i) < finish.get(i-1)) {
++overtakings;
}
}
add a comment |
If I understand this task correctly, then this is my solution:
public static void main(String... args) {
try (Scanner scan = new Scanner(System.in)) {
int t = scan.nextInt();
for (int i = 0; i < t; i++) {
int n = scan.nextInt();
int arr = new int[n];
Set<String> uniqueOvertaking = new HashSet<>();
for (int j = 0; j < n; j++)
arr[j] = scan.nextInt();
for (int j = 0; j < n; j++) {
int val = scan.nextInt();
if (arr[j] != val)
uniqueOvertaking.add(Math.min(arr[j], val) + "->" + Math.max(arr[j], val));
}
int res = uniqueOvertaking.size() == n ? uniqueOvertaking.size() - 1 : uniqueOvertaking.size();
System.out.println("at least " + res + " overtaking(s)");
}
}
}
Output:
at least 1 overtaking(s)
at least 5 overtaking(s)
at least 3 overtaking(s)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53257511%2frandom-permutation-and-counting-how-many-positions-ai-ai1-in-permutation%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
For the first part of your question (generate a random permutation), you can use the method Collections.shuffle:
List<Integer> start = new ArrayList<>();
for (int i = 0; i < n; ++i) {
start.add(i + 1);
}
List<Integer> finish = new ArrayList<>(start);
Collections.shuffle(finish);
To count the overtakings:
int overtakings = 0;
for (int i = 1; i < n; ++i) {
if (finish.get(i) < finish.get(i-1)) {
++overtakings;
}
}
add a comment |
For the first part of your question (generate a random permutation), you can use the method Collections.shuffle:
List<Integer> start = new ArrayList<>();
for (int i = 0; i < n; ++i) {
start.add(i + 1);
}
List<Integer> finish = new ArrayList<>(start);
Collections.shuffle(finish);
To count the overtakings:
int overtakings = 0;
for (int i = 1; i < n; ++i) {
if (finish.get(i) < finish.get(i-1)) {
++overtakings;
}
}
add a comment |
For the first part of your question (generate a random permutation), you can use the method Collections.shuffle:
List<Integer> start = new ArrayList<>();
for (int i = 0; i < n; ++i) {
start.add(i + 1);
}
List<Integer> finish = new ArrayList<>(start);
Collections.shuffle(finish);
To count the overtakings:
int overtakings = 0;
for (int i = 1; i < n; ++i) {
if (finish.get(i) < finish.get(i-1)) {
++overtakings;
}
}
For the first part of your question (generate a random permutation), you can use the method Collections.shuffle:
List<Integer> start = new ArrayList<>();
for (int i = 0; i < n; ++i) {
start.add(i + 1);
}
List<Integer> finish = new ArrayList<>(start);
Collections.shuffle(finish);
To count the overtakings:
int overtakings = 0;
for (int i = 1; i < n; ++i) {
if (finish.get(i) < finish.get(i-1)) {
++overtakings;
}
}
answered Nov 12 '18 at 8:06
Maurice Perry
4,6822415
4,6822415
add a comment |
add a comment |
If I understand this task correctly, then this is my solution:
public static void main(String... args) {
try (Scanner scan = new Scanner(System.in)) {
int t = scan.nextInt();
for (int i = 0; i < t; i++) {
int n = scan.nextInt();
int arr = new int[n];
Set<String> uniqueOvertaking = new HashSet<>();
for (int j = 0; j < n; j++)
arr[j] = scan.nextInt();
for (int j = 0; j < n; j++) {
int val = scan.nextInt();
if (arr[j] != val)
uniqueOvertaking.add(Math.min(arr[j], val) + "->" + Math.max(arr[j], val));
}
int res = uniqueOvertaking.size() == n ? uniqueOvertaking.size() - 1 : uniqueOvertaking.size();
System.out.println("at least " + res + " overtaking(s)");
}
}
}
Output:
at least 1 overtaking(s)
at least 5 overtaking(s)
at least 3 overtaking(s)
add a comment |
If I understand this task correctly, then this is my solution:
public static void main(String... args) {
try (Scanner scan = new Scanner(System.in)) {
int t = scan.nextInt();
for (int i = 0; i < t; i++) {
int n = scan.nextInt();
int arr = new int[n];
Set<String> uniqueOvertaking = new HashSet<>();
for (int j = 0; j < n; j++)
arr[j] = scan.nextInt();
for (int j = 0; j < n; j++) {
int val = scan.nextInt();
if (arr[j] != val)
uniqueOvertaking.add(Math.min(arr[j], val) + "->" + Math.max(arr[j], val));
}
int res = uniqueOvertaking.size() == n ? uniqueOvertaking.size() - 1 : uniqueOvertaking.size();
System.out.println("at least " + res + " overtaking(s)");
}
}
}
Output:
at least 1 overtaking(s)
at least 5 overtaking(s)
at least 3 overtaking(s)
add a comment |
If I understand this task correctly, then this is my solution:
public static void main(String... args) {
try (Scanner scan = new Scanner(System.in)) {
int t = scan.nextInt();
for (int i = 0; i < t; i++) {
int n = scan.nextInt();
int arr = new int[n];
Set<String> uniqueOvertaking = new HashSet<>();
for (int j = 0; j < n; j++)
arr[j] = scan.nextInt();
for (int j = 0; j < n; j++) {
int val = scan.nextInt();
if (arr[j] != val)
uniqueOvertaking.add(Math.min(arr[j], val) + "->" + Math.max(arr[j], val));
}
int res = uniqueOvertaking.size() == n ? uniqueOvertaking.size() - 1 : uniqueOvertaking.size();
System.out.println("at least " + res + " overtaking(s)");
}
}
}
Output:
at least 1 overtaking(s)
at least 5 overtaking(s)
at least 3 overtaking(s)
If I understand this task correctly, then this is my solution:
public static void main(String... args) {
try (Scanner scan = new Scanner(System.in)) {
int t = scan.nextInt();
for (int i = 0; i < t; i++) {
int n = scan.nextInt();
int arr = new int[n];
Set<String> uniqueOvertaking = new HashSet<>();
for (int j = 0; j < n; j++)
arr[j] = scan.nextInt();
for (int j = 0; j < n; j++) {
int val = scan.nextInt();
if (arr[j] != val)
uniqueOvertaking.add(Math.min(arr[j], val) + "->" + Math.max(arr[j], val));
}
int res = uniqueOvertaking.size() == n ? uniqueOvertaking.size() - 1 : uniqueOvertaking.size();
System.out.println("at least " + res + " overtaking(s)");
}
}
}
Output:
at least 1 overtaking(s)
at least 5 overtaking(s)
at least 3 overtaking(s)
edited Nov 12 '18 at 7:51
answered Nov 12 '18 at 7:46
oleg.cherednik
5,53421017
5,53421017
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53257511%2frandom-permutation-and-counting-how-many-positions-ai-ai1-in-permutation%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I wonder why 3rd example's answer is 21?
– oleg.cherednik
Nov 12 '18 at 7:31
If you want to generate a random number between 1...n then you can use int num = (int) (n * Math.random()). Not exactly sure how you expect to make a logical series of overtakings from a pseudo-random sequence of numbers though
– PumpkinBreath
Nov 12 '18 at 7:38
1
please post text rather than an image
– Maurice Perry
Nov 12 '18 at 7:48
@oleg.cherednik Because 7 is also greater than 5 etc. and 4 is greater than 2, 1 ...
– JavaTeachMe2018
Nov 12 '18 at 8:07