import java.util.Scanner; public class Test25 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long[] arrs = new long[n]; long[] temp = new long[n]; for (int i = 0; i < n; i++) { arrs[i] = sc.nextLong(); } for (int i = 1; i <n ; i++) { temp[i-1] = arrs[i] - arrs[i-1]; if(temp[i-1] <=0){ System.out.println(-1); return; } } long d = helper(temp[0],temp[1]); for (int i = 2; i < n-1; i++) { d = helper(temp[i],d); } System.out.println(d); } public static long helper(long num1,long num2){ if(num2 ==0){ return num1; }else { return helper(num2,num1%num2); }}} 123456789101112131415161718192021222324252627282930313233Copy the code



The input

3 50

100 50 51

1000 1000 1000

The output

1000

instructions

If you challenge the first monster first, the first monster deals 1000 damage to the brave one, the brave one’s imperial power increases by 1, then you challenge the second monster, the brave one is not harmed because its defense is already greater than its ability to break. Then challenge the third one, the brave one takes no damage, so the total damage is 1000.

import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; public class Test26 { static class Node{ int a; int b; Node(int a,int b){ this.a = a; this.b = b; } @Override public String toString(){ return a+""+b; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int d = sc.nextInt(); int[] po = new int[n]; int[] shang = new int[n]; for (int i = 0; i < n; i++) { po[i] = sc.nextInt(); } for (int i = 0; i <n ; i++) { shang[i] = sc.nextInt(); } Node[] nodes = new Node[po.length]; for (int i = 0; i < po.length; i++) { nodes[i] = new Node(po[i],shang[i]); } Node temp = nodes[0]; int poivot = d+po.length-1; int i = 0, j = po.length-1; while(i < j){ while(i < j && nodes[j].a <= poivot) { --j; } nodes[i] = nodes[j]; while(i < j && nodes[i].a > poivot){ ++i; } nodes[j] = nodes[i]; } nodes[i] = temp; int start = nodes.length; for(int l = 0; l < nodes.length; ++l) { if(nodes[l].a <= poivot) { start = l; } } Arrays.sort(nodes, new Comparator<Node>() { @Override public int compare(Node o1,Node o2){ if(o1.a == o2.a){ return o1.b - o2.b; } return o2.a - o1.a; }}); int sum = 0; for(int l = 0; l < nodes.length; ++l) { if(nodes[l].a > d+l) { sum += nodes[l].b; } } System.out.println(sum); }} 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646 5666768697071Copy the code

Topic describes

Now suppose that a city has a person, there are people who carry a poison, in the city there were m family gatherings, we assume that people who carry the virus in the family party everyone will be infected, the infected person will continue to infect other, want to know, now the m after the party, the city a total of how many people (including people) virus infection Is numbered from 0.

The input describing

For each set of test data, enter three integers n, M, and F on the first line to represent the number of people in the city, the number of family gatherings that took place, and the number of people who originally carried the virus.

Next, enter m lines, each with a number q, representing the number of people at the house party, followed by q, representing the number of participants.

2 <= n <= 10000

1 <= m <= 500

1 q or less 50 or less

The input

10 2 0

2 0 3

1 2 3 0

The output

4

Train of thought

Contain the infected people in the queue, and then out of the queue, and then each party of the infected people in the queue to identify the party is not infected, until the queue is empty. And then count all the infected people.

import java.util.HashSet; import java.util.Scanner; public class Test27 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n,m,f; n = sc.nextInt(); m = sc.nextInt(); f = sc.nextInt(); HashSet<Integer> set = new HashSet<>(); set.add(f); for (int i = 0; i < m; i++) { int q = sc.nextInt(); int[] part = new int[q]; boolean flag = false; for (int j = 0; j <q ; j++) { part[j] = sc.nextInt(); if(set.contains(part[j])){ flag = true; } if(flag){ for (int k = 0; k < q; k++) { set.add(part[k]); } } } } System.out.println(set.size()); }} 1234567891011121314151617181920212223242526272829303132Copy the code

Please pay attention to the public number “programmer interview way” reply to “interview” to get a complete set of interview package!!

Recommendation of high quality articles

1. Computer network —- Three times handshake four times wave 2. An article that gives you a thorough understanding of the structure of HTTP request and response packets 3. A dream come true —– project self-introduction 4. An article that lets you thoroughly understand the past life of HTTP 5. An article that will get you through HTTP methods and status codes 6. Here’s your design pattern 7. Shock!!! Check out this programmer interview manual!! 9. Nearly 30 interviews shared