(POJ) 2182 lost costs (greedy or tree array)

Portal

Meaning: n cattle, each of which has [1, n] independent number. Now these cattle stand in a disorderly row, giving the number before the i cattle and smaller than the i cattle number. i belongs to 2 to n. Solve the number of each cow now

Thinking: we can think from the back to the front. In fact, the id of the last cow can be determined directly. Greedy recursion (I don't know whether it should be called greedy) is OK. However, we may use tree array. I use other people's directly. I also think from the back to the front, and then divide the number of the current cow. We can judge by the cow smaller than it in the front and the cow smaller than it in the back Broken, the tree array maintains the number smaller than it is behind the current cow. Finally, the id of the cow is to meet the requirement of id-1 = K (the number of numbers smaller than it in the front, known) + num (the number smaller than it in the back, tree array maintenance).
 

//greedy
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<cmath>
#include<sstream>
//#include<bits/stdc++.h>
#define il inline
#define pb push_back
#define fi first
#define se second
#define ms(_data,v) memset(_data,v,sizeof(_data))
#define sc(n) scanf("%d",&n)
#define SC(n,m) scanf("%d %d",&n,&m)
#define SZ(a) int((a).size())
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define drep(i,a,b)	for(int i=a;i>=b;--i)
using namespace std;
typedef long long ll;
const ll inf=0x3f3f3f3f;
const double PI=acos(-1.0);
const double eps=1e-9;
const int maxn=1e5+5;
int n,r[maxn],ans[maxn];
vector<int> tt;
int main() {
	while(sc(n)!=EOF) {
		tt.resize(n+5);
		tt[1]=1;
		rep(i,2,n)	sc(r[i]),tt[i]=i;
		drep(i,n,1) {
			ans[i]=tt[r[i]+1];
			tt.erase(tt.begin()+r[i]+1);
		}
		rep(i,1,n)	cout<<ans[i]<<endl;
		tt.clear();
	}
	return 0;
}
//Tree array
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<cmath>
#include<sstream>
//#include<bits/stdc++.h>
#define il inline
#define pb push_back
#define fi first
#define se second
#define ms(_data,v) memset(_data,v,sizeof(_data))
#define sc(n) scanf("%d",&n)
#define SC(n,m) scanf("%d %d",&n,&m)
#define SZ(a) int((a).size())
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define drep(i,a,b)	for(int i=a;i>=b;--i)
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define MAX 8005
int a[MAX],ans[MAX],tree[MAX],n;
il int ask(int pos){
	int ans=0;
	while(pos>0)	ans+=tree[pos],pos-=pos&(-pos);
	return ans;
}
il void add(int pos,int val){
	while(pos<=n)	tree[pos]+=val,pos+=pos&(-pos);
}
il int solve(int k){
	int left=1,right=n;
	while(left<right){
		int mid=(left+right)>>1;
		int num=ask(mid);
		if(mid-1<num+k) 	left=mid+1;
		else 	right=mid;
	}
	return left;
}
int main() {
	while(sc(n)!=EOF){
		ms(tree,0);
		a[1]=0;
		rep(i,2,n)	sc(a[i]);
		drep(i,n,1){
			int k=solve(a[i]);
			add(k,1);
			ans[i]=k;
		}
		rep(i,1,n)	cout<<ans[i]<<endl;
	}
	return 0;
}

 

Posted by johnska7 on Sun, 10 Nov 2019 08:51:08 -0800