Submission #678492

#TimeUsernameProblemLanguageResultExecution timeMemory
678492cig32Vision Program (IOI19_vision)C++17
100 / 100
43 ms3600 KiB
#include <bits/stdc++.h>
#include "vision.h"
#include <map>
 
void construct_network(int H, int W, int K) {
	std::vector<int> Ns;
  std::vector<int> by_sum, by_diff;
  std::map<int, std::vector<int> > mp_sum, mp_diff;
  for(int i=0; i<H; i++) {
    for(int j=0; j<W; j++) {
      mp_sum[i + j].push_back(i*W + j);
      mp_diff[i - j].push_back(i*W + j);
    }
  }
  std::vector<int> analyze; // Necessary conditions, i.e. AND clause
  for(auto x: mp_sum) {
    by_sum.push_back(add_or(x.second));
  }
  for(auto x: mp_diff) {
    by_diff.push_back(add_or(x.second));
  }
  // Condition 1: at least one of these two arrays' 1s are exactly K cells apart
  std::vector<int> cond1;
  for(int i=0; i<by_sum.size(); i++) {
    if(i + K < by_sum.size()) {
      std::vector<int> uwu = {by_sum[i], by_sum[i + K]};
      cond1.push_back(add_and(uwu));
    }
  }
  for(int i=0; i<by_diff.size(); i++) {
    if(i + K < by_diff.size()) {
      std::vector<int> uwu = {by_diff[i], by_diff[i + K]};
      cond1.push_back(add_and(uwu));
    }
  }
  
  // Only one in cond1 array = 1 is necessary (but at least 1)!
  analyze.push_back(add_or(cond1));
  // condition 2: [1 + K 0s] + at least one 1 in the right should not appear in any of them
  std::vector<int> cond2;
  for(int i=0; i<by_sum.size(); i++) {
    if(i + 2 + K < by_sum.size()) {
      std::vector<int> patt; // does this subarray match the pattern?
      std::vector<int> uwu = {by_sum[i]};
      patt.push_back(add_and(uwu));
      uwu.clear();
      for(int j=i+1; j<i+1+K; j++) uwu.push_back(by_sum[j]);
      patt.push_back(add_not(add_or(uwu)));
      // Everything in patt = 1 iff by_sum[i] = 1 and all by_sum[i+1] to by_sum[i+K] = 0
      std::vector<int> all_nxt;
      for(int j=i+1+K; j<by_sum.size(); j++) all_nxt.push_back(by_sum[j]);
      patt.push_back(add_or(all_nxt));
      cond2.push_back(add_and(patt)); // = 1 if match pattern, = 0 otherwise
    }
  }
  for(int i=0; i<by_diff.size(); i++) {
    if(i + 2 + K < by_diff.size()) {
      std::vector<int> patt; // does this subarray match the pattern?
      std::vector<int> uwu = {by_diff[i]};
      patt.push_back(add_and(uwu));
      uwu.clear();
      for(int j=i+1; j<i+1+K; j++) uwu.push_back(by_diff[j]);
      patt.push_back(add_not(add_or(uwu)));
      // Everything in patt = 1 iff by_diff[i] = 1 and all by_diff[i+1] to by_diff[i+K] = 0
      std::vector<int> all_nxt;
      for(int j=i+1+K; j<by_diff.size(); j++) all_nxt.push_back(by_diff[j]);
      patt.push_back(add_or(all_nxt));
      cond2.push_back(add_and(patt)); // = 1 if match pattern, = 0 otherwise
    }
  }
  if(cond2.size()) analyze.push_back(add_not(add_or(cond2))); // = 1 if all cond2 = 0, i.e. no pattern found
  add_and(analyze); // answer
 
}

Compilation message (stderr)

vision.cpp: In function 'void construct_network(int, int, int)':
vision.cpp:24:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 |   for(int i=0; i<by_sum.size(); i++) {
      |                ~^~~~~~~~~~~~~~
vision.cpp:25:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   25 |     if(i + K < by_sum.size()) {
      |        ~~~~~~^~~~~~~~~~~~~~~
vision.cpp:30:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   30 |   for(int i=0; i<by_diff.size(); i++) {
      |                ~^~~~~~~~~~~~~~~
vision.cpp:31:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   31 |     if(i + K < by_diff.size()) {
      |        ~~~~~~^~~~~~~~~~~~~~~~
vision.cpp:41:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |   for(int i=0; i<by_sum.size(); i++) {
      |                ~^~~~~~~~~~~~~~
vision.cpp:42:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |     if(i + 2 + K < by_sum.size()) {
      |        ~~~~~~~~~~^~~~~~~~~~~~~~~
vision.cpp:51:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |       for(int j=i+1+K; j<by_sum.size(); j++) all_nxt.push_back(by_sum[j]);
      |                        ~^~~~~~~~~~~~~~
vision.cpp:56:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   56 |   for(int i=0; i<by_diff.size(); i++) {
      |                ~^~~~~~~~~~~~~~~
vision.cpp:57:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |     if(i + 2 + K < by_diff.size()) {
      |        ~~~~~~~~~~^~~~~~~~~~~~~~~~
vision.cpp:66:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   66 |       for(int j=i+1+K; j<by_diff.size(); j++) all_nxt.push_back(by_diff[j]);
      |                        ~^~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...