跳过正文
  1. Posts/

BZOJ 1635: [Usaco2007 Jan]Tallest Cow 最高的牛 (差分序列(前缀和的逆))

·2 分钟
目录

1635: [Usaco2007 Jan]Tallest Cow 最高的牛
#

Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 472  Solved: 278 [Submit][Status][Discuss]

Description
#

FJ’s N (1 <= N <= 10,000) cows conveniently indexed 1..N are standing in a line. Each cow has a positive integer height (which is a bit of secret). You are told only the height H (1 <= H <= 1,000,000) of the tallest cow along with the index I of that cow. FJ has made a list of R (0 <= R <= 10,000) lines of the form “cow 17 sees cow 34”. This means that cow 34 is at least as tall as cow 17, and that every cow between 17 and 34 has a height that is strictly smaller than that of cow 17. For each cow from 1..N, determine its maximum possible height, such that all of the information given is still correct. It is guaranteed that it is possible to satisfy all the constraints.

有n(1 <= n <= 10000)头牛从1到n线性排列,每头牛的高度为h[i](1 <= i <= n),现在告诉你这里面的牛的最大高度为maxH,而且有r组关系,每组关系输入两个数字,假设为a和b,表示第a头牛能看到第b头牛,能看到的条件是a, b之间的其它牛的高度都严格小于min(h[a], h[b]),而h[b] >= h[a]

Input
#

  • Line 1: Four space-separated integers: N, I, H and R

  • Lines 2..R+1: Two distinct space-separated integers A and B (1 <= A, B <= N), indicating that cow A can see cow B.

Output
#

  • Lines 1..N: Line i contains the maximum possible height of cow i.

Sample Input
#

9 3 5 5 1 3 5 3 4 3 3 7 9 8

INPUT DETAILS:

There are 9 cows, and the 3rd is the tallest with height 5.

Sample Output
#

5 4 5 3 4 4 5 5 5

差分序列大概就是前缀和的逆过程…?

这道题对于一堆关系(a,b) 要使得【a+1,b-1】中每个都严格小于a,b,但是又要尽可能大,所以令每个[a+1,b-1]每个数-1. 之前其实遇到过,以为是前缀和的第二种应用。但确切来说其实是前缀和的逆过程。。。叫差分序列?

注意去重。对于完全相同的,做一遍就好啦。所以要先排个序

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年04月07日 星期四 22时52分44秒
 4File Name :code/bzoj/1635.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=1E4+7;
34int n,i,mxh,r;
35int p[N];
36
37struct node
38{
39    int a,b;
40
41    bool operator < (node y)const
42    {
43	if (a==y.a) return b<y.b;
44	return a<y.a;
45    }
46}q[N];
47int main()
48{
49	#ifndef  ONLINE_JUDGE
50	freopen("code/in.txt","r",stdin);
51  #endif
52
53	ios::sync_with_stdio(false);
54	cin>>n>>i>>mxh>>r;
55	ms(p,0);
56	for  ( int i = 1 ; i <= r ; i++)
57	{
58	    cin>>q[i].a>>q[i].b;
59	    if (q[i].a>q[i].b) swap(q[i].a,q[i].b);
60	}
61	sort(q+1,q+r+1);
62	for ( int i = 1 ;i <= r; i++)
63	{
64	    int a = q[i].a;
65	    int b = q[i].b;
66	    if (a==q[i-1].a&&b==q[i-1].b) continue;  //判重
67	    p[a+1]--;
68	    p[b]++;
69
70	}
71	for ( int i = 1 ; i <= n ; i++) p[i] = p[i] + p[i-1];
72	for ( int i = 1 ; i <= n ; i++) cout<<mxh+p[i]<<endl;
73  #ifndef ONLINE_JUDGE
74  fclose(stdin);
75  #endif
76    return 0;
77}

相关文章

前缀和问题总结

·2 分钟
There are two types of problems solvable by partial sum. 1.Problems which you are asked to answer some queries about the sum of a part of elements (without modify queries).

hdu 5416 CRB and Tree ( 2015 多校 #10 )

·2 分钟
http://acm.hdu.edu.cn/showproblem.php?pid=5416 # 题意:给出一棵树(n<=1E5),定义二元函数函数f(u,v) (u可以等于v)表示节点u到节点v经过的路径的权值的异或和。给出q组查询(q<=10),每组一个s,问有多少对无序点对(u,v)满足f(u,v)=s. 思路:类似codeforces #340 div 2 E XOR and Favorite Number 先dfs,处理出从根节点都任意节点的异或前缀和。然后对于每个询问o(n)扫一遍,统计sum[i]^s出现多少次。 总的时间复杂度为O(Tqn);

hdu 5327 Olympiad (2015 多校 #4 )

·1 分钟
http://acm.hdu.edu.cn/showproblem.php?pid=5327 题意:问给出的区间[a,b]中有多少个美丽数,美丽数的定义是所有数字都不相同,如123是,100不是,333也不是。 思路:预处理1..100000的美丽数,可以把每个数字拆开放在set里,比较set的size和位数来实现。 然后用前缀和。

cf 611 A||codeforces goodbye 2015 C. New Year and Domino

·1 分钟
http://codeforces.com/contest/611/problem/C 题意:给出一个n*m的地图,.表示可以空,#表示墙。一个东西需要占两个相邻的格子,问给定一个矩形,放一个东西的方案数。 思路:q很大。。应该是先预处理出来直接调用答案。。。计数问题累加性。。应该是前缀和之类。。需要做的就是怎么标记。。我的做法是竖着放和横着放的个数分开来存。从左往右从上往下,每次标记到后一个点。然后二维的前缀和。然后每次询问的时候,去掉最上边和最左边两条边界上对应的多加的点。