POJ 1090 Chain 점차+대수 덧셈


Chain
Time Limit: 1000MS
 
Memory Limit: 10000K
Total Submissions: 2025
 
Accepted: 684
Description
Byteland had not always been a democratic country. There were also black pages in its book of history. One lovely day general Bytel − commander of the junta which had power over Byteland −− decided to finish the long−lasting time of war and released imprisoned activists of the opposition. However, he had no intention to let the leader Bytesar free. He decided to chain him to the wall with the bytish chain. It consists of joined rings and the bar fixed to the wall. Although the rings are not joined with the bar, it is hard to take them off. 
'General, why have you chained me to the prison walls and did not let rejoice at freedom!' cried Bytesar. 
'But Bytesar, you are not chained at all, and I am certain you are able to take off the rings from the bar by yourself.' perfidiously answered general Bytel, and he added 'But deal with that before a clock strikes the cyber hour and do not make a noise at night, otherwise I will be forced to call Civil Cyber Police.' 
Help Bytesar! Number the following rings of the chain with integers 1,2,...,n. We may put on and take off these rings according to the following rules: 
.only one ring can be put on or taken off from the bar in one move, 
.the ring number 1 can be always put on or taken off from the bar, 
.if the rings with the numbers 1,...,k−1 (for 1<= k < n) are taken off from the bar and the ring number k is put on, we can put on or take off the ring number k+1. 
Write a program which: 
.reads from std input the description of the bytish chain, 
.computes minimal number of moves necessary to take off all rings of the bytish chain from the bar, 
.writes the result to std output.
Input
In the first line of the input there is written one integer n, 1 <= n <= 1000. In the second line there are written n integers o1,o2,...,on (each of them is either 0 or 1) separated by single spaces. If oi=1, then the i−th ring is put on the bar, and if oi=0, then the i−th ring is taken off the bar.
Output
The output should contain exactly one integer equal to the minimal number of moves necessary to take off all the rings of the bytish chain from the bar.
Sample Input
4
1 0 1 0

Sample Output
6

Source
POI 2001
 
/* http://acm.pku.edu.cn/JudgeOnline/problem?id=1090이 문제는 중국의 구연환 게임과 약간 유사하다. 제목은 대략 n의 길이를 가진 01 문자열 데이터를 주고 점 규칙에 따라 이 문자열을 전체 0 문자열로 바꾸어 이 과정의 최소 절차를 구하는 것이다.규칙은 다음과 같다. 1) 한 단계는 반드시 바뀌어야 하고 한 자리만 바뀔 수 있다. (0은 1, 1은 0) 첫 번째 문자는 임의의 시간에 바뀔 수 있다. 3) 정수 k, 1<=k#include#define MAX_N 1000 using namespace std; int data[MAX_N + 1]; string takeOff[MAX_N + 1]; int n; string bigIntegerAdd(const string left, const string right) { int lenl = static_cast(left.size()); int lenr = static_cast(right.size()); std::string res = "";//get the minimum length of the two string int minLen = (lenl <= lenr) ? lenl : lenr; int maxLen = (lenl <= lenr) ? lenr : lenl; std::string longerStr = (lenl <= lenr) ? right : left; int residue = 0, sum = 0, quotient = 0;//add operation, first part int pos1; for(pos1 = 0; pos1 <= minLen - 1; pos1++) { sum = (left[pos1] - '0') + (right[pos1] - '0') + quotient; quotient = sum/10; residue = sum % 10; res += residue + '0'; }//add operation, second part for(int pos2 = pos1; pos2 <= maxLen - 1; pos2++) { sum = (longerStr[pos2] - '0') + quotient; quotient = sum/10; residue = sum % 10; res += residue + '0'; }//add the extra carry while(quotient != 0) { residue = quotient % 10; quotient = quotient/10; res += residue + '0'; }//std::cout<<"res size:"<>n; for(i = 1; i <= n; i++) cin>>data[i]; takeOff[1] = "1"; for(i = 2; i <= n; i++) { takeOff[i] = bigIntegerAdd(takeOff[i - 1], takeOff[i - 1]); takeOff[i] = bigIntegerAdd(takeOff[i], "1"); } string res = f(n); for(i = res.length() - 1; i >= 0; i--) cout<

좋은 웹페이지 즐겨찾기