2007å¹´å½å äºé¸Cåé¡ï¼Cut the Cakeï¼(2007/07/30(æ) 01:10:47)
2007å¹´å½å äºé¸Cåé¡ã®Cut the Cakeï¼ã±ã¼ãã«ããï¼ããã£ã¦ã¿ã¾ããã
解æ³
1ã¤1ã¤ã®ãã¼ã¹ãåãããé çªã大ããï¼ç¸¦ã»æ¨ªï¼ã§ç®¡çããã
åããããã¼ã¹ã¯åé¤ããæ°ãã«ï¼ã¤æ°ãããã¼ã¹ã追å ããã
ã½ã¼ãã¯ãåãããé çªã«ã½ã¼ãããless(<)ãªãã¬ã¼ã¿ã¨ãå°ãããã¼ã¹é ã«ã½ã¼ãããgreater(>)ãªãã¬ã¼ã¿ã使ç¨ããã
åã½ã¼ãã¯sorté¢æ°ã使ãããã£ãããã大å°è¨å·ã«ã¯é¢ä¿ãªããï¼ããããããã¨ãããªï¼ï¼ï¼
ã½ã¼ã¹
ï¼ç®æ¨ï¼ï¼å以å ï¼
#include <iostream> #include <vector> using namespace std; typedef struct PIECE{ int no; int w, d; bool operator < (const PIECE &p) const{ return no < p.no || no == p.no && w * d < p.w * p.d; } bool operator > (const PIECE &p) const{ return w * d < p.w * p.d; } }PIECE; int main(){ int n, w, d; while(cin >> n >> w >> d, (n || w || d)){ vector<PIECE> l; PIECE p = {-1, w, d}; l.push_back(p); for(int i = 0; i < n; i++){ int p, s, x = 0, y = 0; cin >> p >> s; vector<PIECE>::iterator iter = l.begin(); for(int j = 1; j != p && iter != l.end(); j++, iter++); if(iter == l.end())cout << "e;Error!!"e; << endl; s %= l[p - 1].w * 2 + l[p - 1].d * 2; if(s < l[p - 1].w)x = s; else if(s < l[p - 1].w + l[p - 1].d)y = s - l[p - 1].w; else if(s < l[p - 1].w * 2 + l[p - 1].d)x = s - (l[p - 1].w + l[p - 1].d); else y = s - (l[p - 1].w * 2 + l[p - 1].d); if(x > 0){ PIECE p1 = {i, x, (*iter).d}; PIECE p2 = {i, (*iter).w - x, (*iter).d}; l.erase(iter); l.push_back(p1); l.push_back(p2); } else{ PIECE p1 = {i, (*iter).w, y}; PIECE p2 = {i, (*iter).w, (*iter).d - y}; l.erase(iter); l.push_back(p1); l.push_back(p2); } sort(l.begin(), l.end(), less<PIECE>()); } sort(l.begin(), l.end(), greater<PIECE>()); int i; for(i = 0; i < l.size() - 1; i++){ cout << (l[i].w * l[i].d) << " "; } cout <<(l[i].w * l[i].d) << endl; } return 0; }