[luogu 5016][2018noip 普及组] 龙虎斗 {暴力}

题目

https://www.luogu.org/problemnew/show/P5016


解题思路

根据题目的意思打一个暴力。 注意开 u n s i g n e d   l o n g   l o n g unsigned\ long\ long unsigned long long,不过听说 n o i p noip noip的数据比较水 ! ! !


代码

#include
#include
#include
#define rr register 
#define ll long long
using namespace std; 
ll n,a[100001],m,p1,s1,s2,p2,b[100001]; 
unsigned ll front,back; 
ll abss(ll x){return x>0?x:-x;}
inline ll read()
{
    ll p=0; char c=getchar(); 
    while (!isdigit(c)) c=getchar(); 
    while (isdigit(c)) p=(p<<3)+(p<<1)+c-48,c=getchar();
    return p; 
}
void write(ll x){if (x>9) write(x/10); putchar(x%10+48);}
int main()
{
    n=read(); 
    for (rr int i=1;i<=n;i++) a[i]=read(); 
    m=read(),p1=read(),s1=read(),s2=read(); 
    for (rr int i=1;i<=n;i++) b[i]=a[i]*abss(m-i); 
    for (rr int i=1;i<m;i++) front+=b[i]; 
    for (rr int i=m+1;i<=n;i++) back+=b[i]; 
    if (p1<m) front+=s1*abss(m-p1); else back+=s1*abss(m-p1); 
    unsigned long long now=abss(front-back); p2=m; 
    for (int i=1;i<=n;i++)
      if (i<m){
      	unsigned long long g=s2*(m-i); 
      	if (abss(front+g-back)<now) {
      		now=abss(front+g-back); p2=i; 
          }
      } else if (i>m) {
      	unsigned long long g=s2*(i-m); 
      	if (abss(front-g-back)<now) {
      		now=abss(front-g-back); p2=i; 
          }
      }
    write(p2); 
}

你可能感兴趣的:(暴力(/模拟/字符串处理))