码迷,mamicode.com
首页 > 其他好文 > 详细

Above the Median&Dueling GPSs

时间:2019-09-13 15:49:29      阅读:88      评论:0      收藏:0      [点我收藏+]

标签:nec   tar   only   submit   imu   contain   problem   数据   owb   

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_JUDGE
    freopen("splay.txt", "r", stdin);
#endif

    scanf("%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_JUDGE
    freopen("splay.txt", "r", stdin);
#endif
    scanf("%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;
}

 

Above the Median&Dueling GPSs

标签:nec   tar   only   submit   imu   contain   problem   数据   owb   

原文地址:https://www.cnblogs.com/czy-power/p/11516760.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!