2557: Above the Median

时间限制: 1 Sec  内存限制: 64 MB
提交: 14  解决: 9
[提交] [状态] [命题人:admin]

题目描述


Farmer John has lined up his N (1 <= N <= 100,000) cows in a row to measure their heights; cow i has height H_i (1 <= H_i <= 1,000,000,000) nanometers--FJ believes in precise measurements! He wants to take a picture of some contiguous subsequence of the cows to submit to a bovine photography contest at the county fair.

The fair has a very strange rule about all submitted photos: a photograph is only valid to submit if it depicts a group of cows whose median height is at least a certain threshold X (1 <= X <= 1,000,000,000).

For purposes of this problem, we define the median of an array A[0...K] to be A[ceiling(K/2)] after A is sorted, where ceiling(K/2) gives K/2 rounded  up to the nearest integer (or K/2 itself, it K/2 is an integer to begin with). For example the median of {7, 3, 2, 6} is 6, and the median of {5, 4, 8} is 5.

Please help FJ count the number of different contiguous subsequences of his cows that he could potentially submit to the photography contest.

输入

* Line 1: Two space-separated integers: N and X.
* Lines 2..N+1: Line i+1 contains the single integer H_i.

输出

* Line 1: The number of subsequences of FJ's cows that have median at least X. Note this may not fit into a 32-bit integer.

样例输入

复制样例数据

4 6
10
5
6
2

样例输出

7

提示

FJ's four cows have heights 10, 5, 6, 2. We want to know how many contiguous subsequences have median at least 6.There are 10 possible contiguous subsequences to consider. Of these, only 7
have median at least 6. They are {10}, {6}, {10, 5}, {5, 6}, {6, 2}, {10,5, 6}, {10, 5, 6, 2}.

#include <bits/stdc++.h>
//#define scan(x) scanf("%d",&x);
//#define scan2(x,y) scanf("%d%d",&x,&y);
//#define rg register int
#define lowbit(x) x&(-x)
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const ll INF = 1e18;
const int M = 1e4 + 6;const int maxn = 6e5 + 10;struct BIT {ll n, c[maxn];inline void init(ll _n_) {n = _n_;memset(c, 0, sizeof(c));}inline void modify(int pos, ll val) {while (pos <= n) {c[pos] += val;pos += lowbit(pos);}}inline ll query(int pos) {ll res = 0;while (pos) {res += c[pos];pos -= lowbit(pos);}return res;}
} bit;int n, x, height[maxn];
int dp[maxn];int main() {
#ifndef ONLINE_JUDGEfreopen("splay.txt", "r", stdin);
#endifscanf("%d%d", &n, &x);for (register int i = 1; i <= n; ++i) {scanf("%d", &height[i]);dp[i] = dp[i - 1];dp[i] += (height[i] >= x);}ll res = 0;bit.init(6 * n + 8);bit.modify(n + 1, 1);for (register int i = 1; i <= n; ++i) {res += bit.query(2 * dp[i] - i + n + 1);bit.modify(2 * dp[i] - i + n + 1, 1);}printf("%lld\n", res);return 0;
}

2418: Dueling GPSs

时间限制: 1 Sec  内存限制: 64 MB
提交: 7  解决: 4
[提交] [状态] [命题人:admin]

题目描述

Farmer John has recently purchased a new car online, but in his haste he accidentally clicked the "Submit" button twice when selecting extra features for the car, and as a result the car ended up equipped with two GPS navigation systems!  Even worse, the two systems often make conflicting decisions about the route that FJ should take.

The map of the region in which FJ lives consists of N intersections (2 <= N <= 10,000) and M directional roads (1 <= M <= 50,000).  Road i connects intersections A_i (1 <= A_i <= N) and B_i (1 <= B_i <= N).  Multiple roads could connect the same pair of intersections, and a bi-directional road (one permitting two-way travel) is represented by two separate directional roads in opposite orientations.  FJ's house is located at intersection 1, and his farm is located at intersection N.  It is possible to reach the farm from his house by traveling along a series of directional roads.

Both GPS units are using the same underlying map as described above; however, they have different notions for the travel time along each road.  Road i takes P_i units of time to traverse according to the first GPS unit, and Q_i units of time to traverse according to the second unit (each travel time is an integer in the range 1..100,000).

FJ wants to travel from his house to the farm.  However, each GPS unit complains loudly any time FJ follows a road (say, from intersection X to intersection Y) that the GPS unit believes not to be part of a shortest route from X to the farm (it is even possible that both GPS units can complain, if FJ takes a road that neither unit likes).

Please help FJ determine the minimum possible number of total complaints he can receive if he chooses his route appropriately.  If both GPS units complain when FJ follows a road, this counts as +2 towards the total.

输入

* Line 1: The integers N and M.
Line i describes road i with four integers: A_i B_i P_i Q_i.

输出

* Line 1: The minimum total number of complaints FJ can receive if he routes himself from his house to the farm optimally.

样例输入

复制样例数据

5 7
3 4 7 1
1 3 2 20
1 4 17 18
4 5 25 3
1 2 10 1
3 5 4 14
2 4 6 5

样例输出

1

提示

There are 5 intersections and 7 directional roads.  The first road connects from intersection 3 to intersection 4; the first GPS thinks this road takes  7 units of time to traverse, and the second GPS thinks it takes 1 unit of time, etc.

If FJ follows the path 1 -> 2 -> 4 -> 5, then the first GPS complains on the 1 -> 2 road (it would prefer the 1 -> 3 road instead).  However, for the rest of the route 2 -> 4 -> 5, both GPSs are happy, since this is a shortest route from 2 to 5 according to each GPS.

#include <bits/stdc++.h>
//#define scan(x) scanf("%d",&x);
//#define scan2(x,y) scanf("%d%d",&x,&y);
//#define rg register int
#define lowbit(x) x&(-x)
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const ll INF = 1e18;
const int M = 1e4 + 6;const int maxn = 1e5 + 10;struct GPS_1 {int to, nx;ll val;
} f[maxn];struct GPS_2 {int to, nx;ll val;
} s[maxn];struct event {int to, nx;ll val;
} o[maxn];int n, m;
ll dis1[M], dis2[M], dis[maxn];int tot1, tot2, tot, head1[maxn], head2[maxn], head[maxn];
int vis[maxn];inline void add_edge1(int u, int v, ll w) {f[++tot1].to = v;f[tot1].val = w;f[tot1].nx = head1[u];head1[u] = tot1;
}inline void add_edge2(int u, int v, ll w) {s[++tot2].to = v;s[tot2].val = w;s[tot2].nx = head2[u];head2[u] = tot2;
}inline void add_edge(int u, int v, ll w) {o[++tot].to = v;o[tot].val = w;o[tot].nx = head[u];head[u] = tot;
}inline void spfa1() {for (register int i = 1; i <= n; ++i)dis1[i] = INT_MAX / 2;queue<int> q;q.push(n);dis1[n] = 0;while (!q.empty()) {int cur = q.front();q.pop();vis[cur] = 0;for (register int i = head1[cur]; i; i = f[i].nx) {int to = f[i].to;if (dis1[to] > dis1[cur] + f[i].val) {dis1[to] = dis1[cur] + f[i].val;if (!vis[to]) {q.push(to);vis[to] = 1;}}}}
}inline void spfa2() {for (register int i = 1; i <= n; ++i)dis2[i] = INT_MAX / 2, vis[i] = 0;queue<int> q;q.push(n);dis2[n] = 0;while (!q.empty()) {int cur = q.front();q.pop();vis[cur] = 0;for (register int i = head2[cur]; i; i = s[i].nx) {int to = s[i].to;if (dis2[to] > dis2[cur] + s[i].val) {dis2[to] = dis2[cur] + s[i].val;if (!vis[to]) {q.push(to);vis[to] = 1;}}}}
}inline void spfa() {for (register int i = 1; i <= n; ++i)dis[i] = INT_MAX / 2, vis[i] = 0;queue<int> q;q.push(1);dis[1] = 0;while (!q.empty()) {int cur = q.front();q.pop();vis[cur] = 0;for (register int i = head[cur]; i; i = o[i].nx) {int to = o[i].to;if (dis[to] > dis[cur] + o[i].val) {dis[to] = dis[cur] + o[i].val;if (!vis[to]) {q.push(to);vis[to] = 1;}}}}
}int main() {
#ifndef ONLINE_JUDGEfreopen("splay.txt", "r", stdin);
#endifscanf("%d%d", &n, &m);ll p,q;for (register int i = 1, a, b; i <= m; ++i) {scanf("%d%d%lld%lld", &a, &b, &p, &q);add_edge1(b, a, p);add_edge2(b, a, q);}spfa1();spfa2();//printf("debug dis1[1] = %lld dis[2] = %lld\n", dis1[1], dis2[1]);for (register int i = 1; i <= n; ++i) {for (register int j = head1[i]; j; j = f[j].nx) {int to = f[j].to;int init = 2;if (dis1[to] == dis1[i] + f[j].val)--init;if (dis2[to] == dis2[i] + s[j].val)--init;add_edge(to, i, init);}}spfa();printf("%lld\n", dis[n]);return 0;
}

转载于:https://www.cnblogs.com/czy-power/p/11516760.html

Above the MedianDueling GPSs相关推荐

  1. Greenplum【部署 04】GPSS扩展安装并使用GPKafka实现Kafka数据导入Greenplum数据库(安装包网盘分享)

    链接:https://pan.baidu.com/s/1MO-qL0Pxe6PojfZKsw3_qA 提取码:o7fl Greenplum Stream Server (GPSS)是一个ETL(提取. ...

  2. hive外部表改为内部表_3000字揭秘Greenplum的外部数据加载——外部表

    外部表是greenplum的一种数据表,它与普通表不同的地方是:外部表是用来访问存储在greenplum数据库之外的数据.如普通表一样,可使用SQL对外部表进行查询和插入操作.外部表主要用于Green ...

  3. 【 Notes 】Positioning system classification

    Positioning systems determine the location of a person or an object either relative to a known posit ...

  4. cdc工具 postgresql_零编码打造异构数据实时同步系统——异构数据源CDC之2

    前言: 本篇是<异构数据源的CDC实时同步系统>的续篇,继续介绍不同CDC的实际测试效果. <异构数据源的CDC实时同步系统> 系列第一篇 (已完成)<零编码打造异构数据 ...

  5. Greenplum【环境搭建 04】使用GPKafka实现Kafka数据导入Greenplum数据库(扩展安装文件网盘分享)

    分享资源地址及文件列表: 链接:https://pan.baidu.com/s/1XVTxKLkOYrL4pCZpFfs-Tg 提取码:sq90 包含文件: # 命令执行 gpkafka # 扩展安装 ...

  6. 打开AzureRay园子的大门,欢迎大家串门哟~

    Duang,Duang,Duang!   作为一个博客园的小白,终于鼓起勇气写第一篇博客.虽然自己的园子还没有时间装修,与其说是没有时间,其实是不敢轻易下手去做这样一件"神圣"的事 ...

  7. centos配置ntp时间同步_Linux CentOS配置ntpd时间同步

    脚本在CentOS7上测试OK.可以直接使用. yum -y install ntp ntpdatecp /usr/share/zoneinfo/Asia/Shanghai /etc/localtim ...

  8. hive内部表和外部表的区别_3000字揭秘Greenplum的外部数据加载——外部表

    外部表是greenplum的一种数据表,它与普通表不同的地方是:外部表是用来访问存储在greenplum数据库之外的数据.如普通表一样,可使用SQL对外部表进行查询和插入操作.外部表主要用于Green ...

  9. android 如何去获取手机Gps的信号强度

    1,看到别人写的app里面有展示Gps信号强度的功能,我们的app也需要这个功能,我是先百度了一圈(没有发现要找到的内同,百度果然有些东西搜索不到),还是翻墙去的谷歌看到了,你也可以先去应用市场里面下 ...

最新文章

  1. git使用变基方式同步远程和本地副本的代码同步方式
  2. C语言高级技巧-在Makefile中引用你的头文件
  3. pageSet还没完count就执行了
  4. phabricator mysql_Phabricator服务的搭建
  5. 蓝桥杯2021年第十二届C++省赛第九题-双向排序
  6. ubuntu国内镜像站点及更新源
  7. .NET进销存系统开发笔记------之Gridview应用
  8. cartopy模块介绍与安装
  9. ACTIVEX控件debug版本在Win7下注册失败的处理方法
  10. jquery prop(“outerHTML“) 获取当前标签和标签内部的html 代码
  11. R语言︱机器学习模型评估方案(以随机森林算法为例)
  12. oracle pq distribute,详解Oracle hints PQ_DISTRIBUTE
  13. 家的温暖社会实践报告
  14. 太赞了!微软《dotnet中文手册》火了,完整PDF开放下载!
  15. 专心致志求精进——给自己的生日祝福
  16. BICC呼叫建立过程
  17. AutoMapper源码解析
  18. 解决Warning: NEWFF used in an obsolete way. See help for NEWFF to update calls to the new argument li
  19. 深入理解类加载机制:拨开迷雾见真章
  20. 质量管理体系认证的标准

热门文章

  1. 我与程序员不得不说的二三事——一天一天
  2. 本题要求实现一个函数,对给定平面任意两点坐标(x 1​ ,y 1​ )和(x 2​ ,y 2​ ),求这两点之间的距离。
  3. ROS2之OpenCV基础代码对比foxy~galactic~humble
  4. PLM系统具体是做什么的呢?
  5. 【商业分析 01】商业分析网站汇总
  6. python就业需要的技能_教你如何快速掌握Python就业技能
  7. c语言程序设计植树,c语言程序设计报告
  8. python中的递归函数如何表示_python:递归函数
  9. 后台管理系统色系搭配推荐
  10. 图片转码 webp 转 png、jpg