T1

n=int(input())
s=input()
if s==s[::-1]:
    print("YES")
else:
    print("NO")

T2

n=int(input())
als=[0]*50001
for i in range(n):
    l,op=map(int,input().split())
    if op==1:
        if als[l]==0:
            print("YES")
            als[l]=1
        else:
            print("NO")
    elif op==2:
        als[l]=0

T3


n=int(input())
ns=[int(i) for i in input().split()]
res=[]
while ns:
    now=set()
    tep=[]
    msx=0
    for i in range(len(ns)):
        if ns[i]>msx:
            now.add(i)
            msx=ns[i]
    nst=[]
    for i in range(len(ns)):
        if i in now:
            tep.append(ns[i])
        else:
            nst.append(ns[i])
    ns=nst
    res.append(tep)
print(len(res))
for i in res:
    if i:
        print(*i)

前三题不做解释。

T4

建立一个每轮最大数数组,遍历原数组,二分当前数在第几轮输出,并更新当前轮数在每轮最大数数组中的值。

python可以通过bisect_right实现在list中二分

c++可以通过upper_bound实现二分

或者自己写个二分

ps:数据结构也可以完成这道题,但是常数比较大,这里就不详细说了

from bisect import bisect_right
n=int(input())
ns=[int(i) for i in input().split()]
hq=[1e9]*n
res=[[] for i in range(n)]
cnt=0
i=0
while i<n:
    t=ns[i]
    a=bisect_right(hq,-t)
    res[a].append(t) 
    hq[a]=-t
    i+=1
cnt=0
for i in res:
    if i:
        cnt+=1
print(cnt)
for i in res:
    print(*i)



#include <bits/stdc++.h>
using namespace std;

int main() {
	int n;
	cin >> n;
	vector<int> ns(n);
	for (int i = 0; i < n; i++) {
		cin >> ns[i];
	}
	vector<int> hq(n, 0);
	vector<vector<int>> res(n);
	int cnt = 0;
	int i = 0;
	while (i < n) {
		int t = ns[i];
		auto it = upper_bound(hq.begin(), hq.begin() + cnt, -t);
		int a = it - hq.begin();
		res[a].push_back(t);
		hq[a] = -t;

		if (a == cnt) {
			cnt++;
		}
		i++;
	}
	cout << cnt << endl;
	for (int j = 0; j < cnt; j++) {
		for (size_t k = 0; k < res[j].size(); k++) {
			if (k > 0)
				cout << " ";
			cout << res[j][k];
		}
		cout << "\n";
	}
	return 0;
}

0 条评论

目前还没有评论...