天天看點

Codeforces Round #279 (Div. 2) E. Restoring Increasing Sequence

用了一個簡單易懂但是實作比較慢的方法

貪心,先用含有問号串構造出最大的滿足條件的值(即?用9代替),如果再挨個位數比較,如果減下去還能大于上一個的值,則就減

進而使得現在的數是滿足條件中最小的。因為是從可能的最大的開始減少,如果還是比前一個小則不可以;因為前面的前部都是滿足條件中最小的數,是以給後面的數留出了最大的改變空間。

//      whn6325689
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>


using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef complex<ld> point;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef vector<int> vi;

#define CLR(x,y) memset(x,y,sizeof(x))
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define lowbit(x) (x&(-x))
#define MID(x,y) (x+((y-x)>>1))
#define eps 1e-9
#define INF 0x3f3f3f3f
#define LLINF 1LL<<62

template<class T>
inline bool read(T &n)
{
    T x = 0, tmp = 1; char c = getchar();
    while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();
    if(c == EOF) return false;
    if(c == '-') c = getchar(), tmp = -1;
    while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();
    n = x*tmp;
    return true;
}
template <class T>
inline void write(T n)
{
    if(n < 0)
    {
        putchar('-');
        n = -n;
    }
    int len = 0,data[20];
    while(n)
    {
        data[len++] = n%10;
        n /= 10;
    }
    if(!len) data[len++] = 0;
    while(len--) putchar(data[len]+48);
}
//-----------------------------------

const int MAXN=1000010; 

string s[MAXN];
int ten[9];
int a[MAXN];

int main()
{
    int n;
    read(n);
    for(int i=1;i<=n;i++)
		cin>>s[i];
    ten[0]=1;
    for(int i=1;i<9;i++)
		ten[i]=ten[i-1]*10;
    for(int i=1;i<=n;i++)
	{
        int len=s[i].size();
        int v=0;
        for(int j=0;j<len;j++)
		{
            if(s[i][j]=='?')
				v=v*10+9;
            else
                v=v*10+s[i][j]-'0';
        }
        for(int j=0;j<len;j++)
		{
            if(s[i][j]=='?')
			{
                for(int k=(!j)?1:0;k<=9;k++)
                    if(v-ten[len-1-j]*(9-k)>a[i-1])
					{
                        v-=ten[len-1-j]*(9-k);
                        break;
                    }
            }
        }
        if(v>a[i-1])
			a[i]=v;
        else
   			goto here;
    }
    puts("YES");
    for(int i=1;i<=n;i++)
		write(a[i]),putchar('\n');
    return 0;
here:
	puts("NO");
	return 0;
}