Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <assert.h>
- #include <iostream>
- #include <map>
- #define MAXN 100000
- using namespace std;
- bool controlla_carica(int carica, map<int, bool> schema, int M) {
- //cout << "Controllo " << carica << endl;
- for (int i = 0; i < M; i++) {
- if (schema[i]) carica++;
- else carica--;
- if (carica <= 0) {
- return false;
- }
- }
- return true;
- }
- int ricarica(int N, int M, int A[], int B[]) {
- map<int, bool> schema;
- for (int i = 0; i < N; i++) {
- for (int j = A[i]; j <= B[i]; j++) {
- schema[j-1] = true;
- }
- }
- int first_carica, last_carica;
- int carica = 1;
- while (!controlla_carica(carica, schema, M)) {
- carica *= 2;
- }
- first_carica = carica / 2;
- last_carica = carica - 1;
- while (first_carica < last_carica) {
- int middle = first_carica + (last_carica - first_carica) / 2 + 1;
- if (!controlla_carica(middle, schema, M)) {
- first_carica = middle;
- }
- else {
- last_carica = middle - 1;
- }
- }
- first_carica++;
- //cout << first_carica << endl;
- return first_carica;
- }
- int A[MAXN], B[MAXN];
- int main() {
- FILE *fr, *fw;
- int N, M, i;
- fr = fopen("input.txt", "r");
- fw = fopen("output.txt", "w");
- assert(2 == fscanf(fr, "%d %d", &N, &M));
- for(i=0; i<N; i++)
- assert(2 == fscanf(fr, "%d %d", &A[i], &B[i]));
- fprintf(fw, "%d\n", ricarica(N, M, A, B));
- fclose(fr);
- fclose(fw);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement