codeforces 896 B. Ithea Plays With Chtholly(構造||貪心)

http://codeforces.com/contest/896/problem/B
題意:交互題,有n格,每次給一個[1,c]的數字,回答填入的位置後再次給數字,要求在m輪內使n格填滿且數列不遞減。n,m>=2,1<=c<=1000,1<=n*[c/2]<=m<=1000。

思路:看到題目,我首先思考給出m>=nc/2 有什麼用,一定能構造出序列,那麼關鍵就在這裏,然後自己寫了幾組數據思考了一下
考慮把小的數放在左邊,大的數放在右邊,每次如果能不破壞遞增序列就儘量往中間放,發現當這個分割爲c/2一定能得到長度爲n的序列
對於左邊替換的位置一定滿足now<pre 纔會替換 那麼最多替換c/2次,假設每個位置都替換c/2次總共就需要(n-1)
[c/2]+1,因爲最後一個位置填上就不可能被替換

#include<bits/stdc++.h>
#include<tr1/unordered_map>
#define fi first
#define se second
#define show(a) cout<<a<<endl;
#define show2(a,b) cout<<a<<" "<<b<<endl;
#define show3(a,b,c) cout<<a<<" "<<b<<" "<<c<<endl;
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
using namespace std;
 
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<P, int> LP;
const int inf = 0x3f3f3f3f;
const int N = 1e6 + 100;
const ll mod = 1e18+7;
const int base=131;
inline ll mul(ll x,ll y) { return (x*y-(ll)((long double)x*y/mod)*mod+mod)%mod;}
inline ll ksm(ll a,ll b) {ll ans=1;while(b){if(b&1)ans=mul(ans,a);a=mul(a,a),b>>=1;}return ans;}
 
 
ll n,m,x,y,cx,cy,flag;
ll a[N],b[N];
ll k,ans,cnt;
ll res[N],vis[N],mp[1005][1005];
ll pos[N];
vector<int> v[N];
 
P mx[N],mi[N];
int st[N];
 
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
 
	int c;
	cin>>n>>m>>c;
	while(m--)
	{
		cin>>x;
		if(x<=c/2)
		{
			for(int i=1;i<=n;i++)
			{
				if(a[i]==0)
				{
					a[i]=x;
					cnt++;
					cout<<i<<endl;
					break;
				}
				else if(x<a[i])
				{
					a[i]=x;
					cout<<i<<endl;
					break;
				}
			}
		}
		else
		{
			for(int i=n;i>=1;i--)
			{
				if(!a[i])
				{
					a[i]=x;
					cnt++;
					cout<<i<<endl;
					break;
				}
				else if(x>a[i])
				{
					a[i]=x;
					cout<<i<<endl;
					break;
				}
			}
		}
		//show2(cnt,m)
		if(cnt==n) return 0;
	}
 
 
}
/*
數據範圍
是否爆int
空間大小
*/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章