zoukankan      html  css  js  c++  java
  • 2019CCSP A. 摘水果(拓扑排序)

    看到数据量1e2,直接暴力即可。每一轮暴力检查两棵树上度为0的点,寻找最优答案输出,同时更新deg数组。

    #include <bits/stdc++.h>
    #define N 405
    #define M 10005
    using namespace std;
    int n, head[N], ver[2 * M], Next[2 * M], tot = 0, deg[N], a[2 * N], b[2 * N];
    void add(int x, int y) {
    	ver[++tot] = y, Next[tot] = head[x], head[x] = tot;
    }
    bool chosen[N];
    int main() {
    	memset(deg, 0, sizeof(deg));
    	cin >> n;
    	for(int i = 1; i <= 2 * n; i++) {
    		cin >> a[i];
    	}
    	for(int i = 1; i <= 2 * n; i++) {
    		cin >> b[i];
    		add(i, b[i]);
    		deg[b[i]]++;
    	}
    	for(int t = 1; t <= n; t++) {
    		int val = 0, aa = 0, id1 = 0, id2 = 0;
    		for(int i = 1; i <= n; i++) {
    			for(int j = n + 1; j <= 2 * n; j++) {
    				if(!(deg[i] == 0 && deg[j] == 0 && chosen[i] == 0 && chosen[j] == 0)) 			continue;
    				int tmp = a[i] ^ a[j];
    				if(tmp > val) {
    					val = tmp;
    					aa = a[i];
    					id1 = i;
    					id2 = j;
    				} else if(tmp == val) {
    					if(a[i] > aa) {
    						aa = a[i];
    						id1 = i;
    						id2 = j;
    					} else if(a[i] == aa) {
    						if(i > id1) {
    							id1 = i;
    							id2 = j;
    						} else if(i == id1) {
    							if(j > id2) {
    								id2 = j;
    							}
    						}
    					}
    				}
    			}
    		}
    		cout << val << (t == n ? "" : " ");
    		//cout << id1 << " " << id2 << endl;
    		chosen[id1] = chosen[id2] = 1;
    		for(int i = head[id1]; i; i = Next[i]) {
    			int y = ver[i];
    			if(deg[y]) deg[y]--;
    		}
    		for(int i = head[id2]; i; i = Next[i]) {
    			int y = ver[i];
    			if(deg[y]) deg[y]--;
    		}
    	}
    }
    // 3              
    // 3 9 4 1 3 4
    // 0 0 2 0 4 4
    
  • 相关阅读:
    2020.4.13 机器学习相关数学基础
    2020.3.30 机器学习概述
    12.18语法制导的语义翻译
    12.11算符优先分析
    12.4自下而上语法分析
    11.27实验二 递归下降语法分析
    11.20LL(1)文法的判断,递归下降分析程序
    11.13消除左递归
    4.K均值算法--应用
    3.K均值算法
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/15409940.html
Copyright © 2011-2022 走看看