当前位置: 代码迷 >> 综合 >> POJ3263 Tallest Cow 括号技巧
  详细解决方案

POJ3263 Tallest Cow 括号技巧

热度:37   发布时间:2024-01-15 08:10:19.0

题目描述

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,代表ab可以相望,最后求所有牛的可能最高身高输出

分析:

如果a能看到 b 则围一个a--b的括号,括号不可能出现跨越的情况,因为不可能出现a x b yx能看到ya又能看到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;  }