Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class olimpiada {
- public static void main(String[] args) {
- //input
- Scanner scanner = new Scanner(System.in);
- String[] temp = scanner.nextLine().split(" ");
- int n = Integer.parseInt(temp[0]);
- if(n < 1 || n > 100000) return;
- int k = Integer.parseInt(temp[1]);
- if(k < 1 || k > 20) return;
- int[][] timetable = new int[k][2];
- for(int i = 0; i < k; i++) {
- temp = scanner.nextLine().split(" ");
- timetable[i][0] = Integer.parseInt(temp[0]);
- timetable[i][1] = Integer.parseInt(temp[1]);
- if(timetable[i][1] > 2*n) return;
- if(timetable[i][0] < 0 || timetable[i][0] > timetable[i][1]) return;
- }
- //calculating
- int[][] timeGaps = new int[1][2];
- timeGaps[0][0] = 0;
- timeGaps[0][1] = 0;
- for(int i = 0; i < k; i++) {
- int amount = timetable[i][1] - timetable[i][0]+1;
- int[][] timeGapsTemp = new int[amount * timeGaps.length][2];
- int index = 0;
- for(int j = 0; j < timeGaps.length; j++) {
- for (int l = 0; l < amount; l++) {
- timeGapsTemp[index][(i+1) % 2] = timeGaps[j][(i+1) % 2];
- int tempTime = timetable[i][0] - timeGaps[j][0] - timeGaps[j][1] + l;
- if (tempTime > n)
- continue;
- timeGapsTemp[index][i % 2] += tempTime;
- int timeLeft = 2 * n - timeGapsTemp[index][0] - timeGapsTemp[index][1];
- if(timeGapsTemp[index][i % 2] == n && timeGapsTemp[index][(i+1) % 2] + timeLeft == n) {
- System.out.println("YES");
- System.out.println(i+1);
- return;
- }
- index++;
- }
- }
- timeGaps = timeGapsTemp;
- }
- System.out.println("NO");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement