【AtCoder Beginner Contest 347】

fishcanfly發表於2024-03-31

D - Popcount and XOR

easy.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        int a = rd.nextInt();
        int b = rd.nextInt();
        long c = rd.nextLong();
        int n = 0;

        int[] arr = new int[60];
        //calculate n,l;
        for (int i = 59; i >= 0; i--) {
            if (((1L << i) & c) > 0) {
                n++;
                arr[i] = 1;
            }
        }
        //取k個數k[0,min(n,a)];
        //a-k<=60-n;k>=a+n-60;
        //2k=a+n-b; 有解
        //

        if ((a + n - b) < 0 || (a + n - b) % 2 != 0) {
            System.out.println(-1);
            return;
        }

        int k = (a + n - b) / 2;
        if (k > a || k > n || k < a + n - 60) {
            System.out.println(-1);
            return;
        }

        long x = 0;
        long y = 0;

        int cnt = 0;
        for (int i = 0; i < 60; i++) {
            if ( arr[i] == 0) {
                continue;
            }
            else if (cnt < k) {
                x += (1L<< i);
                cnt++;
            }
            else {
                y += (1L<<i);
            }
        }

        //x
        for (int i = 0; i < 60; i++) {
            if ( arr[i] == 0 && cnt < a) {
                x += (1L<<i);
                cnt++;
                y += (1L<<i);
            }
        }
        System.out.println(x +" " + y);
    }
}

class rd {
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer tokenizer = new StringTokenizer("");

    // nextLine()讀取字串
    static String nextLine() throws IOException {
        return reader.readLine();
    }

    // next()讀取字串
    static String next() throws IOException {
        while (!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine());
        return tokenizer.nextToken();
    }

    // 讀取一個int型數值
    static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    // 讀取一個double型數值
    static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    // 讀取一個long型數值
    static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    // 讀取一個BigInteger
    static BigInteger nextBigInteger() throws IOException {
        BigInteger d = new BigInteger(rd.nextLine());
        return d;
    }
}

E - Set Add Query

import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        int n = rd.nextInt();
        int q = rd.nextInt();

        //字首和
        long[] sum = new long[q + 1];
        long[] nsum = new long[n + 1];
        //idx map;
        Map<Integer, Integer> map = new HashMap<>();

        for (int i = 1; i <= q; i++) {
            sum[i] = sum[i - 1];
            int x = rd.nextInt();
            if (map.containsKey(x)) {
                int j = map.get(x);
                nsum[x] += sum[i] - sum[j - 1];
                map.remove(x);
                sum[i] += map.size();
            } else {
                map.put(x, i);
                sum[i] += map.size();
            }
        }
        //再計算沒有closed;
        for (Map.Entry<Integer, Integer> entrys : map.entrySet()) {
            int x = entrys.getKey();
            int id = entrys.getValue();

            nsum[x] += sum[q] - sum[id - 1];
        }
        PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out));
        for (int i = 1; i <= n; i++) {
            if (i != 1) {
                pw.print(" ");
            }
            pw.print(nsum[i]);
        }
        pw.println();
        pw.flush();
    }
}

class rd {
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer tokenizer = new StringTokenizer("");

    // nextLine()讀取字串
    static String nextLine() throws IOException {
        return reader.readLine();
    }

    // next()讀取字串
    static String next() throws IOException {
        while (!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine());
        return tokenizer.nextToken();
    }

    // 讀取一個int型數值
    static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    // 讀取一個double型數值
    static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    // 讀取一個long型數值
    static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    // 讀取一個BigInteger
    static BigInteger nextBigInteger() throws IOException {
        BigInteger d = new BigInteger(rd.nextLine());
        return d;
    }
}

F 題: Non-overlapping Squares

//ANMHLIJKTJIY!
#pragma GCC optimize(2)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline","fast-math","unroll-loops","no-stack-protector")
#pragma GCC diagnostic error "-fwhole-program"
#pragma GCC diagnostic error "-fcse-skip-blocks"
#pragma GCC diagnostic error "-funsafe-loop-optimizations"
#include <bits/stdc++.h>
#define INF 1000000000
#define LINF 1000000000000000000
#define MOD 1000000007
#define mod 998244353
#define F first
#define S second
#define ll long long
#define N 1010
using namespace std;
ll n,m,a[N][N],v[N][N],mx[4][N][N],mxc[N],mxr[N];
ll ask(ll lx,ll ly,ll rx,ll ry)
{
	return a[rx][ry]-a[lx-1][ry]-a[rx][ly-1]+a[lx-1][ly-1];
}
int main(){
	ll i,j;
	cin>>n>>m;
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=n;j++)
		{
			cin>>a[i][j];
			a[i][j]+=a[i][j-1];
		}
	}
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=n;j++)
		{
			a[i][j]+=a[i-1][j];
		}
	}
	for(i=1;i+m-1<=n;i++)
	{
		for(j=1;j+m-1<=n;j++)
		{
			v[i][j]=ask(i,j,i+m-1,j+m-1);
			mx[0][i][j]=mx[1][i][j]=mx[2][i][j]=mx[3][i][j]=v[i][j];
			mxr[i]=max(mxr[i],v[i][j]);
			mxc[j]=max(mxc[j],v[i][j]);
		}
	}
	for(i=1;i<=n-m+1;i++)
	{
		for(j=1;j<=n-m+1;j++)
		{
			mx[0][i][j]=max(max(mx[0][i][j],mx[0][i-1][j]),mx[0][i][j-1]);
		}
	}
	for(i=1;i<=n-m+1;i++)
	{
		for(j=n-m+1;j>0;j--)
		{
			mx[1][i][j]=max(max(mx[1][i][j],mx[1][i-1][j]),mx[1][i][j+1]);
		}
	}
	for(i=n-m+1;i>0;i--)
	{
		for(j=1;j<=n-m+1;j++)
		{
			mx[2][i][j]=max(max(mx[2][i][j],mx[2][i+1][j]),mx[2][i][j-1]);
		}
	}
	for(i=n-m+1;i>0;i--)
	{
		for(j=n-m+1;j>0;j--)
		{
			mx[3][i][j]=max(max(mx[3][i][j],mx[3][i+1][j]),mx[3][i][j+1]);
		}
	}
	ll ans=0;
	for(i=m+1;i+m+m-1<=n;i++)
	{
		ll cur=0;
		for(j=i+m;j+m-1<=n;j++)
		{
			cur=max(cur,mxc[j-m]);
			ans=max(ans,cur+mx[0][n-m+1][i-m]+mx[1][n-m+1][j]);
		}
	}
	for(i=m+1;i+m+m-1<=n;i++)
	{
		ll cur=0;
		for(j=i+m;j+m-1<=n;j++)
		{
			cur=max(cur,mxr[j-m]);
			ans=max(ans,cur+mx[0][i-m][n-m+1]+mx[2][j][n-m+1]);
		}
	}
	for(i=m;i+m-1<=n;i++)
	{
		for(j=m;j+m-1<=n;j++)
		{
			ans=max(ans,mx[0][i-m][j-m]+mx[2][i][j-m]+mx[3][1][j]);
			ans=max(ans,mx[2][1][j-m]+mx[1][i-m][j]+mx[3][i][j]);
			ans=max(ans,mx[0][i-m][j-m]+mx[1][i-m][j]+mx[3][i][1]);
			ans=max(ans,mx[1][i-m][1]+mx[2][i][j-m]+mx[3][i][j]);
		}
	}
	cout<<ans<<'\n';
	return 0;
}