首页 > 其他分享 >Tallest Cow(最高的牛)poj3263

Tallest Cow(最高的牛)poj3263

时间:2023-05-24 13:45:05浏览次数:41  
标签:Cow cow int 前缀 d% 高度 height Tallest poj3263

题目描述:
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.


输入格式:
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.


*输出格式:
Lines 1..N: Line i contains the maximum possible height of cow i.


输入样例:
9 3 5 5
1 3
5 3
4 3
3 7
9 8


输出样例:
5
4
5
3
4
4
5
5
5

翻译中文:

 

问题分析:

即在一堆数量为N的牛中,只告诉了你两个值,一个是最高的牛的高,还有他所处的下标,然后有M次记录,每次记录中都有A B两头牛能够互看到对方,不过B的高是>=A的,而AB中间的牛的高都是至少小1于A与B两者的min高的,通过分析,我们可以画出草图发现

图二

   假若是第一张图这种重合的情况,那是不成立的,因为如果这样就出现矛盾了。 所以就只有图二这种情况。

我们不妨设每头牛的高度都为最高的那头牛的高度即H,用一个前缀和数组来存每头牛的高度,然后再M次记录中再对AB之间的牛用前缀和的性质进行修改

例如假设前缀和数组为height[] 一开始让height[1] = H,则可以让后面的牛的高度都为H ,而在M次记录中,想要让AB之间的牛高度-1,那么就只需要说让height[A + 1] -- 和height[B] ++即可,最后问题的输出是输出当前序列每天牛的最大可能高度,所以可以发现上面确实中间的牛的高度最多也就只能是-1的 

代码如下:

点击此处看代码

#include <iostream>
#include <set>
using namespace std;

const int N = 100010;
int height[N];//存放高度的前缀和数组
set<pair<int,int>> existed;//用来判断关系对是否重复

int main()
{
    int N, P, H, M;
    scanf("%d%d%d%d", &N, &P, &H, &M);
    height[1] = H;//让前缀和数组后面的高度都为H
    for(int i = 0; i < M; i ++ )
    {
        int A, B;
        scanf("%d%d", &A, &B);
        if(A > B) swap(A, B);//要确保A的高<=B的高
        
        if(!existed.count({A,B}))//假若之前没有判断此对关系对就执行下面
        {
            existed.insert({A,B});//将关系对插进来 并执行下面操作
            //要让A
            height[A + 1] -- , height[B] ++;
        }
    }
    
    for(int i = 1; i <= N; i ++ )//求差分数组前缀和 即每头牛可能的身高为多少
    {
        height[i] += height[i - 1];
        printf("%d\n", height[i]);
    }
    
    return 0;
}

标签:Cow,cow,int,前缀,d%,高度,height,Tallest,poj3263
From: https://www.cnblogs.com/FISH-Q/p/17428059.html

相关文章

  • P1676 [USACO05FEB] Aggressive cows G 题解
    题目传送门解题思路最大值最小化问题,考虑二分答案。首先要排序,保证序列单调不降,然后求出两个隔间之间的距离。sort(a+1,a+1+n);for(rii=1;i<=n;i++) dis[i]=a[i+1]-a[i];二分出一个\(mid\),判断它是否合法:每次累加距离,如果距离和比\(mid\)大,说明当前可以分配牛,记录数量......
  • NC51101 Lost Cows
    题目链接题目题目描述\(N(2\leqN\leq8,000)\)cowshaveuniquebrandsintherange1..N.Inaspectaculardisplayofpoorjudgment,theyvisitedtheneighborhood'wateringhole'anddrankafewtoomanybeersbeforedinner.Whenitwastimetoli......
  • P1345 [USACO5.4]奶牛的电信Telecowmunication 题解
    一、题目描述:n个点,m条边,给定起点s和终点t,求最少删去几个点后,s和t不连通。注意,s和t不能删掉。1<=n<=100,1<=m<=600; 二、解题思路:刚刚学了最大费用流,知道最大流等于最小割。但此题割的不是边,是点。我们需要将将割点转化为割边。把一个点切成两半......
  • 【题解】XX Open Cup, GP of Moscow
    //createdon23.03.26目录A.AliceandBobB.BracketsC.CirclesD.DejaVuE.EasiestSumF.FunnySalesmanG.GraphColoringH.HiddenGraphI.InsectsJ.JoiningPointsA.AliceandBob对于链上的情况,异色点是一定不会选择走进同色段的(长度不小于\(2\)),因为一定不优。......
  • Codeforces Round #225 (Div. 2) C. Milking cows Greedy
    Iahubhelpshisgrandfatheratthefarm.Todayhemustmilkthecows.Therearencowssittinginarow,numberedfrom1tonfromlefttoright.Eachcowiseitherfacingtotheleftorfacingtotheright.WhenIahubmilksacow,allthecowsthatseet......
  • Hungry Cow(USACO23 FEB Bronze T1)
    题目: 来写周练了,这道题目开开胃,就只用遍历一遍b数组、d数组再加上一些特判即可程序:#include<bits/stdc++.h>usingnamespacestd;constintN=1e5+10;longlongn,t,d[N],b[N];intmain(){ios::sync_with_stdio(false);cin>>n>>t;for(inti=1;i<=n;i++)......
  • 3分钟了解Hudi数据表类型——COW和MOR
    COW(Copy-On-Write)和MRO(Merge-On-Read)是Hudi中两种不同类型的表,它们的主要区别在于读写操作的性能以及内存占用。1.COW(Copy-On-Write)COW表是在写入操作时进行复制的表,每次写入操作都会创建一个新的COW表,并将原表覆盖。COW表的主要优点是可以减少内存占用和提高写入......
  • 题解 P9130 【[USACO23FEB] Hungry Cow P】
    赛时开始一眼线段树分治,交了几发都T了,就意识到事情不对。后来想了想发现势能分析不能带撤销。。。后来加了一些不能改变复杂度假了的优化,没过之后就自闭跑路了。。。赛后听别人说了个楼房重建就明白怎么做了。首先,我们离线下来把\(a\)排序,去重(这样方便一点,不然权值线段树上......
  • 一千个需求如何快速排序?MoSCoW排序法用上了!【No.2】
    什么是MoSCoW排序法?莫斯科排序法是一种优先级排序法,用于管理需求、任务或功能列表。该方法可以帮助团队确定哪些需求、任务或功能是最重要的,并决定在特定时间段内是否需要完成它们。所以在对需求进行排序时,可以从以下维度考虑:能为业务目标产出高价值的需求优先做;节省时间、人......
  • 2012-2013 ACM-ICPC, NEERC, Moscow Subregional Contest题解
    题目链接:2012-2013ACM-ICPC,NEERC,MoscowSubregionalContestC.Cinderella(贪心)思路答案为大于平均值的数的数量代码#include<bits/stdc++.h>usingnamespacestd;usingi64=longlong;intmain(){ios::sync_with_stdio(false);cin.tie(nullptr);......