【问题标题】:Codechef Wormholes: What's wrong with my logic?Codechef 虫洞:我的逻辑有什么问题?
【发布时间】:2016-10-01 02:27:35
【问题描述】:

我一直在尝试从Codechef 解决这个problem。但是我的逻辑有问题,我无法纠正,我等不及要发表社论了,因为比赛持续了很多天(比如 200 天)。

我的代码:

#include <bits/stdc++.h>

using namespace std;

struct exam {int s,e;};

int main() {
    int n,x,y;
    scanf("%d%d%d",&n,&x,&y);

    exam ar[n];
    int v[x],w[y];

    for (int i = 0; i < n; ++i) scanf("%d%d",&ar[i].s,&ar[i].e);
    for (int i = 0; i < x; ++i) scanf("%d",&v[i]);
    for (int i = 0; i < y; ++i) scanf("%d",&w[i]);
    stable_sort(v,v+x); stable_sort(w,w+y);

    int mini = 1e6;

    for (int i = 0; i < n; ++i) {

        int *t1 = lower_bound(v,v+x,ar[i].s);

        if (*t1 > ar[i].s && t1 > v) --t1;
        if (*t1 > ar[i].s && t1 == v) continue;

        int *t2 = lower_bound(w,w+y,ar[i].e);

        if (t2 == w+y) continue;

        mini = min(mini,(*t2-*t1)+1);
    }

    printf("%d",mini);
    return 0;
}

遗憾的是,这段代码没有通过两个测试用例:

谁能告诉我我的逻辑有什么问题?

【问题讨论】:

    标签: c++ algorithm binary-search divide-and-conquer


    【解决方案1】:

    改变

    `if (*t1 > ar[i].s && t1 > v) --t1;
     if (*t1 > ar[i].s && t1 == v) continue;`
    

    if (*t1 != ar[i].s) {
        if (t1 == v)
          continue;
        else
          --t1;
    }
    

    【讨论】:

    • 也许可以解释一下,与 OP 代码所表现出的不正确行为相比,此代码在哪些情况下表现正确?
    猜你喜欢
    • 2016-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-05
    相关资源
    最近更新 更多