2017.10.23

来源:互联网 发布:淘宝没有盗版书了 编辑:程序博客网 时间:2024/05/21 19:22

CodeForces - 225C

You’ve got an n × m pixel picture. Each pixel can be white or black. Your task is to change the colors of as few pixels as possible to obtain a barcode picture.

A picture is a barcode if the following conditions are fulfilled:

All pixels in each column are of the same color.
The width of each monochrome vertical line is at least x and at most y pixels. In other words, if we group all neighbouring columns of the pixels with equal color, the size of each group can not be less than x or greater than y.
Input
The first line contains four space-separated integers n, m, x and y (1 ≤ n, m, x, y ≤ 1000; x ≤ y).

Then follow n lines, describing the original image. Each of these lines contains exactly m characters. Character “.” represents a white pixel and “#” represents a black pixel. The picture description doesn’t have any other characters besides “.” and “#”.

Output
In the first line print the minimum number of pixels to repaint. It is guaranteed that the answer exists.

Example
Input
6 5 1 2

.#.

.###.

..

.##.#

..

Output
11
Input
2 5 1 1

#

…..
Output
5
Note
In the first test sample the picture after changing some colors can looks as follows:

.##..
.##..
.##..
.##..
.##..
.##..
In the second test sample the picture after changing some colors can looks as follows:

.#.#.
.#.#.

dp
主要是写转移方程

#include<iostream>#include<cstring>using namespace std;char mapp[2000][2000];int a[2000],b[2000];int dp[2000][2];int main (){    int n,m,x,y;    while(cin>>n>>m>>x>>y)    {        memset(a,0,sizeof(a));        memset(b,0,sizeof(b));        memset(dp,0x3f,sizeof(dp));        for(int i=1;i<=n;i++)        {            for(int j=1;j<=m;j++)            {                scanf(" %c",&mapp[i][j]);                if(mapp[i][j]=='#')                {                    a[j]++;                }                else                {                    b[j]++;                }            }        }        int i,j;        for(i=1;i<=m;i++)        {            a[i]+=a[i-1];            b[i]+=b[i-1];        }        dp[0][0]=0;        dp[0][1]=0;        for(i=0;i<m;i++)        {            for(j=x;j<=y;j++)            {                dp[i+j][0]=min(dp[i+j][0],dp[i][1]+a[i+j]-a[i]);                dp[i+j][1]=min(dp[i+j][1],dp[i][0]+b[i+j]-b[i]);            }        }        cout<<min(dp[m][0],dp[m][1])<<endl;    }}

ACdream - 1421
Charlie is going to take part in one famous TV Show. The show is a single player game. Initially the player has 100 dollars. The player is asked n questions, one after another. If the player answers the question correctly, the sum he has is doubled. If the answer is incorrect, the player gets nothing and leaves the show.
Before each question the player can choose to leave the show and take away the prize he already has. Also once in the game the player can buy insurance. Insurance costs c dollars which are subtracted from the sum the player has before the question. Insurance has the following effect: if the player answers the question correctly his prize is doubled as usually, if the player answers incorrectly the prize is not doubled, but the game continues. The player must have more than c dollars to buy insurance.
Charlie’s friend Jerry works on TV so he managed to steal the topics of the questions Charlie will be asked. Therefore for each question i Charlie knows p i — the probability that he will answer this question correctly. Now Charlie would like to develop the optimal strategy to maximize his expected prize. Help him.
Input
The first line of the input file contains two integer numbers n and c (1 ≤ n ≤ 50, 1 ≤ c ≤ 10 9). The second line contains n integer numbers ranging from 0 to 100 — the probabilities that Charlie will answer questions correctly, in percent.
Output
Output one real number — the expected prize of Charlie if he follows the optimal strategy. Your answer must have relative or absolute error within 10 - 8
.
Sample Input
2 100
50 50
2 50
50 50
2 50
60 0
Sample Output
100
112.5
120
Hint
The optimal strategy in the second example is to take insurance for the second question. In this case the expected prize is 1/2 × 0 + 1/2 × (1/2 × 150 + 1/2 × 300).
In the third example it is better to leave the show after the first question, because there is no reason to try to answer the second one.

搜索dfs,递归的过程需要注意,还有过程中用double。

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespace std;int n;double m;double a[210];double dfs(int s,double sum,int flag){    if(s>n)    {        return sum;    }    else    {        double ans=max(sum,a[s]*dfs(s+1,sum*2,flag));        double res=0;        if(flag==0&&sum>m)        {            res+=a[s]*dfs(s+1,(sum-m)*2,1);            res+=(1-a[s])*dfs(s+1,sum-m,1);        }        ans=max(res,ans);        return ans;    }}int main (){    while(~scanf("%d%lf",&n,&m))    {        int i;        for(i=1;i<=n;i++)        {            scanf("%lf",&a[i]);            a[i]=a[i]/100;        }        double ans=dfs(1,100,0);        printf("%f\n",ans);    }} 
原创粉丝点击