1167. Bicolored Horses(dp)

  1. Bicolored Horses
    Time limit: 1.0 second
    Memory limit: 64 MB
    Every day, farmer Ion (this is a Romanian name) takes out all his horses, so they may run and play. When they are done, farmer Ion has to take all the horses back to the stables. In order to do this, he places them in a straight line and they follow him to the stables. Because they are very tired, farmer Ion decides that he doesn’t want to make the horses move more than they should. So he develops this algorithm: he places the 1st P1 horses in the first stable, the next P2 in the 2nd stable and so on. Moreover, he doesn’t want any of the K stables he owns to be empty, and no horse must be left outside. Now you should know that farmer Ion only has black or white horses, which don’t really get along too well. If there are i black horses and j white horses in one stable, then the coefficient of unhappiness of that stable is i*j. The total coefficient of unhappiness is the sum of the coefficients of unhappiness of every of the K stables.
    Determine a way to place the N horses into the K stables, so that the total coefficient of unhappiness is minimized.
    Input
    On the 1st line there are 2 numbers: N (1 ≤ N ≤ 500) and K (1 ≤ K ≤ N). On the next N lines there are N numbers. The i-th of these lines contains the color of the i-th horse in the sequence: 1 means that the horse is black, 0 means that the horse is white.
    Output
    You should only output a single number, which is the minimum possible value for the total coefficient of unhappiness.
    Sample
    input output
    6 3
    1
    1
    0
    1
    0
    1
    2
    Notes
    Place the first 2 horses in the first stable, the next 3 horses in the 2nd stable and the last horse in the 3rd stable.
    Đề ý:
    Mỗi ngày, nông dân Ion ( đây là Rumani người tên gọi ) đều sẽ lấy ra hắn sở hữu ngựa, cho nên bọn họ có thể chạy bộ cùng chơi đùa. Khi bọn hắn hoàn thành sau, nông dân Ion cần thiết đem sở hữu mã mang về chuồng ngựa. Vì làm được điểm này, hắn đem bọn họ xếp thành một cái thẳng tắp, bọn họ đi theo hắn đi chuồng ngựa. Bởi vì bọn họ phi thường mệt mỏi, nông dân Ion quyết định hắn không nghĩ làm ngựa di động đến so với bọn hắn hẳn là càng nhiều. Cho nên hắn khai phá cái này thuật toán: Hắn đem cái thứ nhất P 1 mã đặt ở cái thứ nhất ổn định trung, tiếp theo cái P 2 đặt ở cái thứ hai ổn định trung, y này loại suy. Ngoài ra, hắn không hy vọng hắn sở có được bất luận cái gì K chuồng ngựa đều là trống không, hơn nữa không được đem mã lưu tại bên ngoài. Hiện tại ngươi hẳn là biết, nông dân ly tử chỉ có màu đen hoặc màu trắng ngựa, chúng nó cũng không có chân chính ở chung rất khá. Nếu có ta hắc mã cùng j con ngựa ở một cái ổn định, sau đó nên ổn định không hạnh phúc hệ số là i * j. Không hạnh phúc tổng hệ số là mỗi cái K ổn định hệ số bất hạnh hệ số tổng hoà.
    Xác định đem N con ngựa để vào K chuồng ngựa phương pháp, để sử bất hạnh tổng hệ số nhỏ nhất hóa.
    dp[i][j] tỏ vẻ dùng i cái chuồng ngựa an trí j con ngựa
    i-1 cái chuồng ngựa an trí t con ngựa, dư lại mã an trí đến một gian
    Trạng thái dời đi phương trình dp[i][j]=dp[i-1][j-1]+unhappy( dư lại mã );
    Chú ý khởi động lại dp INF dp[0][0]=0
#include <stdio.h>
#include <math.h>
#include <queue>
#include <memory.h>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
int b[505],unhappy[505][505];
int dp[505][505];
int num_unhappy(int x,int y)// cầu i,j chi gian unhappy giá trị
{
int a1,b1;
a1=b[y]-b[x-1];// hắc mã số lượng
b1=y-x+1-a1;// bạch mã
return a1*b1;
}
int main()
{
int n,k,i,j,t,x;
memset(dp,INF,sizeof(dp));
memset(b,0,sizeof(b));
scanf( "%d%d",&n,&k);
for(i=1;i<=n;i++)
{
scanf( "%d",&x);
b[i]=b[i-1]+x;
}
for(i=1;i<=n;i++)
{
for(j=i;j<=n;j++)
{
unhappy[i][j]=num_unhappy(i,j);
}
}
dp[0][0]=0;
for(i=1;i<=k;i++)// chuồng ngựa
{
for(j=1;j<=n;j++)// mã
{
for(t=1;t<=j;t++)
{
dp[i][j]=min(dp[i][j],dp[i-1][t-1]+unhappy[t][j]);
}
}
}
printf( "%d\n",dp[k][n]);
return 0;
}
Bình luận
Tăng thêm bao lì xì

Thỉnh điền bao lì xì chúc phúc ngữ hoặc tiêu đề

Cái

Bao lì xì cái số nhỏ nhất vì 10 cái

Nguyên

Bao lì xì kim ngạch thấp nhất 5 nguyên

Trước mặt ngạch trống3.43Nguyên Đi trước nạp phí >
Cần chi trả:10.00Nguyên
Thành tựu một trăm triệu kỹ thuật người!
Lĩnh sau ngươi sẽ tự động trở thành bác chủ cùng bao lì xì chủ fans Quy tắc
hope_wisdom
Phát ra bao lì xì
Thật phóNguyên
Sử dụng ngạch trống chi trả
Điểm đánh một lần nữa thu hoạch
Quét mã chi trả
Tiền bao ngạch trống 0

Để khấu thuyết minh:

1. Ngạch trống là tiền bao nạp phí giả thuyết tiền, dựa theo 1:1 tỉ lệ tiến hành chi trả kim ngạch để khấu.
2. Ngạch trống vô pháp trực tiếp mua sắm download, có thể mua sắm VIP, trả phí chuyên mục cập chương trình học.

Ngạch trống nạp phí