zoukankan      html  css  js  c++  java
  • Vijos P1066 弱弱的战壕【多解,线段树,暴力,树状数组】

    弱弱的战壕

    描述

    永恒和mx正在玩一个即时战略游戏,名字嘛~~~~~~恕本人记性不好,忘了-_-b。

    mx在他的基地附近建立了n个战壕,每个战壕都是一个独立的作战单位,射程可以达到无限(“mx不赢定了?!?”永恒ftING...@_@)。

    但是,战壕有一个弱点,就是只能攻击它的左下方,说白了就是横纵坐标都不大于它的点(mx:“我的战壕为什么这么菜”ToT)。这样,永恒就可以从别的地方进攻摧毁战壕,从而消灭mx的部队。

    战壕都有一个保护范围,同它的攻击范围一样,它可以保护处在它左下方的战壕。所有处于它保护范围的战壕都叫做它的保护对象。这样,永恒就必须找到mx的战壕中保护对象最多的点,从而优先消灭它。

    现在,由于永恒没有时间来计算,所以拜托你来完成这个任务:

    给出这n个战壕的坐标xi、yi,要你求出保护对象个数为0,1,2……n-1的战壕的个数。

    格式

    输入格式

    第一行,一个正整数n(1<=n<=15000)
    接下来n行,每行两个数xi,yi,代表第i个点的坐标
    (1<=xi,yi<=32000)
    注意:可能包含多重战壕的情况(即有数个点在同一坐标)

    输出格式

    输出n行,分别代表保护对象为0,1,2……n-1的战壕的个数。

    样例1

    样例输入1

    5
    1 1
    5 1
    7 1
    3 3
    5 5
    

    样例输出1

    1
    2
    1
    1
    0
    

    限制

    各点2s(算是宽限吧^_^)

    来源

    URAL1028战役版

    题目链接:https://vijos.org/p/1066

    分析:先给出我线段树写法吧,XX了几个小时,一行代码,三个错误,我也是无语了!好不容易过了样例,啪啪啪,打脸了,才过了三组数据,查了半天只要改了右区间就会过qwq!

    此题是要我们求每次有一个新的点加入时,比它小的y有几个,因为,先对X从小到大排序以后,每次加入一个y,之前比它小的y保证x和y都小于它,个数就是这个星星的等级这就可以这样,一个1-32000的线段,每次加入一个y,加入了一个[y,32000]的线段,然后每次看比y少的点有几个,就是它当前被压在几条线段下!

    下面给出线段树写法:

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int N=16666;
     4 int ans[N<<1];
     5 struct Node
     6 {
     7     int l,r,sum;
     8 }tree[N<<4];
     9 struct data
    10 {
    11     int x,y;
    12 }f[N<<1];
    13 inline bool cmp(data a,data b)
    14 {
    15     return (a.x==b.x)?(a.y<b.y):(a.x<b.x);
    16 }
    17 void buildtree(int l,int r,int pos)
    18 {
    19     tree[pos].sum=0;
    20     tree[pos].l=l;
    21     tree[pos].r=r;
    22     if(l==r)
    23         return;
    24     int mid=(l+r)/2;
    25     buildtree(l,mid,pos*2);
    26     buildtree(mid+1,r,pos*2+1);
    27 }
    28 void update(int st,int ed,int pos)
    29 {
    30     if(tree[pos].l==st&&tree[pos].r==ed)
    31     {
    32         tree[pos].sum++;
    33         return;
    34     }
    35     if(tree[pos].l==tree[pos].r)
    36         return;
    37     int mid=(tree[pos].l+tree[pos].r)/2;
    38     if(mid>=ed)
    39         update(st,ed,pos*2);
    40     else if(st>mid)
    41         update(st,ed,pos*2+1);
    42     else
    43     {
    44         update(st,mid,pos*2);
    45         update(mid+1,ed,pos*2+1);
    46     }
    47 }
    48 int query(int c,int pos)
    49 {
    50     if(tree[pos].l==tree[pos].r)
    51         return tree[pos].sum;
    52     int mid=(tree[pos].l+tree[pos].r)/2;
    53     if(c<=mid)
    54         return tree[pos].sum+query(c,pos*2);
    55     else return tree[pos].sum+query(c,pos*2+1);
    56 }
    57 int main()
    58 {
    59     int n;
    60     cin>>n;
    61     buildtree(0,32001,1);
    62     for(int i=0;i<n;i++)
    63         scanf("%d%d",&f[i].x,&f[i].y);
    64     sort(f,f+n,cmp);
    65     for(int i=0;i<n;i++)
    66     {
    67         ans[query(f[i].y,1)]++;
    68         update(f[i].y,32001,1);
    69     }
    70     for(int i=0;i<n;i++)
    71         cout<<ans[i]<<endl;
    72     return 0;
    73 }

    此题数据太弱,所以用暴力同样可以解决问题!

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int N=16666;
     4 struct Node
     5 {
     6     int x,y;
     7 }p[N<<1];
     8 int n,ans[N<<1];
     9 int main()
    10 {
    11     cin>>n;
    12     for(int i=0;i<n;i++)
    13         cin>>p[i].x>>p[i].y;
    14     for(int i=0;i<n;i++)
    15     {
    16         int tot=0;
    17         for(int j=0;j<n;j++)
    18         {
    19             if(i==j)
    20                 continue;
    21             else if(p[i].x>=p[j].x&&p[i].y>=p[j].y)
    22                 tot++;
    23         }
    24         ans[tot]++;
    25     }
    26     for(int i=0;i<n;i++)
    27         cout<<ans[i]<<endl;
    28     return 0;
    29 }

    其实呢,此题貌似用树状数组也行啊,只能说数据太水qwq!

    树状数组,对X排下序之后以每个点的y进行操作。如果在某次算出sum(y)之后,肯定就是他的等级。因为在它之前的点都是x比它小或者和它相等,但是y没他大的。

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int N=16666;
     4 struct Node
     5 {
     6     int x,y;
     7 }p[N<<1];
     8 int n,c[N<<1],ans[N<<1];
     9 int lowbit(int x)
    10 {
    11     return x&-x;
    12 }
    13 inline bool cmp(Node p,Node q)
    14 {
    15     return p.x==q.x?(p.y<q.y):(p.x<q.x);
    16 }
    17 int sum(int x)
    18 {
    19     int ret=0;
    20     while(x>0)
    21     {
    22         ret+=c[x];
    23         x-=lowbit(x);
    24     }
    25     return ret;
    26 }
    27 void add(int x)
    28 {
    29     while(x<=32001)
    30     {
    31         c[x]++;
    32         x+=lowbit(x);
    33     }
    34 }
    35 int main()
    36 {
    37     cin>>n;
    38     for(int i=1;i<=n;i++)
    39         cin>>p[i].x>>p[i].y;
    40     sort(p+1,p+1+n,cmp);
    41     for(int i=1;i<=n;i++)
    42     {
    43         ans[sum(p[i].y)]++;
    44         add(p[i].y);
    45     }
    46     for(int i=0;i<n;i++)
    47         cout<<ans[i]<<endl;
    48     return 0;
    49 }
  • 相关阅读:
    混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集。
    SQL中获取自增长的最大ID
    (inline)内联函数在IOS开发中的使用
    MS SQL SERVER 2005 高可用性之日志传送
    19_toast通知和notify通知 onTouch事件响应
    20 按比例设置 子控件的宽度和高度
    18_SurfaceView 其他线程绘图
    使用Microsoft Media Service实现网络影音多媒体应用系列第三篇技术要点
    使用Microsoft Media Service实现网络影音多媒体应用系列第二篇开发须知
    MVC3WIN7下的IIS7.5部署MVC3应用程序
  • 原文地址:https://www.cnblogs.com/ECJTUACM-873284962/p/7041534.html
Copyright © 2011-2022 走看看