AtCoder Beginner Contest 153 F - Silver Fox vs Monster

Topic Link https://atcoder.jp/contests/abc153/tasks/abc153_f

Meaning of the questions: There are a few monsters in the coordinate axes, each monster has a corresponding value of life hi, you can for them shelling, shelling every time you can damage A team monster in the D range before and after that point, at least ask to shelled many times.

My initial idea was to sort of sweep to the left of the monster, the first shelling, killing him, and then recorded how many times the shelling, then the monster 2d are shelling it from the subsequent discovery timeout

code show as below:

inline ll read(){
   ll s=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
   return s*w;
}
const int N = 2e5+5;
struct mon
{
        ll x,h;
};
mon monster[N];
bool cmp(mon a, mon b)
{
    return a.x < b.x;
}
int main()
{
    //ios::sync_with_stdio(false);cin.tie(NULL);
    ll n, d, a;
    n = read();
    d = read();
    a = read();
    for (int i = 0; i < n; i++)
    {
        monster[i].x = read();
        monster[i].h= read();
    }
    sort(monster, monster + n,cmp);
    ll ans = 0;
    for (int i = 0; i < n; i++)
    {
        if (monster[i].h>0)
        {
            ll xx = monster[i].x + d;
            ll num = (monster[i].h + a - 1) / a;
            ans += num;
            monster[i].h = 0;
            bool flag = false;
            for (int j = i + 1; monster[j].x >= (xx - d) && monster[j].x<= xx + d && j < n;j++)
            {
                monster[j].h-=a*num;

            }
        }
    }
    cout << ans << endl;

}

Later, learning the chiefs of the code, he is so operated, create an array c [i], recording each point several times already shelled need, then c [i] + = need, the next point is the number of times already shelling c [i + 1] + = c [i], because it is outside the range invalid, so each point i you have to find where he can hit the farthest, and then to lose in advance need, namely c [j] - = need; so, when i to j-1, executing c [i + 1] + = c [i], need just covers, I think this technique really wonderful

Code:

#include<iostream>
#include<string>
#include <cstdlib>
#include <algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<bitset>
#include <iomanip>// #pragma comment(linker, "/STACK:1024000000,1024000000")
// #define pi acos(-1)
// #include<bits/stdc++.h>std;namespaceusing


 
typedef long long ll;
#define INF 0x7f7f7f7f //2139062143
#define INF1 0x3f3f3f3f //1061109567
#define INF2 2147483647
#define llINF 9223372036854775807
#define pi 3.141592653589793//23846264338327950254
#define pb push_back
#define ll long long
#define debug cout << "debug\n";
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// ios::sync_with_stdio(false);cin.tie(NULL);
#define scai(x) scanf("%d", &x)
#define sca2i(x, y) scanf("%d %d", &x, &y)
#define scaf(x) scanf("%lf", &x)
#define sca2f(x, y) scanf("%lf %lf", &x, &y)
#define For(m,n) for (int i = m;  i < n; i++)

#define local
#ifdef local
#endif

#define MAX 10233
#define LCH(i) ((i) << 1)
#define RCH(i) ((i) << 1 | 1)
inline ll read(){
   ll s=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
   return s*w;
}
const int N = 2e5+5;
ll c[N];
struct mon
{
        ll x,h;
};
mon monster[N];
bool cmp(mon a, mon b)
{
    return a.x < b.x;
}
int main()
{
    //ios::sync_with_stdio(false);cin.tie(NULL);
   // cout << 50ll << endl;
    ll n, d, a;
    n = read();
    d = read();
    a = read();
    for (int i = 0; i < n; i++)
    {
        monster[i].x = read();
        monster[i].h= read();
    }
    sort(monster, monster + n,cmp);
    ll ans = 0;
    for (int i = 0,j = 0; i < n; i++)
    {
        while(j < n && monster[j].x <=monster[i].x + 2*d)
            ++j;
        ll need = max((monster[i].h - c[i]*a + a - 1)/a, 0ll);
        ans += need;
        c[i] += need;
        c[j] -= need;
        c[i+1] += c[i];
    }
    cout << ans << endl;

}

 

Guess you like

Origin www.cnblogs.com/hulian425/p/12236097.html