# 17088. ๋“ฑ์ฐจ์ˆ˜์—ด ๋ฐ˜ํ™˜
Study Repository

17088. ๋“ฑ์ฐจ์ˆ˜์—ด ๋ฐ˜ํ™˜

by rlaehddnd0422

https://www.acmicpc.net/problem/17088

 

17088๋ฒˆ: ๋“ฑ์ฐจ์ˆ˜์—ด ๋ณ€ํ™˜

ํฌ๊ธฐ๊ฐ€ N์ธ ์ˆ˜์—ด A = [A1, A2, ..., AN]์ด ์žˆ์„ ๋•Œ, ๋ชจ๋“  1 ≤ i < N์— ๋Œ€ํ•ด์„œ, Ai+1-Ai๊ฐ€ ๋ชจ๋‘ ์ผ์น˜ํ•˜๋ฉด ๋“ฑ์ฐจ์ˆ˜์—ด์ด๋ผ๊ณ  ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, [3], [6, 6, 6], [2, 8, 14, 20], [6, 4, 2]๋Š” ๋“ฑ์ฐจ์ˆ˜์—ด์ด๊ณ , [4, 5, 4], [6, 3, 1]

www.acmicpc.net



 

๋“ฑ์ฐจ์ˆ˜์—ด์˜ ์„ฑ์งˆ์„ ์ด์šฉํ•œ ๋ธŒ๋ฃจํŠธํฌ์Šค

  • ๋“ฑ์ฐจ์ˆ˜์—ด์˜ n๋ฒˆ์งธ ํ•ญ์€ A(n) = A(n-1) + D
  • ๋“ฑ์ฐจ์ˆ˜์—ด์˜ ๊ณต์ฐจ D๋Š” ๋‘๋ฒˆ์งธํ•ญ์—์„œ ์ฒซ์งธํ•ญ์„ ๋นผ์„œ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๊ฐ€๋Šฅํ•œ ์ฒซ์งธํ•ญ์€ A[0]-1, A[0], A[0]+1 ์„ธ ๊ฐ€์ง€๊ฐ€ ์žˆ๊ณ , ๊ฐ€๋Šฅํ•œ ๋‘˜์งธํ•ญ์€ A[1]-1,A[1],A[1]+1 ์„ธ ๊ฐ€์ง€, ์ด 3*3 = 9๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.
  • ์ด๋ ‡๊ฒŒ 9๊ฐ€์ง€์˜ ๊ณต์ฐจ๊ฐ’์„ ๊ณ„์‚ฐํ•ด ์ค€ ๋‹ค์Œ, ์„ธ๋ฒˆ์งธ ํ•ญ๋ถ€ํ„ฐ ์•ž์„œ ๊ณ„์‚ฐํ•œ ๊ณต์ฐจ๊ฐ’์„ ํ†ตํ•ด ํ•ด๋‹น ํ•ญ์˜ ๊ฐ’๊ณผ ์ฐจ์ด๊ฐ€ +1, -1์ด ์žˆ์œผ๋ฉด ์—ฐ์‚ฐ ํšŸ์ˆ˜๋ฅผ ๋”ํ•ด์ค€๋‹ค.

 

 

์†Œ์Šค์ฝ”๋“œ 

#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#define FASTio ios_base :: sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define endl '\n' 
using namespace std;

int n;
int ans = -1;

/*
์ฒซ๋ฒˆ์งธํ•ญ, ๋‘๋ฒˆ์งธํ•ญ ๊ฒฐ์ •
-> ๊ณต์ฐจ๊ณ„์‚ฐ
-> 3๋ฒˆ์งธํ•ญ๋ถ€ํ„ฐ ํ•ด๋‹น ํ•ญ๊ฐ’์ด ๊ณต์ฐจ+0 ์™€ ๊ฐ™์œผ๋ฉด continue;
->                      ๊ณต์ฐจ-1 or ๊ณต์ฐจ+1๊ณผ ๊ฐ™์œผ๋ฉด ans++; an += diff;
-> ์ตœ์†Œ cnt = ans


vector<int> a(100000);
int main()
{
    FASTio;
    cin >> n;

    for(int i=0;i<n;i++)
    {
        cin >> a[i];
    }
    
    if(n==1)
    {
        cout << 0 << endl;
        return 0;
    }
    
    for(int d1=-1;d1<=1;d1++)
    {
        for(int d2=-1;d2<=1;d2++)
        {   
            int cnt = 0;
            if(d1!=0) cnt++;
            if(d2!=0) cnt++;

            int a0 = a[0] + d1;
            int a1 = a[1] + d2;
            int diff = a1-a0;
            
            int an = a1 + diff;

            bool isit = true;

            // 3๋ฒˆ์งธ ํ•ญ๋ถ€ํ„ฐ ๊ฒ€์‚ฌ
            for(int i=2;i<n;i++)
            {   
                if(a[i]==an)
                {
                    an +=diff;
                    continue;
                }

                if(a[i]-1==an) cnt++;
                else if(a[i]+1==an) cnt++;
                else
                {
                    isit = false;
                    break;
                }

                an += diff;
            }

            if(isit==true)
            {   
                if(ans==-1 || ans>cnt)
                    ans = cnt;
            }
        }
    }
    
    cout << ans << endl;



    return 0;
}

 

 

'๐Ÿ“™ Problem Solving' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

5014. ์Šคํƒ€ํŠธ๋งํฌ  (0) 2023.01.28
3187. ์–‘์น˜๊ธฐ ๊ฟ  (0) 2023.01.27
17089. ์„ธ ์นœ๊ตฌ  (0) 2023.01.24
15686. ์น˜ํ‚จ ๋ฐฐ๋‹ฌ  (0) 2023.01.23
16968 / ์ฐจ๋Ÿ‰ ๋ฒˆํ˜ธํŒ 1  (0) 2023.01.20

๋ธ”๋กœ๊ทธ์˜ ์ •๋ณด

Study Repository

rlaehddnd0422

ํ™œ๋™ํ•˜๊ธฐ