Codeforces Round #661 (Div. 3)

Powered by:AB_IN 局外人

A. Remove Smallest

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll t,n,a[101],x;
int main()
{
    //freopen("write.txt","w",stdout);
    cin>>t;
    while(t--){
        int flag=0;
        cin>>n;
        for(int i=1;i<=n;i++){
            cin>>a[i];
        }
        sort(a+1,a+1+n);
        for(int i=2;i<=n;i++){
            if(a[i]!=a[i-1]&&a[i]!=a[i-1]+1){
                cout<<"NO\n";
                flag=1;
                break;
            }
        }
        if(!flag) cout<<"YES\n";
    }
    return 0;
}

B. Gifts Fixing

t=int(input())
while t>0:
    t-=1
    ans=0
    n=int(input())
    a=list(map(int ,input().split()))
    #a.sort()
    b=list(map(int ,input().split()))
    #b.sort()
    ma=min(a)
    mb=min(b)
    for i in range(n):
        c_a=a[i]-ma
        c_b=b[i]-mb
        if a[i]>ma and b[i]>mb:
            ans+=max(c_a,c_b)
        elif a[i]>ma:
            ans+=c_a
        elif b[i]>mb:
            ans+=c_b
    print(ans)

C. Boats Competition

既然是两个元素,那么自然而然双指针。
枚举 s s 即可, s o r t sort 之后, s s 最大就是最后两项的和。

t = int(input())
for i in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	a.sort()
	mx = 0
	for s in range (a[n-2]+a[n-1]+1):
		ans = 0
		p1 = 0
		p2 = n-1
		while p1<p2:
			if a[p1]+a[p2]==s:
				p1 += 1
				p2 -= 1
				ans += 1
			elif a[p1]+a[p2]>s:
				p2 -= 1
			else:
				p1 += 1
		mx = max(mx,ans)
		
	print(mx)

D. Binary String To Subsequences

其实就是模拟队列。
现在有 z e r o zero o n e one 两个队列。
z e r o zero 举例,如果一开始来了 0 0 ,先看看 o n e one 有没有值。
如果有,那么 0 0 这儿对应 o n e one 的队首,并弹出,加入到 z e r o zero 中。
如果没有,说明前面没有空余的 1 1 的右位置了,继续加入 z e r o zero 中,并自增。
举一个例子吧
01100 01100
开始是 0 0 , o n e one 里没有,所以 o n e : [ ] one:[] , z e r o : [ 1 ] zero:[1] , g = 2 g=2 a [ 0 ] = 1 a[0]=1
然后是 1 1 , z e r o zero 里有, 所以 o n e : [ 1 ] one:[1] , z e r o : [ ] zero:[] , g = 2 g=2 , a [ 1 ] = 1 a[1]=1
然后是 1 1 , z e r o zero 里没有,所以 o n e : [ 1 , 2 ] one:[1,2] , z e r o : [ ] zero:[] , g = 3 g=3 , a [ 2 ] = 2 a[2]=2
然后是 0 0 , o n e one 里有,但要是最靠近的,所以弹出队首, o n e : [ 1 ] one:[1] z e r o : [ 2 ] zero:[2] , g = 3 g=3 , a [ 3 ] = 2 a[3]=2
然后是 0 0 , o n e one 里有,所以 o n e : [ ] one:[] , z e r o : [ 1 ] zero:[1] , g = 2 g=2 , a [ 4 ] = 1 a[4]=1 (相当于这个 0 0 和一开始的 01 01 接上了。)

for i in range(int(input())):
    n = int(input())
    t = input()
    a = [0] * n
    zero = []
    one = []
    g = 1
    for i in range(n):
        if t[i]=="0":
            if one:
                x = one.pop()
                a[i] = x
                zero.append(x)
            else:
                a[i] = g
                zero.append(g)
                g += 1
 
        else:
            if zero:
                x = zero.pop()
                a[i] = x
                one.append(x)
            else:
                a[i] = g
                one.append(g)
                g += 1
    print(max(a))
    print(*a)

完结。

猜你喜欢

转载自blog.csdn.net/qq_45859188/article/details/107839378