poj 2182 給你每個數前面有幾個數比他小讓你輸出次數的編號

POJ 2182 Lost Cows(樹狀數組,暴力解法)

2014年03月20日 02:00:55 閱讀數:1938 標籤: ACM 更多

個人分類: 數據結構--樹狀數組ACM--題解彙總★★ACM--技巧題practice again

版權聲明:本文爲博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/u013480600/article/details/21574467

POJ 2182 Lost Cows(樹狀數組,暴力解法)

http://poj.org/problem?id=2182

題意:

        N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacular display of poor judgment, they visited the neighborhood 'watering hole' and drank a few too many beers before dinner. When it was time to line up for their evening meal, they did not line up in the required ascending numerical order of their brands. 
        Regrettably, FJ does not have a way to sort them. Furthermore, he's not very good at observing problems. Instead of writing down each cow's brand, he determined a rather silly statistic: For each cow in line, he knows the number of cows that precede that cow in line that do, in fact, have smaller brands than that cow. 
        Given this data, tell FJ the exact ordering of the cows. 

 

Input

* Line 1: A single integer, N 
* Lines 2..N: These N-1 lines describe the number of cows that precede a given cow in line and have brands smaller than that cow. Of course, no cows precede the first cow in line, so she is not listed. Line 2 of the input describes the number of preceding cows whose brands are smaller than the cow in slot #2; line 3 describes the number of preceding cows whose brands are smaller than the cow in slot #3; and so on. 

Output

* Lines 1..N: Each of the N lines of output tells the brand of a cow in line. Line #1 of the output tells the brand of the first cow in line; line 2 tells the brand of the second cow; and so on.

Sample Input

5
1
2
1
0

Sample Output

2
4
5
3
1

 

分析:

       其實這道題目只要會手算用例就能暴力解決。

       假設讀入題目給的數組a[n],其中a[1]=0

       這道題目只給出了在i之前且比位於第i個位置的值小的值有多少個,我們在紙上分析用例可知,最後一個數的值肯定是a[n]+1.

       然後我們從後往前推,且初始化一個數組vis[n]爲全1。vis[i]==1表示當前值爲i的數還沒出現。首先我們推出最後一個數的值爲a[n]+1,所以我們標記vis[a[n]+1]=0,接着我們推n-1的值,假設a[n-1]=3,那麼就是在n-1的數之前有3個還未出現的數比它小,那麼我們從vis[1]開始掃描,找到第4個1的位置就是a[n-1]的值。然後把這第4個1的位置vis[x]標記0.接下去找n-2位置的值,以此類推即可。

<span style="font-size:18px;">#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN =10000;
int a[MAXN];//初始
int vis[MAXN];//標記1
int ans[MAXN];//最終計算的結果
int main()
{
    int n;
    while(scanf("%d",&n)==1&&n)
    {
        a[1]=0;
        for(int i=2;i<=n;i++)
            scanf("%d",&a[i]);
 
        for(int i=1;i<=n;i++)
            vis[i]=1;
        ans[n]=a[n]+1;
        vis[ans[n]]=0;
        for(int i=n-1;i>=1;i--)
        {
            int num = a[i];
            int j;
            for(j=1;i<=n;j++)
            {
                if(vis[j]==1)
                {
                    num--;
                    if(num<0)
                    {
                        vis[j]=0;
                        break;
                    }
                }
            }
            ans[i]=j;
        }
        for(int i=1;i<=n;i++)
            printf("%d\n",ans[i]);
    }
    return 0;
}</span>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章