1. 程式人生 > >[EOJ](3645)莫干山奇遇 ---- 數學+貪心★

[EOJ](3645)莫干山奇遇 ---- 數學+貪心★

題目傳送門

做法:

  • si = (x+i)%p 由這個式子,可以推出x = p-1,p = max(ai)+1;
  • 構造的話,暴力模擬一下,發現一定從0開始。我們需要做的就是貪心的往相鄰兩個數之間填充數即可。

AC程式碼:

#include<bits/stdc++.h>
#define IO          ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define pb(x)       push_back(x)
#define sz(x)       (int)(x).size()
#define sc(x)       scanf("%d",&x)
#define pr(x)       printf("%d\n",x)
#define abs(x)      ((x)<0 ? -(x) : x)
#define all(x)      x.begin(),x.end()
#define mk(x,y)     make_pair(x,y)
#define debug       printf("!!!!!!\n")
#define fin         freopen("in.txt","r",stdin)
#define fout        freopen("out.txt","w",stdout)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int mod = 1e9+7;
const double PI = 4*atan(1.0);
const int maxm = 1e8+5;
const int maxn = 2e5+5;
const int INF = 0x3f3f3f3f;
ll a[maxn];
int main()
{
    #ifdef LOCAL_FILE
    fin;
    #endif // LOCAL_FILE
    //IO;
    int n;
    ll mx = -1;
    sc(n);
    for(int i=1;i<=n;i++)
    {
        sc(a[i]);
        mx = max(mx,a[i]);
    }
    ll p = mx+1;
    ll ans = n;
    for(int i=2;i<=n;i++)
    {
        ll tmp = a[i]-a[i-1]-1;
        if(tmp<0) ans+=tmp+p;
        else ans+=tmp;
    }
    printf("%lld\n",ans);
    return 0;
}