L Payment 线性dp

问题 L: Payment
时间限制: 1 Sec 内存限制: 128 MB

题目描述
In the Kingdom of AtCoder, only banknotes are used as currency. There are 10100+1 kinds of banknotes, with the values of 1,10,102,103,…,10(10^100). You have come shopping at a mall and are now buying a takoyaki machine with a value of N. (Takoyaki is the name of a Japanese snack.)

To make the payment, you will choose some amount of money which is at least N and give it to the clerk. Then, the clerk gives you back the change, which is the amount of money you give minus N.

What will be the minimum possible number of total banknotes used by you and the clerk, when both choose the combination of banknotes to minimize this count?

Assume that you have sufficient numbers of banknotes, and so does the clerk.

Constraints
·N is an integer between 1 and 101,000,000 (inclusive).

输入
Input is given from Standard Input in the following format:

N

输出
Print the minimum possible number of total banknotes used by you and the clerk.
样例输入 Copy
【样例1】
36
【样例2】
91
【样例3】
314159265358979323846264338327950288419716939937551058209749445923078164062862089986280348253421170
样例输出 Copy
【样例1】
8
【样例2】
3
【样例3】
243
提示
样例1解释
If you give four banknotes of value 10 each, and the clerk gives you back four banknotes of value 1 each, a total of eight banknotes are used.
The payment cannot be made with less than eight banknotes in total, so the answer is 8.
样例2解释
If you give two banknotes of value 100,1, and the clerk gives you back one banknote of value 10, a total of three banknotes are used.

线性dp,分两类,一类是直接给服务员x张,另一类是服务员给他10-x张,让后他给服务员1张。
还要注意一下会有进位,所以最后面加一位0来处理进位。比如说99,按第二类分的话加一会进到9前面一位。

#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#define X first
#define Y second
using namespace std;

typedef long long LL;
typedef pair<int,int> PII;

const int N=1000010,mod=1e9+7;

LL gcd(LL a,LL b) {return b? gcd(b,a%b):a;};

string s;
vector<int>v;
LL f[2][N];

int main()
{
//	ios::sync_with_stdio(false);
//	cin.tie(0);

	cin>>s;
	
	int l=s.length();
	for(int i=l-1;i>=0;i--)
		v.push_back(s[i]-'0');
	
	l=v.size();
	f[0][0]=v[0];
	f[1][0]=10-v[0];
	v.push_back(0);
	for(int i=1;i<=l;i++)
	{
		int t1=v[i],t2=10-v[i];
		f[0][i]=min(f[0][i-1]+t1,f[1][i-1]+t1+1);
		f[1][i]=min(f[0][i-1]+t2,f[1][i-1]+t2-1);
	}

	cout<<min(f[0][l],f[1][l])<<endl;

















	return 0;
}










发布了43 篇原创文章 · 获赞 1 · 访问量 1580

猜你喜欢

转载自blog.csdn.net/DaNIelLAk/article/details/105014095
l
今日推荐