性感树状数组代码在线求调,WA On Test #10
查看原帖
性感树状数组代码在线求调,WA On Test #10
430133
liangbob楼主2023/3/18 23:20
#include <iostream>
#include <iomanip> 
#include <cmath> 
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define IL inline
#define int long long 
#define lowbit(i) (i & -i)
using namespace std;
const int N = 2e5 + 10;
const int INF = 0x3f3f3f3f;

IL int read() 
{
    int x = 0,f = 1;
    char c = getchar();
    while(c <'0'|| c >'9'){if(c == '-') f = -1;c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0',c = getchar();
    return x * f;
}

void write(int x) 
{
    if(x < 0) putchar('-'),x = -x;
    if(x > 9) write(x / 10);
    putchar(x % 10 + '0');
}

int c[N], n, cnt, f[N];
struct node
{
	int i_ai, ai;	
}; 
node a[N];

int query(int x)
{
	int sum = 0;
	while(x > 0)
	{
		sum = max(sum, c[x]);
		x -= lowbit(x);
	}
	return sum;
}

void update(int x, int p)
{
	while(x <= cnt)
	{
		c[x] = max(c[x], p);
		x += lowbit(x);
	}
}

bool cmp(node x, node y)
{
	return x.i_ai < y.i_ai || (!(y.i_ai < x.i_ai) && x.ai < y.ai);
}

signed main()
{
	int n;
	cin >> n;
	for(int i = 1;i <= n;i++)
	{
		int t;
		cin >> t;
		f[i] = -INF;
		if(t <= i)
		{
			a[++cnt].ai = t;
			a[cnt].i_ai = i - t;
		}
	}
	int maxv = 0;
	sort(a + 1, a + cnt + 1, cmp);
	for(int i = 1;i <= cnt;i++)
	{
		f[i] = query(a[i].ai - 1) + 1;
		update(a[i].ai, f[i]);
		maxv = max(maxv, f[i]);
	}
	cout << maxv << endl;
	return 0;
}
2023/3/18 23:20
加载中...