Luo Gu P2813 [the mothership] problem solution

The general idea: Enter the shield and attack, then quickly sort sort to go up,

After drained order from first start looking, if the attack is greater than the shield, shields continue to the next,

The attack was recorded as zero, if the rain, then it would continue to the next attack, shield does not move,

One of the most special places is a big part of the data points that the shield is 0 In this case, when the shield is zero, continue to the next to find a shield, but the attack would not move.

Let me then distributed parsing code

 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <complex>
#include <algorithm> #include <climits> #include <queue> #include <map> #include <set> #include <iomanip> using namespace std; int hudun[100001],gongji[100001];

It is edible header files, arrays used to doing, Pinyin lay qwq

Next

     scanf("%d%d",&n,&m);
    for(int i=1; i<=n; ++i) { scanf("%d",&hudun[i]);//护盾防御力  } for(int j=1; j<=m; ++j) { scanf("%d",&gongji[j]);//攻击力 }

Enter the number of defense systems and attack power

N and m are

Then go for loop into an array

then

Very simple sort quick sort, although this is very explosive, but still in use in this title may be the AC

Sort (+ hudun . 1, n-+ + hudun . 1); // Quick Sort  Sort (+ gongji . 1, m + + gongji . 1); // Quicksort

Note that the above must be +1 Oh, because when I entered above is from 1 to m or n to start rather than starting from 0, and 0 if it is from the beginning, that this is really useless, oh +1

Here comes the climax! ! ! ! ! ! ! ! ! ! ! !

The core code section! ! ! ! ! ! ! !

ANS = int 0, JS = . 1, JSS = . 1; // counter the while (! = n-+ JSS . 1) { // end condition, i.e. when the protective shield break all IF (JS + == m . 1 JSS &&! n-+ = . 1 ) {the printf ( " 0 \ n- " ); return 0 ;} IF (hudun [JSS] == 0) { // shield ++ zero when JSS ; Continue ;} the else IF (hudun [JSS] <gongji [JS]) { // can break when the shield gongji [JS] = 0 ; JSS ++ ; JS ++ ; Continue ;} the else {JS ++ ; Continue ;}}

ans is the final answer to do not ignore it, this is less than, js is counting attack was the first of several counter, then jss defense system is obvious counter it

All starting from the first look, if the shield is less than the attack, shields counter increments, attack power becomes zero, as can be seen as a shield and offset

If greater than or equal to, then it would continue to look for, which is a counter attack js accumulate as sort sorted so the more the greater the damage to the back to find

Special sentenced front shield is 0, the only shield counter jss accumulate enough

End condition when the attack ran out but the shield there is a surplus, then it is like the end of the output 0

for ( int I = . 1; I <= m; ++ I) { // the printf ( "% D \ n-", gongji [I]); gongji ANS = + [I]; // in turn add } the printf ( " D% \ n- ", ANS); // output return 0;

Finally, starting from the first accumulation, have become consumed 0, so a direct increase like, out of the number of injuries plus is the ability to cause

The complete code to give it qwq

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <complex>
 6 #include <algorithm>  7 #include <climits>  8 #include <queue>  9 #include <map> 10 #include <set> 11 #include <iomanip> 12 13 using namespace std; 14 15 int hudun[100001],gongji[100001]; 16 17 int main() { 18 int n,m; 19 scanf("%d%d",&n,&m); 20 for(I = int . 1; I <= n-; ++ I) { 21 is Scanf ( " % D ", & hudun [I]); // shield defense 22 is } 23 is for ( int J = . 1; J <= m; ++ J) { 24 Scanf ( " % D ", & gongji [J]); // attack 25 } 26 is Sort (hudun + . 1, hudun + n-+ . 1); // quicksort 27 Sort (gongji + . 1, gongji + m + 1); // quicksort 28 int ANS = 0, JS = 1, JSS = 1; // counter 29 the while (n-JSS = +! 1) { //End condition, i.e. when the protective shield break all 30 IF (JS + == m . 1 && n-JSS + =! . 1 ) { 31 is the printf ( " 0 \ n- " ); 32 return 0 ; 33 is } 34 is IF (hudun [ JSS] == 0) { // shield zero time 35 JSS ++ ; 36 Continue ; 37 [} the else IF (hudun [JSS] <gongji [JS]) { // can break when the shield 38 gongji [js] = 0 ; 39 ++ JSS ; 40 ++ JS ; 41 is Continue ; 42 is} the else {43 is JS ++ ; 44 is Continue ; 45 } 46 is } 47 for ( int I = . 1; I <= m; ++ I) { 48 // the printf ( "% D \ n-", gongji [I]); 49 ANS + = gongji [I]; // in turn add 50 } 51 is the printf ( " % D \ n- ", ANS); // output 52 is return 0 ; 53 is}

 

Guess you like

Origin www.cnblogs.com/acioi/p/11242106.html