c++ - Codechef 虫洞 : What's wrong with my logic?

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

我一直在努力解决这个问题 problem来自 Codechef .但是我的逻辑有问题,我无法纠正,而且我等不及社论出现,因为比赛要进行很多天(比如 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;
}

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

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

最佳答案

改变

`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;
}

关于c++ - Codechef 虫洞 : What's wrong with my logic?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37573421/

相关文章:

algorithm - 一场有100个对手的比赛,赢尽可能多的钱

algorithm - 在 big-O 中使用 < 或 ≤

java - 大O : is this the tightest upper bound for recursive algorithm?

algorithm - 无除法快速平均

C++模板类运算符重载

c++ - 在 C++ 中序列化对象

c++ - 在并行桶排序中使用递归基数排序

c++ - 模板化 printMap 未编译

algorithm - 如何编写程序来生成排序决策树?

algorithm - 为什么二分查找是一种分而治之的算法?