#include <iostream>
#include <vector>
#include <algorithm>
#include "dzialka.h"
#include "message.h"
using namespace std;
#define LOG if (false) cerr
typedef long long ll;
const ll MAX_POLE = 30000000;
struct Komorka {
int poLewej;
int wokol;
Komorka(): poLewej(0), wokol(0) {}
};
void rozwiaz2() {
int wysokosc = GetFieldHeight();
int szerokosc = GetFieldWidth();
vector<vector<Komorka> > pole;
if ((ll)wysokosc * szerokosc > 16000000) {
return;
}
pole.resize(wysokosc + 1, vector<Komorka>(szerokosc + 1));
ll wynik = 0;
for (int y = 1; y <= wysokosc; y++) {
for (int x = 1; x <= szerokosc; x++) {
if (!IsUsableCell(y-1, x-1)) {
continue;
}
Komorka& komorka = pole[y][x];
const Komorka& komorkaWyzej = pole[y-1][x];
komorka.poLewej = pole[y][x-1].poLewej + 1;
komorka.wokol = komorka.poLewej;
if (komorkaWyzej.poLewej <= komorka.poLewej) {
komorka.wokol += komorkaWyzej.wokol;
}
else {
int szer = komorka.poLewej;
for (int yy = y-1; pole[yy][x].poLewej > 0; yy--) {
szer = min(szer, pole[yy][x].poLewej);
komorka.wokol += szer;
}
}
wynik += komorka.wokol;
}
}
cout << wynik << endl;
}
void rozwiaz() {
int wysokosc = GetFieldHeight();
int szerokosc = GetFieldWidth();
vector<vector<int> > pole;
if ((ll)wysokosc * szerokosc > MAX_POLE) {
return;
}
pole.resize(wysokosc + 1, vector<int>(szerokosc + 1));
ll wynik = 0;
for (int y = 1; y <= wysokosc; y++) {
for (int x = 1; x <= szerokosc; x++) {
if (!IsUsableCell(y-1, x-1)) {
continue;
}
pole[y][x] = pole[y][x-1] + 1;
int szer = pole[y][x];
for (int yy = y; pole[yy][x] > 0; yy--) {
szer = min(szer, pole[yy][x]);
wynik += szer;
}
LOG << x << ", " << y << ": " << wynik << endl;
}
}
cout << wynik << endl;
}
int main() {
if (MyNodeId() > 0) {
return 0;
}
// rozwiaz();
rozwiaz2();
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | #include <iostream> #include <vector> #include <algorithm> #include "dzialka.h" #include "message.h" using namespace std; #define LOG if (false) cerr typedef long long ll; const ll MAX_POLE = 30000000; struct Komorka { int poLewej; int wokol; Komorka(): poLewej(0), wokol(0) {} }; void rozwiaz2() { int wysokosc = GetFieldHeight(); int szerokosc = GetFieldWidth(); vector<vector<Komorka> > pole; if ((ll)wysokosc * szerokosc > 16000000) { return; } pole.resize(wysokosc + 1, vector<Komorka>(szerokosc + 1)); ll wynik = 0; for (int y = 1; y <= wysokosc; y++) { for (int x = 1; x <= szerokosc; x++) { if (!IsUsableCell(y-1, x-1)) { continue; } Komorka& komorka = pole[y][x]; const Komorka& komorkaWyzej = pole[y-1][x]; komorka.poLewej = pole[y][x-1].poLewej + 1; komorka.wokol = komorka.poLewej; if (komorkaWyzej.poLewej <= komorka.poLewej) { komorka.wokol += komorkaWyzej.wokol; } else { int szer = komorka.poLewej; for (int yy = y-1; pole[yy][x].poLewej > 0; yy--) { szer = min(szer, pole[yy][x].poLewej); komorka.wokol += szer; } } wynik += komorka.wokol; } } cout << wynik << endl; } void rozwiaz() { int wysokosc = GetFieldHeight(); int szerokosc = GetFieldWidth(); vector<vector<int> > pole; if ((ll)wysokosc * szerokosc > MAX_POLE) { return; } pole.resize(wysokosc + 1, vector<int>(szerokosc + 1)); ll wynik = 0; for (int y = 1; y <= wysokosc; y++) { for (int x = 1; x <= szerokosc; x++) { if (!IsUsableCell(y-1, x-1)) { continue; } pole[y][x] = pole[y][x-1] + 1; int szer = pole[y][x]; for (int yy = y; pole[yy][x] > 0; yy--) { szer = min(szer, pole[yy][x]); wynik += szer; } LOG << x << ", " << y << ": " << wynik << endl; } } cout << wynik << endl; } int main() { if (MyNodeId() > 0) { return 0; } // rozwiaz(); rozwiaz2(); return 0; } |
English