On the application of differential fractional group (ii) &[noip2012] borrowing classroom

Source: Internet
Author: User

[NOIP2012 Improve & Rokua P1083] Borrow classroom description

It is often necessary to rent a classroom during college. Large to the Department to organize activities, small to study group discussion, all need to apply to the school to borrow the classroom. The classroom size function is different, borrows the classroom person's identity to be different, borrows the classroom The procedure also is dissimilar. In the face of the vast amount of information rented classrooms, we naturally want to program to solve this problem.
We need to deal with the next n days of borrowing classroom information, where I day school has RI classrooms available for rent. A total of M orders, each order with three positive integer description, respectively, DJ,SJ,TJ, indicating that a renter from SJ Day to TJ days to rent a classroom (including SJ Day and TJ Days), daily need to rent a DJ classroom. We assume that the renter has no requirement for the size and location of the classroom. That is, for each order, we only need to provide a DJ classroom every day, and they are specific classrooms, whether the same classroom every day is not considered.
The principle of borrowing a classroom is first come first served, that is, we have to assign the classroom to each order in order. If you encounter an order that cannot be fully met during the allocation process, you will need to stop the classroom assignment and notify the current applicant to modify the order. The inability to meet here means that the number of classrooms remaining at least one day from the day of SJ to TJ is less than a DJ.
Now we need to know if there will be an order that is not fully satisfied. If so, which applicant needs to be notified to modify the order.
Input format:
The first line contains two positive integer n,m, representing the number of days and orders.
The second line contains n positive integers, where the number of I is RI, which indicates the number of classrooms that can be used for lease on day I.
Next there are m lines, each containing three positive integer DJ,SJ,TJ, indicating the number of leases, the start of the lease, and the end of the day respectively.
Each row is separated by a space between the two numbers that are adjacent to each other. The number of days and orders is numbered with an integer starting from 1.
Output format:
If all orders are satisfied, the output is only one row and contains an integer 0. Otherwise (orders cannot be fully satisfied)
Output two lines, the first line outputs a negative integer-1, and the second line outputs the applicant number to modify the order.

Solution

1. It is obvious that the subject can be done by line tree, this article does not discuss line tree practice, only the difference score Group + Two-point answer procedure;
2. First, to determine the current status of all tasks can meet the requirements of the daily classroom volume within the allowable range, then we handle the way is to use the difference score group to record each task in the current state of the total daily demand for the classroom and the difference between the previous day, then according to the nature of the difference score Group can be obtained every day of actual demand , that is, the demand value of the n[i]=σ (1≤k≤i) f[i], and the number of allowed classrooms on the topic of the day, if greater than the supply, return to the wrong, because each pair is in the day order, we can use a variable for each day of the current task actual demand value, the specific implementation is as follows (F is the difference score Group, OK is the number of classrooms allowed for each day, J is the demand value for the day of the cycle):

bool valid(int x){    memset(need,0,sizeof(need));    memset(f,0,sizeof(f));    for(i=1;i<=x;++i){        f[a[i].s]+=a[i].w;        f[a[i].t+1]-=a[i].w;    }    for(i=1;i<=n;++i){        need[i]=need[i-1]+f[i];        if(need[i]>ok[i])return false;    }    return true;}

3. According to the description of the topic, test the operation of M-type task is feasible, if feasible directly output 0return;
4. If not, we will be the two-point answer, obviously at this time the horizontal axis for the task number, two-point answer (the same way) until all the tasks can be satisfied, note if the use of mid= (l+r+1)/2 of the way the answer to add one, using mid= (L+R)/ 2 of the way two points directly output the found solution can be;

#include<iostream>#include<cmath>#include<cstring>#include<cstdio>#include<algorithm>using namespace std;int n,m,i,j,k,maxn=0,f[1001000],ok[1000100];struct node{    int s,t,w;}a[1001000];bool valid(int x){    memset(f,0,sizeof(f));    for(i=1;i<=x;++i){        f[a[i].s]+=a[i].w;        f[a[i].t+1]-=a[i].w;    }    j=0;    for(i=1;i<=n;++i){        j+=f[i];        if(j>ok[i])return false;    }    return true;}int main(){    memset(f,0,sizeof(f));    scanf("%d%d",&n,&m);    for(i=1;i<=n;++i)scanf("%d",&ok[i]);    for(i=1;i<=m;++i)scanf("%d%d%d",&a[i].w,&a[i].s,&a[i].t);    if(valid(m)){printf("0\n");return 0;}    int l=1,r=m;    while(l<r){        int mid=(l+r)/2;        if(valid(mid)) l=mid+1;        else r=mid;    }    printf("-1\n%d\n",l);    return 0;}
The basis of the difference Score Group reference previous essays: http://www.cnblogs.com/COLIN-LIGHTNING/p/8436624.html

On the application of differential fractional group (ii) &[noip2012] borrowing classroom

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.