(DP) POI Bytecomputer
Bytecomputer
Memory limit: 128 MB
A sequence of integers from the set is given. The bytecomputer is a device that allows the following operation on the sequence: incrementing by for any . There is no limit on the range of integers the bytecomputer can store, i.e., each can (in principle) have arbitrarily small or large value.
Program the bytecomputer so that it transforms the input sequence into a non-decreasing sequence (i.e., such that ) with the minimum number of operations.
Input
The first line of the standard input holds a single integer (), the number of elements in the (bytecomputer's) input sequence.
The second line contains integers () that are the successive elements of the (bytecomputer's) input sequence, separated by single spaces.
In tests worth 24% of the total points it holds that , and in tests worth 48% of the total points it holds that .
Output
The first and only line of the standard output should give one integer, the minimum number of operations the bytecomputer has to perform to make its input sequence non-decreasing, of the single word BRAK (Polish for none) if obtaining such a sequence is impossible.
Example
For the input data:
6 -1 1 0 -1 0 1
the correct result is:
3
Explanation of the example: with three operations, the bytecomputer can obtain the sequence .
Sample grading tests:
- 0grade: , a small test with the answer BRAK;
- 1grade: , a small test with the answer ;
- 2grade: , all the elements in the sequence equal ;
- 3grade: , , , ;
- 4grade: , , , and .
Task author: Jacek Tomasiewicz.
题意:
a[x]=a[x-1]+a[x]要让序列递增
那么无非是 -1 -1 -1 -1 0 0 0 0 0 0 0 1 1 1 1 1 1 11 11 1 1
f[i][j]填写到i个数字这个数字为j然后转移就好了
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<algorithm> #include<cstdlib> #include<queue> #include<vector> #include<stack> #define INF 100000000 using namespace std; int dp[1000005][3],n,a[1000005]; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=1;i<=n;i++) { for(int j=0;j<=2;j++) dp[i][j]=INF; } dp[1][a[1]+1]=0; for(int i=1;i<n;i++) { for(int j=0;j<=2;j++) { for(int k=0;k<=2;k++) { if(dp[i][j]==INF) continue; int newj; newj=a[i+1]+(j-1)*k; if(newj>=-1&&newj<=1&&newj>=(j-1)) dp[i+1][newj+1]=min(dp[i+1][newj+1],dp[i][j]+k); } } } int ans=INF; for(int i=0;i<=2;i++) ans=min(ans,dp[n][i]); if(ans==INF) printf("BRAK\n"); else printf("%d\n",ans); return 0; }