【DP】Gym100524E Ebola Virus

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_32506797/article/details/84885527

Souce:2014-2015 Summer Petrozavodsk Camp, Andrew Stankevich Contest 46 (ASC 46)
Problem:
n个村庄,如果第i个村庄没有被治疗,每天会死掉a[i]个人。现在起始在第一个村庄,每次选择花费一天的时间 往前走/往后走/治疗当前村庄,使得总共死去的人数最少。
但行动有个限制,如果你的前进方向上有一个曾经被路过但没有被治疗的村庄,那么直到这个村庄被治疗之前,你都不能改变你的方向,并且当你经过这个村庄时,必须治疗他。
Idea:
dp[l][r]dp[l][r]表示当前在l点,一直前进到rr点再折回到l点所需要的最小花费。答案一定是由数个区间拼接而成的。
对于llrr都固定时,在治疗村庄时ii,把后面所有村庄的贡献都归到ii身上,那么可以得到ii点的贡献为min((il)a[i]+s[r]s[i],(3r2il)a[i])min((i-l)*a[i]+s[r]-s[i], (3*r-2*i-l)*a[i])
注意到抠出式子中的la[i]-l*a[i]可以使DP优化到O(N2)O(N^2)
Code:

#include<bits/stdc++.h>
using namespace std;

#define I inline
#define fi first
#define se second
#define pb push_back
#define ALL(X) (X).begin(), (X).end()
#define CLR(A, X) memset(A, X, sizeof(A))
#define bcnt(X) __builtin_popcountll(X)
typedef long long LL;
typedef pair<int, int> PII;

const int N = 3e3+10;

LL a[N], s[N], dp[N][N], f[N];

I void work() {
    int n; scanf("%d", &n);
    if(!n) exit(0);
    for(int i = 1; i <= n; i++) {
        scanf("%lld", a+i);
        s[i] = s[i-1]+a[i];
        f[i] = 1e18;
    }
    for(int r = 1; r <= n; r++) {
        for(int l = r; l >= 1; l--) {
            dp[l][r] = dp[l+1][r]+min(l*a[l]+s[r]-s[l], (3*r-2*l)*a[l]);
            LL tmp = dp[l][r]-l*(s[r]-s[l-1])+(4*(r-l)+2)*(s[n]-s[r]);
            f[r] = min(f[r], f[l-1]+tmp);
        }
    }
    printf("%lld\n", f[n]);
}

int main() {
    if(fopen("ebola.in", "r")) {
        freopen("ebola.in", "r", stdin);
        freopen("ebola.out", "w", stdout);
    }
    for(;;) work();
    return 0;
}

展开阅读全文

Detect the Virus

02-20

One day, Nobita found that his computer is extremely slow. After several hours' work, he finally found that it was a virus that made his poor computer slow and the virus was activated by a misoperation of opening an attachment of an email.nnNobita did use an outstanding anti-virus software, however, for some strange reason, this software did not check email attachments. Now Nobita decide to detect viruses in emails by himself.nnTo detect an virus, a virus sample (several binary bytes) is needed. If these binary bytes can be found in the email attachment (binary data), then the attachment contains the virus.nnNote that attachments (binary data) in emails are usually encoded in base64. To encode a binary stream in base64, first write the binary stream into bits. Then take 6 bits from the stream in turn, encode these 6 bits into a base64 character according the following table:nnThat is, translate every 3 bytes into 4 base64 characters. If the original binary stream contains 3k + 1 bytes, where k is an integer, fill last bits using zero when encoding and append '==' as padding. If the original binary stream contains 3k + 2 bytes, fill last bits using zero when encoding and append '=' as padding. No padding is needed when the original binary stream contains 3k bytes.nnValue 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31nEncoding A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e fnValue 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63nEncoding g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9 + /nFor example, to encode 'hello' into base64, first write 'hello' as binary bits, that is: 01101000 01100101 01101100 01101100 01101111nThen, take 6 bits in turn and fill last bits as zero as padding (zero padding bits are marked in bold): 011010 000110 010101 101100 011011 000110 111100nThey are 26 6 21 44 27 6 60 in decimal. Look up the table above and use corresponding characters: aGVsbG8nSince original binary data contains 1 * 3 + 2 bytes, padding is needed, append '=' and 'hello' is finally encoded in base64: aGVsbG8=nnSection 5.2 of RFC 1521 describes how to encode a binary stream in base64 much more detailedly:nnClick here to see Section 5.2 of RFC 1521 if you have interestnnHere is a piece of ANSI C code that can encode binary data in base64. It contains a function, encode (infile, outfile), to encode binary file infile in base64 and output result to outfile.nnClick here to see the reference C code if you have interestnnInputnnInput contains multiple cases (about 15, of which most are small ones). The first line of each case contains an integer N (0 <= N <= 512). In the next N distinct lines, each line contains a sample of a kind of virus, which is not empty, has not more than 64 bytes in binary and is encoded in base64. Then, the next line contains an integer M (1 <= M <= 128). In the following M lines, each line contains the content of a file to be detected, which is not empty, has no more than 2048 bytes in binary and is encoded in base64.nnThere is a blank line after each case.nnOutputnnFor each case, output M lines. The ith line contains the number of kinds of virus detected in the ith file.nnOutput a blank line after each case.nnSample Inputnn3nYmFzZTY0ndmlydXM=ndDogn1ndGVzdDogdmlydXMunn1nQA==n2nQA==nICAgICAgICA=nSample Outputnn2nn1n0n 问答

没有更多推荐了,返回首页