首页 > 其他分享 >POJ3263 Tallest Cow 括号技巧

POJ3263 Tallest Cow 括号技巧

时间:2023-02-07 12:07:30浏览次数:58  
标签:Cow POJ3263 cow int LL height const include Tallest


题目描述

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.

给出牛的可能最高身高,然后输入m组数据 a b,代表a,b可以相望,最后求所有牛的可能最高身高输出

输入输出格式

输入格式:


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.

输入输出样例

输入样例#1: 复制

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


输出样例#1:  复制

5 4 5 3 4 4 5 5 5





算法分析:

题意:

给出牛的可能最高身高,然后输入m组数据 a b,代表a,b可以相望,最后求所有牛的可能最高身高输出

分析:

如果a能看到 b 则围一个a--b的括号,括号不可能出现跨越的情况,因为不可能出现a x b y即x能看到y,a又能看到b的情况,然后用 f[a]表示a这位以及b后面几位都要进行的操作,计算公式f[i]+=f[i-1];,如果a到 b围了一个括号则f[a+1]-- ; f[b]++ 。

举个例子: 1 ~4

f[1]=0;f[2]=-1;f[3]=0;f[4]=1;

f[i]+=f[i-1];

f[1]=0;f[2]=-1;f[3]=-1;f[4]=0;


为了防止相同的条件重复减用个map 记录一下,但还是会出现3 7 和7 3这种本质相同的操作,所以要交换一下。

 

#include<cstdio>  
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
using namespace std;
const double eps = 1e-8;
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 1e5 + 10;
const int MAXT = 10000 + 10;
const int N=100005;
int n,i,h,r;
map<int,int>mp[N];//用普通会超内存
int f[N];
int main()
{
while(scanf("%d%d%d%d",&n,&i,&h,&r)!=EOF)
{
memset(f,0,sizeof(f));
for(int i=1;i<=n;i++)
mp[i].clear();
for(int i=1;i<=r;i++)
{
int x,y;
scanf("%d%d",&x,&y);
if(x>y) swap(x,y);

if(mp[x][y])
continue;
else mp[x][y]=1;
f[x+1]--;
f[y]++;
}
for(int i=1;i<=n;i++)
{
cout<<f[i]<<" ";
f[i]=f[i]+f[i-1];
printf("%d\n",f[i]+h);
}

}
return 0;

}



标签:Cow,POJ3263,cow,int,LL,height,const,include,Tallest
From: https://blog.51cto.com/u_14932227/6041835

相关文章

  • 【P5196】【USACO19JAN】Cow Poetry G
    前言这是我们今天课上一道练习,结果全班就我一个过了。看到这道题我就有了思路(不过还是调了很久。Solution题意明白,不多赘述。首先考虑对于一行诗,凑足\(k\)个音节有......
  • P1472 奶牛家谱 Cow Pedigrees(DP)
    题目描述农民约翰准备购买一群新奶牛。在这个新的奶牛群中,每一个母亲奶牛都生两个小奶牛。这些奶牛间的关系可以用二叉树来表示。这些二叉树总共有N个节点(3<=N<200)......
  • P3119 [USACO15JAN]Grass Cownoisseur G 题解
    做过的原题,模拟赛时PDF里的题面实在有点难受。首先有显然结论:在一个环上反走一定是不值的,因为环上的点本来就相互可达。所以考虑缩点。缩点后的问题可以看成:求对于每一......
  • C++Day09 深拷贝、写时复制(cow)、短字符串优化
    一、std::string的底层实现1、深拷贝1classString{2public:3String(constString&rhs):m_pstr(newchar[strlen(rhs)+1]()){4}5private:6cha......
  • NC25064 [USACO 2007 Mar G]Ranking the Cows
    题目链接题目题目描述EachofFarmerJohn'sNcows(1≤N≤1,000)producesmilkatadifferentpositiverate,andFJwouldliketoorderhiscowsaccording......
  • 213. 字典序最小问题 Best Cow Line(挑战程序设计竞赛)
    地址https://www.papamelon.com/problem/213给定一个字符SS,长度为NN。由SS构成出新的字符串TT,长度也为NN。起初TT是一个空串,然后执行NN次操作,每次操作有两种......
  • [USACO22DEC] Cow College B 题解
    洛谷P8897AcWing4821题目描述有\(n\)头奶牛,每头奶牛愿意交的最大学费位\(c_i\),问如何设置学费,可以使赚到的钱最多。\(1\len\le10^5,1\lec_i\le10^6\)做法分析......
  • IfcOwnerHistory
    IfcOwnerHistory实体定义IfcOwnerHistory定义所有历史和标识相关信息。为了提供快速访问,它直接连接到所有独立的对象、关系和属性。 IfcOwnerHistory用于标识创建和拥......
  • [oeasy]python0036_牛说_cowsay_小动物说话_asciiart_figlet_lolcat_管道(祝大家新年
    ​ 牛说(cowsay)回忆上次内容上次我们研究了shell脚本的编程并且在shell中实现了循环语句延迟命令清屏命令python命令figlet命令​编辑还能整点什么......
  • POJ 3176 Cow Bowling
    POJ3176CowBowling题意:给出一个数字三角形,每个分叉路口可以选择一条道路向下走,获得路上的点的权值。求可以获得的最大权值是多少?思路:从上向下走和从下向上走是一......