Skip to content

Instantly share code, notes, and snippets.

@Rockbet
Created September 7, 2021 23:57
Show Gist options
  • Save Rockbet/1e7a321fb454b8e39275c165b561cb97 to your computer and use it in GitHub Desktop.
Save Rockbet/1e7a321fb454b8e39275c165b561cb97 to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
using namespace std;
const int maxn = 110;
string mat[maxn][maxn];
int vl[maxn], vc[maxn];
map<string, int> m;
set<string> s;
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int n, M;
cin >> n >> M;
for(int i=1; i<=n; i++){
for(int j=1; j<=M; j++){
cin >> mat[i][j];
s.insert(mat[i][j]);
}
cin >> vl[i];
}
for(int i=1; i<=M; i++) cin >> vc[i];
while(m.size() < s.size()){
for(int i=1; i<=n; i++){
int qtd = 0, sum = 0;
string aux;
bool ok = 1;
for(int j=1; j<=M; j++){
if(m.find(mat[i][j]) == m.end()){
if(qtd == 0){
qtd++;
aux = mat[i][j];
}
else if(aux == mat[i][j]) qtd++;
else{
ok = 0;
break;
}
}
else if(m.find(mat[i][j]) != m.end()) sum += m[mat[i][j]];
}
if(ok and qtd) m[aux] = (vl[i] - sum)/qtd;
}
for(int i=1; i<=M; i++){
int qtd = 0, sum = 0;
string aux;
bool ok = 1;
for(int j=1; j<=n; j++){
if(m.find(mat[j][i]) == m.end()){
if(qtd == 0){
qtd++;
aux = mat[j][i];
}
else if(aux == mat[j][i]) qtd++;
else{
ok = 0;
break;
}
}
else if(m.find(mat[j][i]) != m.end()) sum += m[mat[j][i]];
}
if(ok and qtd) m[aux] = (vc[i] - sum)/qtd;
}
}
for(auto it : m){
cout << it.first << " " << it.second << "\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment