Day4 morning Problem Solving report, day4 morning Problem Solving

Source: Internet
Author: User

Day4 morning Problem Solving report, day4 morning Problem Solving
Expected score: 50 + 0 + 0 = 50 actual score: 50 + 0 + 10 = 60 subjects with cancer, T3 does not give brute force score (*  ̄) T1

Https://www.luogu.org/problem/show? Pid = T15564

Greedy,

But I don't know how to maintain it.

 

1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 # include <algorithm> 6 using namespace std; 7 const int MAXN = 1e6; 8 const int INF = 0x7ffff; 9 inline int read () 10 {11 char c = getchar (); int flag = 1, x = 0; 12 while (c <'0' | c> '9') {if (c = '-') flag =-1; c = getchar ();} 13 while (c> = '0' & c <= '9') x = x * 10 + c-48, c = getchar (); return x * flag; 14} 15 int zero; 16 int n; 17 int a [MAXN]; 18 int tmp [MAX N]; 19 int main () 20 {21 // freopen ("multiset. in "," r ", stdin); 22 // freopen (" multiset. out "," w ", stdout); 23 n = read (); 24 for (int I = 1; I <= n; I ++) a [I] = read (); 25 sort (a + 1, a + n + 1); 26 int now = 0; 27 int num = n; 28 int bg = INF; 29 int ans = 0; 30 while (num! = 1) 31 {32 zero = 0; bg = INF; 33 for (int I = 1; I <= num; I ++) 34 {35 if (a [I] = 0) zero ++; 36 if (a [I]> = 1) 37 {38 a [I] --; 39 if (bg = INF) bg = I; 40} 41 42} 43 int nownum = 0; 44 for (int I = 1; I <= zero/2; I ++) 45 tmp [++ nownum] = 0; 46 if (zero % 2 = 1) 47 tmp [++ nownum] = 0; 48 for (int I = bg; I <= num; I ++) 49 tmp [++ nownum] = a [I]; 50 for (int I = 1; I <= nownum; I ++) a [I] = tmp [I]; 51 num = nownum; 52 ans ++; 53} 54 printf ("% d ", ans); 55 return 0; 56} 57 58 59/* 60 561 0 0 3 3 // 562 263 2 1 // 364 465 1 0 2 3 // 466 67 */50 points of violence

 

Positive Solution

Consider how to move from the final state

Greedy ideas:

Make the value not 0 smaller.

Reduce the number of zeros.

Violence-"50

How to optimize

Use a bucket to record,

A [0]-> (a [0] + 1)/2-> a [1]

 

1 # include <cstdio> 2 # include <iostream> 3 # include <algorithm> 4 # include <cstring> 5 # include <climits> 6 # include <cstdlib> 7 # include <cmath> 8 # include <queue> 9 # include <vector> 10 # include <utility> 11 using namespace std; 12 const int MAXN = 5e6 + 60, MX = 1e6; 13 int N, a [MAXN], cnt [MAXN], res, lim; 14 15 int main () 16 {17 freopen ("multiset. in "," r ", stdin); 18 freopen (" multiset. out "," w ", stdout); 19 scanf (" % d ", & N); 20 for (int I = 1; I <= N; ++ I) {21 scanf ("% d", & a [I]); 22 lim = max (lim, a [I]); 23 ++ cnt [a [I]; 24} 25 int l = 0, z = cnt [0]; 26 for (int I = 1; I <= lim; ++ I) {27 ++ res; 28 z = (z + 1)/2; 29 z + = cnt [I]; 30} 31 for (; z> 1; z = (z + 1)/2) + res; 32 printf ("% d \ n", res); 33 return 0; 34}View Code

 

T2

 

I didn't understand the example at the beginning.

For a long time, I thought he meant to re-mark each group ..

It wasn't until I asked zzx in the last 20 minutes that I found that there was a brute force score of 50 !!!! (Lyq has been tested for 80) {{{( >_< )}}}

 

Positive Solution:

Method 1: Add edge in brute force mode. Each BFS operation can add a pruning rule. BFS is used only for vertices that need to be added each time. Complexity: $ O (m) $

Method 2: one-way parallel query

Method 3: For each vertex

Multiply the length of the split to ensure that the length is between $2 ^ ~ 2 ^ p $, second in this interval, time complexity $ O (m * log) $

 

1 # include <iostream> 2 # include <cstdio> 3 # include <cstdlib> 4 # include <cstring> 5 # include <algorithm> 6 # include <cmath> 7 # include <vector> 8 using namespace std; 9 const int N = 500010; 10 typedef long ll; 11 12 int n, m; 13 int vis [N], u [N], v [N]; 14 vector <int> vec [N]; 15 16 bool dfs (int u) 17 {18 if (vis [u]) return false; 19 if (u = n) return true; 20 vis [u] = 1; 21 bool ret = false; 22 for (int I = 0; I <vec [u]. size (); I ++) 23 {24 ret = dfs (vec [u] [I]); 25 if (ret) return true; 26} 27 return false; 28} 29 30 int read () 31 {32 char ch = getchar (); 33 int x = 0; 34 while (! Isdigit (ch) ch = getchar (); 35 while (isdigit (ch) {x = x * 10 + (ch-'0 '); ch = getchar () ;}36 return x; 37} 38 39 bool check (int sta, int las) 40 {41 for (int I = sta; I <= las; I ++) 42 vec [u [I]. push_back (v [I]); 43 44 bool ret = dfs (1); 45 46 for (int I = sta; I <= las; I ++) 47 {48 vis [u [I] = vis [v [I] = 0; 49 vec [u [I]. clear (); 50} 51 vis [1] = 0; 52 return ret; 53} 54 55 56 57 int main () 58 {59 freopen ("road. in ", "R", stdin); 60 freopen ("road. out "," w ", stdout); 61 n = read (), m = read (); 62 for (int I = 0; I <m; I ++) 63 {64 // scanf ("% d", & u [I], & v [I]); 65 u [I] = read (); v [I] = read (); 66} 67 int now = 0, ans = 0; 68 while (now <m) 69 {70 int I; 71 for (I = 1; I + now <= m; I <= 1) 72 if (check (now, now + I-1) break; 73 I> = 1; 74 int nowtmp = now + I; 75 for (; I> 0; I> = 1) 76 if (nowtmp + I <= m &&! Check (now, nowtmp + I-1) 77 nowtmp + = I; 78 ans ++; 79 now = nowtmp; 80 81} 82 cout <ans <endl; 83}Positive Solution

 

 

 

T3

At first, yy had a self-righteous greed.

Then it took more than an hour to get violent.

Only n <= 10 can be detected. Greedy groups have two wrong groups.

GG...

Later, it was inexplicably more than a point ....

 

 

Positive Solution

Greedy

Expected sequence: 1 2 3 4 5

5 5 5 5 5

4 3 2 1 0

1 2 3 4 5

$ F [I] [j] $ a soldier with a blood volume of $ I $, empty $ j $ knife

Deformation of the backpack ..

Maintain with one stack

 

1 # include <iostream> 2 # include <cstdio> 3 # include <cstdlib> 4 # include <algorithm> 5 # include <cmath> 6 # include <cstring> 7 using namespace std; 8 9 const int MAXN = 1000 + 10; 10 int a [MAXN]; 11 int cnt [MAXN], sta [MAXN], c [MAXN]; 12 int f [MAXN] [MAXN]; 13 14 int main () 15 {16 freopen ("cs. in "," r ", stdin); 17 freopen (" cs. out "," w ", stdout); 18 int T; 19 scanf (" % d ", & T); 20 for (int num = 1; num <= T; ++ n Um) 21 {22 int N, maxn = 0; 23 scanf ("% d", & N); 24 memset (cnt, 0, sizeof (cnt )); 25 memset (c, 0, sizeof (c); 26 memset (f, 0, sizeof (f); 27 for (int I = 1; I <= N; ++ I) 28 {29 scanf ("% d", & a [I]); ++ cnt [a [I]; 30 maxn = max (maxn, a [I]); 31} 32 int top = 0; 33 for (int I = 1; I <= maxn; ++ I) 34 if (cnt [I] = 0) sta [++ top] = I; else35 {36 while (cnt [I]> 1 & top> 0) {c [sta [top --] = I; -- cnt [I];} 37 c [I] = I; 38} 39 40 int ans = 0; 41 for (int I = 1; I <= maxn; ++ I) 42 for (int j = 0; j <= I; ++ j) 43 {44 if (j> 0) f [I] [j] = f [I-1] [j-1]; 45 if (c [I]! = 0 & j + c [I]-I <I) 46 f [I] [j] = max (f [I] [j], f [I-1] [j + c [I]-I] + 1); 47 ans = max (ans, f [I] [j]); 48} 49 printf ("% d \ n", ans); 50} 51 return 0; 52}View Code

 

 

 

Summary

This exam made too many mistakes ..

T2 clearly has 80 brute force points

It's about T3 ....

 

Related Article

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.