Bzoj1584: [usaco 2009 Mar] cleaning up cleaning

Source: Internet
Author: User

 

Description

 

There are native cows, each of which has a PI, 1 <= pI <= m <= n <= 40000. Now Farmer John is going to divide these cows into several sections and define the non-crab degree of each section as: if there are k different numbers in this section, then the non-crab degree is K * K. Then the total "no crab" degree is the sum of "no crab degree" in all segments.

Question: http://www.lydsy.com/JudgeOnline/problem.php? Id = 1584

Question:

Comparison of God's DP... How have we met God's questions recently...

Write some comments in the code

1 # include <cstdio> 2 3 # include <cstdlib> 4 5 # include <cmath> 6 7 # include <cstring> 8 9 # include <algorithm> 10 11 # include <iostream> 12 13 # include <vector> 14 15 # include <map> 16 17 # include <set> 18 19 # include <queue> 20 21 # include <string> 22 23 # define INF 100000000024 25 # define maxn 5000026 27 # define maxm 500 + 10028 29 # define EPS 1e-1030 31 # define ll long long32 33 # define PA pair <int, int> 34 35 # define for0 (I, n) for (INT I = 0; I <= (n); I ++) 36 37 # define for1 (I, n) for (INT I = 1; I <= (n); I ++) 38 39 # define for2 (I, x, y) for (INT I = (X ); I <= (y); I ++) 40 41 # define for3 (I, x, y) for (INT I = (x); I> = (y ); I --) 42 43 # define mod 100000000744 45 using namespace STD; 46 47 inline int read () 48 49 {50 51 int x = 0, F = 1; char CH = getchar (); 52 53 While (CH <'0' | ch> '9') {If (CH = '-') F =-1; ch = getchar ();} 54 while (CH> = '0' & Ch <= '9') {x = 10 * x + CH-'0 '; ch = getchar ();} 56 57 Return x * F; 58 59} 60 int n, m, a [maxn], B [maxn], C [maxn], f [maxn], pre [maxn]; 61 62 int main () 63 64 {65 66 freopen ("input.txt", "r", stdin ); 67 68 freopen ("output.txt", "W", stdout); 69 70 N = read (); M = SQRT (N ); 71 for1 (I, n) A [I] = read (); 72 memset (F, 60, sizeof (f); 73 memset (PRE,-1, sizeof (pre); // pre [I] indicates the position where I appeared in the previous sequence 74 f [0] = 0; 75 for1 (I, n) 76 {77 for1 (J, m) if (pre [A [I] <= B [J]) C [J] ++; // B [J] indicates that B [J] + 1 to I has <= J different numbers 78 pre [A [I] = I; 79 for1 (J, m) 80 If (C [J]> J) // C [J] indicates the number of different numbers from B [J] + 1 to I, if J is clearly not defined in B [J], delete 81 {82 int T = B [J] + 1; // pointer 83 while (pre [A [T]> T) T ++; // scan forward, pre [A [T]> T indicates that there is still a [T] between T and I, so we continue to move forward, knowing that there is no successor to a number, meaning 84 B [J] = t after I; C [J] --; 85} 86 for1 (J, m) f [I] = min (F [I], f [B [J] + J * j); 87} 88 printf ("% d \ n", F [N]); 89 90 return 0; 91 92}
View code

 

 

 

 

 

 

 

 

Bzoj1584: [usaco 2009 Mar] cleaning up cleaning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.