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\n%d",i);
                return 0;
            }
        }
    }
    printf("0\n");
    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\n",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
*/

 

posted @ 2018-08-01 22:53  DukeLv  阅读(152)  评论(0编辑  收藏  举报