<cf>Schedule

课程调度算法
Schedule
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

At the beginning of the new semester there is new schedule in the Berland State University. According to this schedule, n groups have lessons at the room 31. For each group the starting time of the lesson and the finishing time of the lesson are known. It has turned out that it is impossible to hold all lessons, because for some groups periods of their lessons intersect. If at some moment of time one groups finishes it's lesson, and the other group starts the lesson, their lessons don't intersect.

The dean wants to cancel the lesson in one group so that no two time periods of lessons of the remaining groups intersect. You are to find all ways to do that.

Input

The first line contains integer n (1 ≤ n ≤ 5000) — amount of groups, which have lessons in the room 31. Then n lines follow, each of them contains two integers li ri (1 ≤ li < ri ≤ 106) — starting and finishing times of lesson of the i-th group. It is possible that initially no two lessons intersect (see sample 1).

Output

Output integer k — amount of ways to cancel the lesson in exactly one group so that no two time periods of lessons of the remaining groups intersect. In the second line output k numbers — indexes of groups, where it is possible to cancel the lesson. Groups are numbered starting from 1 in the order that they were given in the input. Output the numbers in increasing order.

Sample test(s)
input
3
3 10
20 30
1 3
output
3
1 2 3 
input
4
3 10
20 30
1 3
1 39
output
1
4 
input
3
1 5
2 6
3 7
output
0

AC Code #1:
//Memory: 1500 KB		Time: 50 MS
//Language: GNU C++ 4.6		Result: Accepted
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

struct Group
{
    int s,e,idx;//"s" for start,"e" for end,"idx" for index 
}a[5001];

int pos[5001],k=0;//pos 记录被舍弃的课程的下标
int n,i,j;
bool OK=1;

bool cmp(Group x, Group y)
{
    if(x.e!=y.e) return x.e<y.e;//按结束时间从小到大排序
    else if(x.s!=y.s) return x.s<y.s;
    else return x.idx<y.idx;
}

//此函数用于判断去掉a[i]后余下课程是否有时间冲突,是则返回false  
bool check(int i)
{
    for(int j=1;j<i-1;j++)
    {
        if(a[j].e>a[j+1].s)
        {
            return 0;
        }
    }
    if(i>1 && i<n && a[i-1].e>a[i+1].s)
    {
        return 0;
    }
    for(int j=i+1;j<n;j++)
    {
        if(a[j].e>a[j+1].s)
        {
            return 0;
        }
    }
    return 1;
}

int main()
{
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        scanf("%d%d",&a[i].s,&a[i].e);
        a[i].idx=i;
    }
    sort(a+1,a+n+1,cmp);
    for(i=1;i<n;i++)
    {
        if(a[i].e>a[i+1].s)
        {
            if(!k)
            {
                bool m=check(i);
                bool n=check(i+1);
                if(m && !n) pos[k++]=a[i].idx;
                else if(!m && n)
                {
                    pos[k++]=a[i+1].idx;
                    i++;
                }
                else if(m && n)
                {
                    pos[k++]=a[i].idx;
                    pos[k++]=a[i+1].idx;
                    i++;
                }
                else
                {
                    OK=0;
                    break;
                }
            }
            else
            {
                OK=0;
                break;
            }
        }
    }
    if(!OK) puts("0");
    //ok==1但k==0
    else if(!k)
    {
        printf("%d\n",n);
        for(i=1;i<n;i++)
            printf("%d ",i);
        printf("%d\n",i);
    }
    //ok==1 and k>0
    else
    {
        sort(pos,pos+k);
        printf("%d\n",k);
        for(i=0;i<k-1;i++)
            printf("%d ",pos[i]);
        printf("%d\n",pos[i]);
    }
    return 0;
}


还有一个复杂一点的方法:
AC Code #2:
//Memory: 1500 KB		Time: 50 MS
//Language: GNU C++ 4.6		Result: Accepted

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

struct Group
{
    int s,e,idx;
}a[5001];

int pos[5001],k=0;
int n,i,j;
bool OK=1;

bool cmp(Group x, Group y)
{
    if(x.e!=y.e) return x.e<y.e;//按结束时间从小到大排序
    else if(x.s!=y.s) return x.s<y.s;
    else return x.idx<y.idx;
}

int check(int i)
{
    int j=i+1;
    while(a[i].e>a[j].s && j<=n)
    {
        j++;
    }
    return j;
}

int main()
{
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        scanf("%d%d",&a[i].s,&a[i].e);
        a[i].idx=i;
    }
    sort(a+1,a+n+1,cmp);
    for(i=1;i<n;i++)
    {
        j=check(i);
        //a[i]的结束时间晚于a[i+1]和a[i+2]的开始时间,必弃a[i]
        if(j-i>2)
        {
            //当前面已有课程被舍去,OK为假.
            if(k)
            {
                OK=0;
                break;
            }
            else
            {
                pos[k]=a[i].idx;
                //当后面仍有课程必须舍弃,OK也为假
                for(i++;i<n && a[i].e<=a[i+1].s;i++){}
                if(i<n)
                {
                    OK=0;
                }
                break;
            }
        }
        //这时a[i]的结束时间大于且仅大于a[i+1]的开始时间,两者要舍去其一
        else if(j-i==2)
        {
            //当前面已有课程被舍去,OK为假.
            if(k)
            {
                OK=0;
                break;
            }
            else
            {
                //当>2时a[i+1]必定要舍去;当==2时,a[i+1]和a[i+2]必弃其一,又结合
                //a[i]和a[i+1]必弃其一,则还是只能舍弃a[i+1].
                if(check(i+1)-(i+1)>=2)
                {
                    pos[k++]=a[i+1].idx;
                }
                else if(i>0 && a[i-1].e>a[i+1].s)
                {
                    pos[k++]=a[i+1].idx;
                }
                //此时a[i+1].e<=a[i+2].s,a[i],a[i+1]都可以选为被舍弃的组。
                else
                {
                    pos[k++]=a[i].idx;
                    pos[k++]=a[i+1].idx;
                }
                i++;
            }
        }
    }
    if(!OK) puts("0");
    //ok==1但k==0
    else if(!k)
    {
        printf("%d\n",n);
        for(i=1;i<n;i++)
            printf("%d ",i);
        printf("%d\n",i);
    }
    else
    {
        sort(pos,pos+k);
        printf("%d\n",k);
        for(i=0;i<k-1;i++)
            printf("%d ",pos[i]);
        printf("%d\n",pos[i]);
    }
    return 0;
}



void power_supply_changed(struct power_supply *psy) { unsigned long flags; dev_dbg(&psy->dev, "%s\n", __func__); pr_info("%s Oliver\n", __func__); spin_lock_irqsave(&psy->changed_lock, flags); psy->changed = true; pm_stay_awake(&psy->dev); spin_unlock_irqrestore(&psy->changed_lock, flags); schedule_work(&psy->changed_work); } EXPORT_SYMBOL_GPL(power_supply_changed); static int mt_charger_set_property(struct power_supply *psy, enum power_supply_property psp, const union power_supply_propval *val) { struct mt_charger *mtk_chg = power_supply_get_drvdata(psy); struct chg_type_info *cti = NULL; #ifdef CONFIG_EXTCON_USB_CHG struct usb_extcon_info *info; #endif pr_info("%s Oliver enter psp=%d val->intval=%d \n", __func__,psp,val->intval); if (!mtk_chg) { pr_notice("%s: no mtk chg data\n", __func__); return -EINVAL; } #ifdef CONFIG_EXTCON_USB_CHG info = mtk_chg->extcon_info; #endif switch (psp) { case POWER_SUPPLY_PROP_ONLINE: mtk_chg->chg_online = val->intval; mt_charger_online(mtk_chg); return 0; case POWER_SUPPLY_PROP_CHARGE_TYPE: mtk_chg->chg_type = val->intval; break; default: return -EINVAL; } dump_charger_name(mtk_chg->chg_type); cti = mtk_chg->cti; pr_info("%s Oliver cti->ignore_usb=%d\n", __func__,cti->ignore_usb); if (!cti->ignore_usb) { /* usb */ if ((mtk_chg->chg_type == STANDARD_HOST) || (mtk_chg->chg_type == CHARGING_HOST) || (mtk_chg->chg_type == NONSTANDARD_CHARGER)) { mt_usb_connect(); #ifdef CONFIG_EXTCON_USB_CHG info->vbus_state = 1; #endif } else { mt_usb_disconnect(); #ifdef CONFIG_EXTCON_USB_CHG info->vbus_state = 0; #endif } } pr_info("%s Oliver ------111\n", __func__); queue_work(cti->chg_in_wq, &cti->chg_in_work); #ifdef CONFIG_EXTCON_USB_CHG if (!IS_ERR(info->edev)) queue_delayed_work(system_power_efficient_wq, &info->wq_detcable, info->debounce_jiffies); #endif pr_info("%s Oliver------ 2222\n", __func__); power_supply_changed(mtk_chg->ac_psy); pr_info("%s Oliver ------333\n", __func__); //power_supply_changed(mtk_chg->poe_psy); pr_info("%s Oliver ------444\n", __func__); power_supply_changed(mtk_chg->usb_psy); pr_info("%s Oliver end\n", __func__); return 0; } Log error 如下,發生krernel crash [ 12.891284] <0>.(0)[162:kworker/0:2]mt_charger_set_property Oliver ------111 [ 12.891688] <3>.(3)[633:camerahalserver][pmic_enable_interrupt] intNo=26, en=0, depth=1 [ 12.892568] <0>.(0)[162:kworker/0:2]mt_charger_set_property Oliver------ 2222 [ 12.892574] <0>.(2)[224:kworker/u8:2]mtk_charger_int_handler [ 12.892585] <0>.(2)[224:kworker/u8:2]battery_callback:1 [ 12.893588] <0>.(3)[633:camerahalserver][imgsensor][regulator] imgsensor_oc_interrupt INT_VCAMIO_OC 0 [ 12.893598] <0>.(3)[633:camerahalserver][pmic_enable_interrupt] intNo=26, en=0, depth=1 [ 12.893600] <0>.(3)[633:camerahalserver][imgsensor][regulator] imgsensor_oc_interrupt INT_VCAMIO_OC 0 [ 12.893650] <0>.(3)[633:camerahalserver][pmic_enable_interrupt] intNo=25, en=0, depth=1 [ 12.893653] <0>.(3)[633:camerahalserver][imgsensor][regulator] imgsensor_oc_interrupt INT_VCAMD_OC 0 [ 12.893656] <0>.(3)[633:camerahalserver][pmic_enable_interrupt] intNo=26, en=0, depth=1 [ 12.893658] <0>.(3)[633:camerahalserver][imgsensor][regulator] imgsensor_oc_interrupt INT_VCAMIO_OC 0 [ 12.893723] <0>.(3)[633:camerahalserver]imgsensor_release 0 [ 12.909490] <0>.(0)[162:kworker/0:2]power_supply_changed Oliver [ 12.910395] <0>.(0)[162:kworker/0:2]mt_charger_set_property Oliver ------333 [ 12.911318] <0>.(0)[162:kworker/0:2]power_supply_changed Oliver [ 12.912085] <0>-(0)[162:kworker/0:2]Unable to handle kernel read from unreadable memory at virtual address 00000380 [ 12.913387] <0>-(0)[162:kworker/0:2]Mem abort info: [ 12.914002] <0>-(0)[162:kworker/0:2] Exception class = DABT (current EL), IL = 32 bits [ 12.915003] <0>-(0)[162:kworker/0:2] SET = 0, FnV = 0 [ 12.915647] <0>-(0)[162:kworker/0:2] EA = 0, S1PTW = 0 [ 12.916303] <0>-(0)[162:kworker/0:2]Data abort info: [ 12.916926] <0>-(0)[162:kworker/0:2] ISV = 0, ISS = 0x00000005 [ 12.917667] <0>-(0)[162:kworker/0:2] CM = 0, WnR = 0 [ 12.918302] <0>-(0)[162:kworker/0:2]user pgtable: 4k pages, 39-bit VAs, pgd = 0000000068b04685 [ 12.919380] <0>-(0)[162:kworker/0:2][0000000000000380] *pgd=0000000000000000, *pud=0000000000000000 [ 12.920516] <0>-(0)[162:kworker/0:2]Internal error: Oops: 96000005 [#1] PREEMPT SMP [ 12.921475] <0>-(0)[162:kworker/0:2]disable aee kernel api [ 12.921478] <0>-(0)[162:kworker/0:2]Kernel Offset: 0x0 from 0xffffff8008000000 [ 12.923066] <0>-(0)[162:kworker/0:2]PHYS_OFFSET: 0x40000000 [ 12.923765] <0>-(0)[162:kworker/0:2]Modules linked in: wlan_drv_gen4m ffffff8001072000 (null) 2310144 0 (O) wmt_chrdev_wifi ffffff8001065000 (null) 28672 0 (O) gps_drv ffffff8001051000 (null) 61440 0 (O) bt_drv ffffff8001042000 (null) 40960 0 (O) wmt_drv ffffff8000f08000 (null) 1191936 0 (O) fpsgo ffffff8000e3e000 (null) 811008 0 (PO) [ 12.929129] <0>-(0)[162:kworker/0:2]Unable to handle kernel paging request at virtual address ffffff8009fc10fc [ 12.930380] <0>-(0)[162:kworker/0:2]Mem abort info: [ 12.930993] <0>-(0)[162:kworker/0:2] Exception class = DABT (current EL), IL = 32 bits [ 12.931991] <0>-(0)[162:kworker/0:2] SET = 0, FnV = 0 [ 12.932633] <0>-(0)[162:kworker/0:2] EA = 0, S1PTW = 0 [ 12.933285] <0>-(0)[162:kworker/0:2]Data abort info: [ 12.933906] <0>-(0)[162:kworker/0:2] ISV = 0, ISS = 0x00000061 [ 12.934645] <0>-(0)[162:kworker/0:2] CM = 0, WnR = 1 [ 12.935279] <0>-(0)[162:kworker/0:2]swapper pgtable: 4k pages, 39-bit VAs, pgd = 00000000646d4cf1 [ 12.936385] <0>-(0)[162:kworker/0:2][ffffff8009fc10fc] *pgd=00000000bf93e003, *pud=00000000bf93e003, *pmd=00000000bf938003, *pte=00680000543f0703 [ 12.938014] <0>-(0)[162:kworker/0:2]die_lock:cpu:0 already locked(owner:0) [ 12.938876] <0>-(0)[162:kworker/0:2]CPU: 0 PID: 162 Comm: kworker/0:2 Tainted: P W O 4.14.186-g5af1172-dirty #11 [ 12.940262] <0>-(0)[162:kworker/0:2]Hardware name: MT8365 (DT) [ 12.941004] <0>-(0)[162:kworker/0:2]Workqueue: events do_charger_detection_work [ 12.941918] <0>-(0)[162:kworker/0:2]Call trace: [ 12.942490] <0>-(0)[162:kworker/0:2] dump_backtrace+0x0/0x180 [ 12.943209] <0>-(0)[162:kworker/0:2] show_stack+0x14/0x1c [ 12.943887] <0>-(0)[162:kworker/0:2] dump_stack+0xd4/0x10c [ 12.944573] <0>-(0)[162:kworker/0:2] die+0xb8/0x27c [ 12.945185] <0>-(0)[162:kworker/0:2] __do_kernel_fault+0x130/0x140 [ 12.945958] <0>-(0)[162:kworker/0:2] do_alignment_fault+0x30/0x64 [ 12.946719] <0>-(0)[162:kworker/0:2] do_mem_abort+0x4c/0xd0 [ 12.947417] <0>-(0)[162:kworker/0:2] el1_da+0x20/0x38 [ 12.948050] <0>-(0)[162:kworker/0:2] __memcpy+0x88/0x180 [ 12.948719] <0>-(0)[162:kworker/0:2] __mrdump_create_oops_dump+0x198/0x2b8 [ 12.949578] <0>-(0)[162:kworker/0:2] mrdump_common_die+0x94/0x108 [ 12.950340] <0>-(0)[162:kworker/0:2] ipanic_die+0x20/0x74 [ 12.951017] <0>-(0)[162:kworker/0:2] notify_die+0x64/0xb4 [ 12.951693] <0>-(0)[162:kworker/0:2] die+0x118/0x27c [ 12.952314] <0>-(0)[162:kworker/0:2] __do_kernel_fault+0x130/0x140 [ 12.953087] <0>-(0)[162:kworker/0:2] do_page_fault+0x44/0x3a0 [ 12.953807] <0>-(0)[162:kworker/0:2] do_translation_fault+0x44/0x98 [ 12.954590] <0>-(0)[162:kworker/0:2] do_mem_abort+0x4c/0xd0 [ 12.955286] <0>-(0)[162:kworker/0:2] el1_da+0x20/0x38 [ 12.955921] <0>-(0)[162:kworker/0:2] _raw_spin_lock_irqsave+0x1c/0x50 [ 12.956728] <0>-(0)[162:kworker/0:2] mt_charger_set_property+0x1d4/0x210 [ 12.957566] <0>-(0)[162:kworker/0:2] power_supply_set_property+0x24/0x38 [ 12.958405] <0>-(0)[162:kworker/0:2] chrdet_inform_psy_changed+0x94/0x138 [ 12.959253] <0>-(0)[162:kworker/0:2] mtk_pmic_enable_chr_type_det+0x80/0xac [ 12.960124] <0>-(0)[162:kworker/0:2] do_charger_detect+0x24/0x8c [ 12.960876] <0>-(0)[162:kworker/0:2] do_charger_detection_work+0x1c/0x60 [ 12.961714] <0>-(0)[162:kworker/0:2] process_one_work+0x278/0x4d8 [ 12.962477] <0>-(0)[162:kworker/0:2] worker_thread+0x2c8/0x568 [ 12.963208] <0>-(0)[162:kworker/0:2] kthread+0x17c/0x18c [ 12.963873] <0>-(0)[162:kworker/0:2] ret_from_fork+0x10/0x18 [ 12.964581] <0>-(0)[162:kworker/0:2]Internal error: Oops: 96000061 [#2] PREEMPT SMP
08-20
[root@k8s-master ~]# kubectl get po -n kube-system NAME READY STATUS RESTARTS AGE calico-kube-controllers-cd8566cf-k7nrw 0/1 Pending 0 3m53s calico-node-8fmgc 0/1 Init:0/3 0 3m53s calico-node-pxsgx 0/1 Init:ImagePullBackOff 0 3m53s coredns-6d8c4cb4d-hntkb 0/1 Pending 0 99m coredns-6d8c4cb4d-v6lwm 0/1 Pending 0 99m etcd-k8s-master 1/1 Running 0 99m kube-apiserver-k8s-master 1/1 Running 0 99m kube-controller-manager-k8s-master 1/1 Running 0 99m kube-proxy-b6lwj 1/1 Running 0 99m kube-proxy-q2787 1/1 Running 0 62m kube-scheduler-k8s-master 1/1 Running 0 99m [root@k8s-master ~]# kubectl describe po calico-kube-controllers-cd8566cf-k7nrw -n kube-system Name: calico-kube-controllers-cd8566cf-k7nrw Namespace: kube-system Priority: 2000000000 Priority Class Name: system-cluster-critical Node: <none> Labels: k8s-app=calico-kube-controllers pod-template-hash=cd8566cf Annotations: <none> Status: Pending IP: IPs: <none> Controlled By: ReplicaSet/calico-kube-controllers-cd8566cf Containers: calico-kube-controllers: Image: calico/kube-controllers:v3.25.0 Port: <none> Host Port: <none> Liveness: exec [/usr/bin/check-status -l] delay=10s timeout=10s period=10s #success=1 #failure=6 Readiness: exec [/usr/bin/check-status -r] delay=0s timeout=1s period=10s #success=1 #failure=3 Environment: ENABLED_CONTROLLERS: node DATASTORE_TYPE: kubernetes Mounts: /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-tj7nl (ro) Conditions: Type Status PodScheduled False Volumes: kube-api-access-tj7nl: Type: Projected (a volume that contains injected data from multiple sources) TokenExpirationSeconds: 3607 ConfigMapName: kube-root-ca.crt ConfigMapOptional: <nil> DownwardAPI: true QoS Class: BestEffort Node-Selectors: kubernetes.io/os=linux Tolerations: CriticalAddonsOnly op=Exists node-role.kubernetes.io/control-plane:NoSchedule node-role.kubernetes.io/master:NoSchedule node.kubernetes.io/not-ready:NoExecute op=Exists for 300s node.kubernetes.io/unreachable:NoExecute op=Exists for 300s Events: Type Reason Age From Message ---- ------ ---- ---- ------- Warning FailedScheduling 15s (x6 over 5m20s) default-scheduler 0/2 nodes are available: 2 node(s) had taint {node.kubernetes.io/not-ready: }, that the pod didn't tolerate. [root@k8s-master ~]#
11-30
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值