POJ 2528 Mayor's posters+zoj 1610 Count the Colors(数组记录)

来源:互联网 发布:艾克里里用什么软件 编辑:程序博客网 时间:2024/04/30 12:55

思路:

对于线段树区间覆盖的这种题。。我觉得我的办法也挺好,而且如果查询次数更多的话,我觉得线段树也不一定能过,这个办法只要n在10^5以下。。感觉10^5会死里。我觉得是可以过

目前至于到10^3 10^4

poj 2528

#include <iostream>#include <stdio.h>#include <cstring>using namespace std;int wall[10000001];int l[10005];int r[10005];int main(){    int T;    cin>>T;    while(T--)    {        memset(wall,0,sizeof(wall));        int n;        scanf("%d",&n);        int ans=0;        for(int i=1;i<=n;i++)            scanf("%d%d",&l[i],&r[i]);        for(int k=n;k>=1;k--)        {            int yes=0;            for(int i=l[k];i<=r[k];i++)            {                if(wall[i]!=0)                {                    i=wall[i];                }                else                {                    wall[i]=r[k];                    yes=1;                }            }            if(yes)                ans++;        }        cout<<ans<<endl;    }    return 0;}


zoj 1610

#include <stdio.h>#include <algorithm>#include <cstring>using namespace std;const int maxn=8005;int table[maxn];int color[maxn];int l[maxn];int r[maxn];int w[maxn];int ans[maxn];int main(){    int n;    while(~scanf("%d",&n))    {        memset(table,-1,sizeof(table));        memset(color,-1,sizeof(color));        memset(ans,0,sizeof(ans));        for(int i=1;i<=n;i++)        {            scanf("%d%d%d",&l[i],&r[i],&w[i]);            l[i]++;        }        for(int i=n;i>=1;i--)        {            int cnt=0;            for(int j=l[i];j<=r[i];)            {                if(table[j]==-1)                {                    color[j]  =w[i];                    table[j]=r[i];                    j++;                }                else                {                    j=table[j];                    j++;                        cnt++;                }            }            if(cnt>n/2)            {                for(int j=l[i];j<=r[i];j++)                {                    table[j]=r[i];                }            }        }        for(int i=1;i<maxn;i++)        {            if(color[i]!=color[i-1]&&color[i]!=-1)                ans[color[i]]++;        }        for(int i=0;i<=maxn;i++)            if(ans[i])            printf("%d %d\n",i,ans[i]);        printf("\n");    }}


0 0
原创粉丝点击