codeforces 509C
给出一个序列,代表原序列对应位置数的每一位的数字之和,原序列单调递增,问原序列的最后一个数最小的方案每一个数是多少。
#include
#include
#include
#include
#define MAX 500
using namespace std;
int s[2][MAX];
int len[MAX];
int n,a[MAX];
void print ( int i )
{
int x = i%2;
for ( int j = len[i]-1 ; j >= 0 ; j-- )
printf ( "%d" , s[x][j] );
puts ("");
}
int main ( )
{
while ( ~scanf ( "%d" , &n ) )
{
for ( int i = 0 ; i < n ; i++ )
scanf ( "%d" , &a[i] );
memset ( len , 0 , sizeof ( len ));
int temp = a[0];
while ( temp )
{
if ( temp > 8 )
{
s[0][len[0]++] = 9;
temp -= 9;
}
else
{
s[0][len[0]++] = temp;
temp = 0;
}
}
s[0][len[0]] = 0;
print ( 0 );
for ( int i = 1 ; i < n ; i++ )
{
int x = i%2;
int y = (i-1)%2;
memset ( s[x] , 0 , sizeof ( s[x] ));
if ( a[i] > a[i-1] )
{
int d = a[i]-a[i-1];
int j = 0;
while ( d )
{
int tt = s[y][j];
if ( s[y][j] != 9 )
{
if ( d >= 9-tt )
{
d -= 9 -tt;
s[x][len[i]++] = 9;
}
else
{
s[x][len[i]++] = tt+d;
d = 0;
}
}
else s[x][len[i]++] = 9;
j++;
}
for ( int k = j ; k < len[i-1] ; k++ )
s[x][len[i]++] = s[y][k];
}
else
{
int d = a[i-1]-a[i]+1;
for ( int j = 1 ; j <= len[i-1] ; j++ )
{
int sum = -d;
for ( int k = j-1 ; k >= 0 ; k-- )
{
int tt = s[y][k];
sum += tt;
}
if ( sum < 0 ) continue;
for ( int k = 0 ; k < j ; k++ )
{
if ( sum >= 9 )
{
s[x][k] = 9;
sum -= 9;
}
else
{
s[x][k] = sum;
sum = 0;
}
}
if ( sum == 0 && s[y][j] != 9 )
{
s[x][j] = s[y][j]+1;
if ( j == len[i-1] ) len[i] = len[i-1]+1;
else len[i] = len[i-1];
for ( int k = j+1 ; k < len[i] ; k++ )
s[x][k] = s[y][k];
break;
}
}
}
print ( i );
}
}
}