zoukankan      html  css  js  c++  java
  • NOIP2012 借教室

    今天又做了一道NOIP的题,对自信又打击了不少,这次做的是D2T2。

    这个题上来一看直接暴力,结果得了45分(还算理想,我不会说我一开始写暴力写错了。。。)

    后来想正解,没想出来(本来想到了,但是被自己否了。。。)

    看题解发现是二分答案加差分。。。直接上代码:

    暴力45分:

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    using namespace std;
    #define duke(i,a,n) for(int i = a;i <= n;i++)
    #define lv(i,a,n) for(int i = a;i >= n;i--)
    template <class T>
    void read(T &x)
    {
        char c;
        bool op = 0;
        while(c = getchar(),c < '0' || c > '9')
            if(c == '-') op = 1;
        x = c - '0';
        while(c = getchar(),c >= '0' && c <= '9')
            x = x * 10 + c - '0';
        if(op == 1)
            x = -x;
    }
    struct node
    {
        int l,r,w,h;
    }a[1000010];
    bool cmp(node a,node b)
    {
        if(a.l != b.l)
        {
            return a.l < b.l;
        }
        else
        {
            return a.r < b.r;
        }
    }
    int t[1000010],n,m;
    int main()
    {
        read(n);read(m);
        duke(i,1,n)
        {
            read(t[i]);
        }
        duke(i,1,m)
        {
            read(a[i].w);
            read(a[i].l);
            read(a[i].r);
    //        a[i].h = i;
        }
    //    sort(a + 1,a + m + 1,cmp);
        int k = 1;
        duke(i,1,m)
        {
            duke(j,a[i].l,a[i].r)
            {
                t[j] -= a[i].w;
                if(t[j] < 0)
                {
                    printf("-1
    %d",i);
                    return 0;
                }
            }
        }
        printf("0
    ");
        return 0;
    }

    AC代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #define duke(i,a,n) for(int i = a;i <= n;i++)
    #define lv(i,a,n) for(int i = a;i >= n;i--)
    using namespace std;
    int dfn[200010],low[200010],lst[200010],len = 0;
    int ans = 0,top = 0,n,m,tot = 0,cut[200010];
    bool vis[200010];
    template <class T>
    void read(T &x)
    {
        char c;
        bool op = 0;
        while(c = getchar(),c < '0' || c > '9')
            if(c == '-') op = 1;
        x = c - '0';
        while(c = getchar(),c >= '0' && c <= '9')
        {
            x = x * 10 + c - '0';
        }
        if(op == 1)
            x = -x;
    }
    struct node{
        int l,r,nxt;
    }a[200010];
    void add(int x,int y)
    {
        a[++len].l = x;
        a[len].r = y;
        a[len].nxt = lst[x];
        lst[x] = len;
    }
    void tarjan(int x,int fa)
    {
        int child = 0;
        dfn[x] = low[x] = ++tot;
    //    stc[++top] = x;
    //    vis[x] = 1;
        for(int k = lst[x];k;k = a[k].nxt)
        {
            int y = a[k].r;
            if(!dfn[y])
            {
                tarjan(y,fa);
                low[x] = min(low[x],low[y]);
                if (low[y] >= dfn[x] && fa != x) cut[x]=true;
                if(x == fa)
                    child++;
            }
    //        else if(vis[y])
            {
                low[x] = min(low[x],dfn[y]);
            }
        }
        if(x == fa && child >= 2)
            cut[x] = true;
    }
    int main()
    {
        memset(cut,false,sizeof(cut));
        read(n); read(m);
        duke(i,1,m)
        {
            int x,y;
            read(x);read(y);
            add(x,y);
            add(y,x);
        }
        duke(i,1,n)
        {
            if(dfn[i] == 0)
            {
                tarjan(i,i);
            }
        }
        int num = 0;
        duke(i,1,n)
        {
            if(cut[i])
                num++;
        }
        printf("%d
    ",num);
        duke(i,1,n)
        {
            if(cut[i])
                printf("%d ",i);
        }
        return 0;
    }
    /*
    6 7
    1 2
    1 3
    1 4
    2 5
    3 5
    4 5
    5 6
    */
  • 相关阅读:
    CodeBlocks 中fopen函数不支持命令 “r”
    【转载】分享一些Qt学习资源,欢迎下载
    【转载】知乎答案----孙志岗----Google 发布了程序员养成指南,国内互联网巨头是否也有类似的指南和课程推荐
    【转载】谷歌公司推荐的程序员必修课(英文教程)
    【转载】张逸--ThoughtWorks(中国)程序员读书雷达
    在windows环境下,为什么要用Notepad++编辑?
    【转载】池建强--趣谈个人建站
    JAVA入门第二季 第一章 类和对象
    CAP理论总结
    分布式通信方式之消息队列之RocketMQ
  • 原文地址:https://www.cnblogs.com/DukeLv/p/9404475.html
Copyright © 2011-2022 走看看