1574D - The Strongest Build - CodeForces Solution


binary search brute force data structures dfs and similar graphs greedy hashing implementation *2000

Please click on ads to support us..

C++ Code:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <vector>

using namespace std;
using ll = long long;
#define endl '\n'


// First = hash, index 
// Second = final hash, final result
set<vector<int>> s; // Banned
int n;
const int MAX_LOW = -2e9;

pair<vector<int>, int> dp(vector<vector<int>> &v, vector<int> &hash, int i=0) {
	if (i == n) {
		if (s.find(hash) == s.end())
			return {{}, 0};
		
		else
			return {{}, MAX_LOW};
	}
	
	else {
		pair<vector<int>, int> result = {{}, MAX_LOW};
		
		for (int j = v[i].size()-1; j >= 0; j--) {
			hash.push_back(j+1);
			pair<vector<int>, int> nxtRes = dp(v, hash, i+1);
			
			nxtRes.first.push_back(j+1);
			nxtRes.second += v[i][j];
			
			if (nxtRes.second > result.second)
				result = nxtRes;
			
			if (s.find(hash) == s.end()) {
				hash.pop_back();
				break;
			}
			
			else
				hash.pop_back();
		}
		
		return result;
	}
}


vector<string> removeDupWord(string str)
{
	vector<string> result;
	string cur = "";
	
	for (char c : str) {
		if (c == ' ') {
			result.push_back(cur);
			cur = "";
		}
		
		else
			cur += c;
	}
	
	result.push_back(cur);
	return result;
}


void solve() 
{
	cin >> n;
	vector<vector<int>> v(n);
	
	for (int i = 0; i < n; i++) {
		int m;
		cin >> m;
		v[i] = vector<int>(m);
		
		for (int j = 0; j < m; j++)
			cin >> v[i][j];
	}
	
	int m;
	cin >> m;
	vector<vector<int>> banned(m, vector<int>(n));
	
	for (int i = 0; i < m; i++)
		for (int j = 0; j < n; j++)
			cin >> banned[i][j];
		
	for (int i = 0; i < m; i++) {
		vector<int> cur;
		
		for (int j = 0; j < n; j++) {
			cur.push_back(banned[i][j]);
			s.insert(cur);
		}
	}
	
	vector<int> hash;
	vector<int> res = dp(v, hash).first;
	reverse(res.begin(), res.end());
	
	for (int val : res)
		cout << val << " ";
	
	cout << endl;
}


int main() 
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	
//	int t;
//	cin >> t;
//	
//	while (t--)
//		solve();
	
	solve();
}


Comments

Submit
0 Comments
More Questions

115A - Party
746B - Decoding
1424G - Years
1663A - Who Tested
1073B - Vasya and Books
195B - After Training
455A - Boredom
1099A - Snowball
1651D - Nearest Excluded Points
599A - Patrick and Shopping
237A - Free Cash
1615B - And It's Non-Zero
1619E - MEX and Increments
34B - Sale
1436A - Reorder
1363C - Game On Leaves
1373C - Pluses and Minuses
1173B - Nauuo and Chess
318B - Strings of Power
1625A - Ancient Civilization
864A - Fair Game
1663B - Mike's Sequence
448A - Rewards
1622A - Construct a Rectangle
1620A - Equal or Not Equal
1517A - Sum of 2050
620A - Professor GukiZ's Robot
1342A - Road To Zero
1520A - Do Not Be Distracted
352A - Jeff and Digits