POJ 2352 Stars
来源:互联网 发布:ios md5算法原理 编辑:程序博客网 时间:2024/06/05 07:53
题意:计算星星的等级。
星星的等级是由有多少 x 坐标小于它并且 y 坐标也小于它的星星的数量决定的。
由于输入已经是按照 先y从小到大,再x从小到大的顺序排列好了的。
所以 只需要按输入顺序插入,然后统计比当前x小的有多少个即可。
只需要一个logn的修改和查询的数据结构就可以了。
以前用数状数组写过,现在再补一个线段树的。
线段树:
#include<cstdio>#include<cstring>#include<string>#include<queue>#include<algorithm>#include<map>#include<stack>#include<iostream>#include<list>#include<set>#include<bitset>#include<vector>#include<cmath>#define INF 0x7fffffff#define eps 1e-8#define LL long long#define PI 3.141592654#define CLR(a,b) memset(a,b,sizeof(a))#define FOR(i,a,b) for(int i=a;i<b;i++)#define FOR_(i,a,b) for(int i=a;i>=b;i--)#define sf scanf#define pf printf#define all(v) (v).begin(),(v).end()#define acfun std::ios::sync_with_stdio(false)#define SIZE (32000 +2)#define MOD 1000000007using namespace std;int a[SIZE*4];int up;void update(int l,int r,int o){ if(l==r) a[o]++; else { int m=(l+r)>>1; if(up<=m)update(l,m,o*2); else update(m+1,r,o*2+1); a[o]=a[o*2]+a[o*2+1]; }}int ql,qr;int query(int l,int r,int o){ if(l>=ql&&r<=qr)return a[o]; int m=(l+r)>>1; int ans=0; if(ql<=m)ans+=query(l,m,o*2); if(qr>m)ans+=query(m+1,r,o*2+1); return ans;}int level[SIZE];int main(){ int n; while(sf("%d",&n)!=EOF) { CLR(a,0); CLR(level,0); FOR(i,0,n) { int x,y; sf("%d%d",&x,&y); up=x+1; update(1,SIZE,1); ql=1,qr=up; int tmp=query(1,SIZE,1); level[tmp]++; } FOR(i,1,n+1) pf("%d\n",level[i]); }}
数状数组:
C语言的。
#include<stdio.h>#include<string.h>int n,c[32002];int lowbit(int x){ return x&(-x);}void add (int x){ while(x<=32002) { c[x]++; x += lowbit(x); }}int sum(int x){ int s = 0; while(x>0) { s += c[x]; x -= lowbit(x); } return s;}int main(){ int i = 0, x = 0, y = 0, level[15002]; while(scanf("%d",&n) != EOF) { memset(level,0,sizeof(level)); memset(c,0,sizeof(level)); for(i = 0; i<n; i++) { scanf("%d %d", &x, &y); x++; level[sum(x)]++; add(x); } for(i = 0; i<n; i++) printf("%d\n",level[i]); } return 0;}
0 0
- POJ 2352 STARS
- POJ 2352 Stars
- Poj 2352 Stars
- poj 2352 Stars
- poj 2352 Stars
- poj 2352 Stars
- POJ 2352 Stars
- POJ 2352 Stars
- POJ-2352-Stars
- POJ 2352 Stars
- Poj 2352 Stars 题解
- poj 2352 stars
- POJ 2352 stars
- POJ 2352 - Stars
- POJ 2352 Stars
- POJ 2352 Stars
- poj 2352 Stars
- poj 2352 Stars
- NYOJ 1165 just a problem【计算几何】
- 4 Activity & Fragment 的生命周期
- iOS各种profile文件
- Java 7并发编程实战手册
- java工程积累——加密hibernate数据源连接符
- POJ 2352 Stars
- Matrix Chain Multiplication UVA442 矩阵连乘 stack
- eui 解释
- 区分 int,NSInteger,NSUInteger,NSNumber
- textarea 高度自适应(chrome+ie8测试有效)
- Apache+tomcat实现高可用WEB集群----简介
- 刚学Android遇到的问题 findViewById值为null(新版本)
- java多线程下载美女图片
- Linux网络概述