https://codeforces.com/contest/1490/problem/B

 

Problem - B - Codeforces

 

codeforces.com

 

[난이도] Div.3
[유형] 구현

[풀이]
크기 3짜리 배열에 나머지가 0,1,2인 숫자들의 개수를 각각 저장 뒤.
index 0,1,2 각각에서 시작해서 N/3만큼만 남기고 순차적으로 우측( a[(i+1)%3] ) 으로
밀어보는 시도를 한다.
최대 2회까지 밀어보고 나머지를 민 타겟의 위치가 N/3이 되게 되면 그때가 최적의 정답이다.

 

#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
int tc,n,a[50];
int main(){
    scanf("%d",&tc);
    while(tc--){
        scanf("%d",&n);
        for(int i=0;i<n;i++) scanf("%d",&a[i]);
        int ans = 0;
        for(int i=1;i<n;i++){
            int p=a[i-1],q=a[i];
            if(p>q) swap(p,q);
            if(2*p>=q) continue;
            int cur=p;
 
            for(int cur=2*p;cur<q;cur*=2){
                ans++;
            }  
        }
        printf("%d\n",ans);
    }
}

 


https://github.com/has2/Problem-Solving/blob/master/codeforces/Round702-Div.3/B.cpp

 

 

+ Recent posts