C. LuoTianyi and the Show (贪心)

Problem - C - Codeforces

 给定n个xi,m个位置,xi>0 代表当前这个人有想坐的位置,xi=-1,代表这个人可以放在当前已经排好的队伍的最左边(如果当前没有人就可以先放在m的位置上),xi=-2,代表这个人可以放在当前已经排好的队伍的最右边(如果当前没有人就可以先放在1的位置上)

有三种操作,第一种操作是排xi=-1的人,第二种操作是排xi=-2的人,第三种操作是排xi>0的人,问最终能让多少人坐在位置上

输入

10
3 10
5 5 5
4 6
1 -2 -2 1
5 7
-1 -1 4 -2 -2
6 7
5 -2 -2 -2 -2 -2
6 6
-1 1 4 5 -1 4
6 8
-1 -1 -1 3 -1 -2
6 7
5 -1 -2 -2 -2 -2
3 1
-2 -2 1
2 5
5 -2
1 2
-1

输入

1
3
5
6
5
5
5
1
2
1

 

#include <iostream>
#include <vector>
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <algorithm>
#define x first
#define y second
#define pb emplace_back
#define fu(i,a,b) for(int i=a;i<=b; ++ i)
#define fd(i,a,b) for(int i=a;i>=b;	-- i)
#define endl '\n'
#define ms(x,y) memset(x,y,sizeof x)
#define ios ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
using namespace std;

typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef vector<vector<LL>> VVL;
typedef vector<vector<int>> VVI;
typedef vector<LL> VL;
typedef vector<bool> VB;
typedef vector<int> VI;
typedef vector<string> VS;
typedef pair<int,int> PII;
typedef vector<PII> VPII;
typedef pair<PII,int> PIII;
typedef pair<double,double> PDD;
typedef pair<double,int> PDI;
typedef pair<char,int> PCI;
typedef pair<string,int> PSI;
typedef pair<int,string> PIS;
typedef pair<int,char> PIC;
typedef pair<LL,LL> PLL;
typedef __int128 i128;
typedef unsigned long long ULL;
const int N =3e3 +  10 ,M = N *5,INF = 0x3f3f3f3f,P = 131;
const double eps = 1e-8,DNF = 1e18;
const int mod = 998244353 ,base= 20010;
const LL LNF=(LL) INF * INF;



void solve()
{
	int n,m;cin >> n >> m ;
	int t,l=0,r=0;
	VI a;
	
	fu(i,0,n-1)
	{
		int t;cin >> t ;
		if(t == -1) l ++ ; // 第一种操作向左扩张 
		else if(t == - 2 ) r ++;
		else a.push_back(t);// 有固定位置想法的人 
	}
	
	sort(a.begin(),a.end());
	a.erase(unique(a.begin(),a.end()),a.end());
	
	int ans = 0;
	//  先满足 有固定的位置想法的人,然后只用其中一种操作扩张
// 比如只用第一种操作, 先从m 向左到a中的最后一个元素,后面看是用a中的元素,还是使用第一种操作 
	ans= min(max(ans,l + (int)a.size() ),m);
	ans = min(max(ans,r + (int)a.size()),m);

// 两个操作都用,选定一个人向两边扩张,每次选用前两种操作或者是第三种操作 
	fu(i,0,(int)a.size() - 1 )
	{
		int t =min(a[i] - 1 - i,l) +
// 当前这个有固定位置想法的 人 ,左边除有固定的位置的人以外还有多少能扩张的位置		
		min(m-a[i] + 1 - ((int)a.size() - i ) ,r) 
// 当前这个有固定位置想法的 人 ,右边除有固定的位置的人以外还有多少能扩张的位置		
		+ (int) a.size();
// 最后加上 有固定位置 人的数量		
		ans = max(ans,t); 	
	}	
	cout << ans << endl;
	
}


signed main()
{
//  freopen("1.txt","r",stdin);
//	#define int long long 
//	init(N-1);
    ios
//	cout << fixed<<setprecision(2);
    int t=1;
   	cin>>t;
    int now = 1;
    while(t -- )
    {
//     cout<<"Case "; 
//      cout<<"Case #"; 
//      cout<<"Scenario #"; 
//     cout<< now ++ <<": ";
//      cout<< now ++ <<": \n";
		solve();
    }


    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值