传送门

Solution:

记一颗流星在视野内的时间段为(L, R),

为了使所有(L, R)都取整数,首先将坐标放大。

放大倍数可取为

    LCM(1, 2, ..., 10)= 2520

接着计算:从0时刻开始,每单位时间内入画的流星数,即数组:

  a[0], ..., a[M]

显然这可借助线段树,但难免繁琐。

考虑到这道题区间操作的特殊性:每次对区间[L, R)内的元素加一,用差分序列更方便。

数组a[0...M]的差分序列d[0...M]定义为:

  d[0]=a[0],

  d[i]=a[i]-a[i-1], i>0

对于这道题,我们只要维护数组a[ ]的差分序列d[ ]即可。

------------------------------------------------------------------------------------

Implementation:

当然可以先离散化,但下面的实现用map (also called "asscociated array") 避免离散化:

#include <bits/stdc++.h>
using namespace std; const int N=1e5+;
const int INF=0x3f3f3f3f; int x[N], y[N], a[N], b[N], t[N][];
int n, w, h; map<int,int> mp;

//! Repeating
void get_t(int i){
int tx1, tx2, ty1, ty2;
if(a[i]==){
if(x[i]> && x[i]<w){
tx1=;
tx2=INF;
}
else{
tx1=tx2=;
}
}
else{
tx1=-x[i]/a[i];
tx2=(w-x[i])/a[i];
}
if(b[i]==){
if(y[i]> && y[i]<h){
ty1=;
ty2=INF;
}
else{
ty1=ty2=;
}
}
else{
ty1=-y[i]/b[i];
ty2=(h-y[i])/b[i];
}
t[i][]=max(min(tx1, tx2), min(ty1, ty2));
t[i][]=min(max(tx1, tx2), max(ty1, ty2));
t[i][]=max(t[i][], );
if(t[i][]>t[i][]){
mp[t[i][]+]++;
mp[t[i][]]--;
}
} int main(){
int T;
for(cin>>T; T--; mp.clear()){
cin>>w>>h>>n;
w*=;
h*=;
mp[];
for(int i=; i<n; i++){
cin>>x[i]>>y[i]>>a[i]>>b[i];
x[i]*=, y[i]*=;
get_t(i);
}
// As with any other type specifier, we can define mutiple variables using auto.
// Because a declaratin can involve only a single base type, the initializers for all the
// variables in the declaration must have types that are consistent with each other.
int ans =;
     // tedious
auto i=mp.begin(), j=i;
++j; for(; j!=mp.end(); ++i, ++j){
j->second+=i->second;
ans=max(ans, j->second);
}
cout << ans <<'\n';
} return ;
}

代码写得很差劲:

  •   重复,不符合 Keep DRY (Don't Repeat Yourself)原则
  • 所有的数组其实都不需要开
  • 计算前项和(prefix sum)过于繁琐,显得很笨 (dull)

---------------------------------------------------------------------------------------

总之丑得看不下去了,代码写成这样,和咸鱼有什么区别。。。。。

Enhanced:

#include <bits/stdc++.h>
using namespace std; const int N=1e5+;
const int INF=0x3f3f3f3f;
int x, y, a, b;
int n, w, h; map<int,int> mp; void get_t(int &L, int &R, int v, int p, int b){
if(v){
L=max(L, min(-p/v, (b-p)/v));
R=min(R, max(-p/v, (b-p)/v));
}
else if(p<= || p>=b) R=L;
} int main(){
ios::sync_with_stdio(false); int T;
for(cin>>T; T--; mp.clear()){
cin>>w>>h>>n;
w*=;
h*=; for(int i=, L, R; i<n; i++){
cin>>x>>y>>a>>b;
x*=, y*=;
L=, R=INF;
get_t(L, R, a, x, w);
get_t(L, R, b, y, h); if(R>L)
mp[L]++, mp[R]--;
}
int ans =, sum=; for(auto i:mp){
sum+=i.second;
ans=max(sum, ans);
}
cout << ans <<'\n';
}
return ;
}

-----------------------------------------------

还有一种更为美妙的实现:

#include <bits/stdc++.h>
using namespace std; const int N=1e5+;
const int INF=0x3f3f3f3f;
int x, y, a, b;
int n, w, h; vector<pair<int,int>> vec; //Don't repeat yourself. (keep DRY) void get_t(int &L, int &R, int v, int p, int b){
if(v){
L=max(L, min(-p/v, (b-p)/v));
R=min(R, max(-p/v, (b-p)/v));
}
else if(p<= || p>=b) R=L;
} int main(){
ios::sync_with_stdio(false); int T;
for(cin>>T; T--; ){
cin>>w>>h>>n;
w*=;
h*=; vec.clear(); for(int i=, L, R; i<n; i++){
cin>>x>>y>>a>>b;
x*=, y*=;
L=, R=INF;
get_t(L, R, a, x, w);
get_t(L, R, b, y, h);
if(R>L){ //error-prone
vec.push_back({L, });
vec.push_back({R, -});
}
}
sort(vec.begin(), vec.end());
int ans =, sum=; for(auto i:vec){
sum+=i.second;
ans=max(sum, ans);
}
cout << ans <<'\n';
} return ;
}

言不尽意,读者自己欣赏吧。。。。。。。。。

UVA 1398 Meteor的更多相关文章

  1. 【译】Meteor 新手教程:在排行榜上添加新特性

    原文:http://danneu.com/posts/6-meteor-tutorial-for-fellow-noobs-adding-features-to-the-leaderboard-dem ...

  2. Using View and Data API with Meteor

    By Daniel Du I have been studying Meteor these days, and find that Meteor is really a mind-blowing f ...

  3. POJ 3669 Meteor Shower【BFS】

    POJ 3669 去看流星雨,不料流星掉下来会砸毁上下左右中五个点.每个流星掉下的位置和时间都不同,求能否活命,如果能活命,最短的逃跑时间是多少? 思路:对流星雨排序,然后将地图的每个点的值设为该点最 ...

  4. 如何在Meteor中使用npm模块?

    首先,请在AtmosphereJs上搜索有无相关的封装包.尽量采用已有的封装包,而不是自己封装. 有两种方法在项目中使用来自npm的模块. 封装为Meteor包并在项目中添加包.使用meteor cr ...

  5. windows下Meteor+AngularJS开发的坑

    有复杂的地方我再开贴记录,这里只记录容易解决的坑. 1. windows下手工增加smart package.直接将下载下来的包扔到meteor package中.记得将文件夹名字改得和smart.j ...

  6. Meteor + node-imap(nodejs) + mailparser(nodejs) 实现完整收发邮件

    版本信息: Meteor:windows MIS安装  0.6.4 node-imap:npm指定的0.8.0版,不是默认的0.7.x版. mailparser:npm安装0.3.6 以下是记录踩到的 ...

  7. 手工给Meteor增加smart package的方法

    windows下无法装mrt(Meteor的包管理工具).不过还好smart package本身也就只是一个文件夹而已,不需要在Meteor中注册什么东西.所以直接把smart package扔到me ...

  8. Meteor+AngularJS:超快速Web开发

        为了更好地描述Meteor和AngularJS为什么值得一谈,我先从个人角度来回顾一下这三年来WEB开发的变化:     三年前,我已经开始尝试前后端分离,后端使用php的轻量业务逻辑框架.但 ...

  9. uva 1354 Mobile Computing ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5

随机推荐

  1. Android安全机制(2) Android Permission权限控制机制

    http://blog.csdn.net/vshuang/article/details/44001661 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 1.概述 Andro ...

  2. 传递消息--第三方开源--EventBus的简单使用

    EventBus下载地址:https://github.com/greenrobot/EventBus MyEvent: package com.zzw.testeventbus; public cl ...

  3. leetcode总结:permutations, permutations II, next permutation, permutation sequence

    Next Permutation: Implement next permutation, which rearranges numbers into the lexicographically ne ...

  4. LeetCode:Convert Sorted Array to Binary Search Tree,Convert Sorted List to Binary Search Tree

    LeetCode:Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in asce ...

  5. 信息安全系统设计基础第四次实验报告20135324&&20135330

    课程:信息安全系统设计基础 班级:1353 姓名:杨舒雯 张若嘉 学号:20135324,20135330 实验日期:2015.11.17 15:30-17:30 实验名称:外设驱动程序设计 实验目的 ...

  6. iOS从App跳转至系统设置菜单各功能项的编写方法讲解

    跳到系统设置里的WiFi界面 info里面设置: 在项目中的info.plist中添加 URL types 并设置一项URL Schemes为prefs,如下图 代码: 复制代码 代码如下: NSUR ...

  7. IOS-一步一步教你自定义评分星级条RatingBar ——转载的

    由于项目的需要,需要设计能评分.能显示评分数据的星级评分条,但是IOS上好像没有这个控件,Android是有RatingBar这个控件的(又发现一个IOS不如Android好的),那就只能自定义了,在 ...

  8. canvas学习笔记:小小滴公式,大大滴乐趣

    声明:本文为原创文章,如需转载,请注明来源WAxes,谢谢! 最近想弄一个网页,把自己学HTML5过程中做的部分DEMO放上去做集合,但是,如果就仅仅做个网页把所有DEMO一个一个排列又觉得太难看了. ...

  9. Cocos2d-x中使用OpenGL ES2.0编写shader

    这几天在看子龙山人的关于OpenGL的文章,先依葫芦画瓢,能看到些东西,才能慢慢深入了解,当入门文章不错,但是其中遇到的一些问题,折腾了一些时间,为了方便和我一样的小白们,在这篇文章中进行写补充. O ...

  10. #Linux学习笔记# Linux系统查看文件内容的命令

    1.cat 连结多个文件的内容并显示在屏幕上:如果没有指定文件或文件名为“-”,则读取标准输入.语法如下: cat [option] ... [file] ... 常用的选项有: 选项-n:编号所有行 ...