結果

問題 No.873 バイナリ、ヤバいなり!w
ユーザー tnakao0123
提出日時 2019-09-05 09:41:25
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 174 ms / 2,000 ms
コード長 1,490 bytes
コンパイル時間 1,306 ms
コンパイル使用メモリ 84,032 KB
実行使用メモリ 7,168 KB
最終ジャッジ日時 2024-06-11 14:04:21
合計ジャッジ時間 3,320 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:52:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   52 |   scanf("%d", &n);
      |   ~~~~~^~~~~~~~~~

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 873.cc:  No.873 バイナリ、ヤバいなり!w - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 300000;
const int MAX_M = 600;
const int INF = 1 << 30;

/* typedef */

/* global variables */

int dp[MAX_N + 1];
int pmins[MAX_N + 1], pmaxs[MAX_N + 1];

/* subroutines */

bool ltprty(const int &a, const int &b) {
  int pa = (a & 1), pb = (b & 1);
  if (pa != pb) return (pa > pb); // odd < even
  return (pa ? (a < b) : (a > b));
}

/* main */

int main() {
  int n;
  scanf("%d", &n);

  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    dp[i] = INF;
    pmins[i] = 0, pmaxs[i] = -1;
    for (int j = 1; j * j <= i; j++) {
      int jj = j * j, d = dp[i - jj] + j;
      if (dp[i] > d)
	dp[i] = d, pmins[i] = pmaxs[i] = j;
      else if (dp[i] == d) {
	if (ltprty(j, pmins[i])) pmins[i] = j;
	if (ltprty(pmaxs[i], j)) pmaxs[i] = j;
      }
    }
  }
  //printf("%d\n", dp[n]);

  for (int m = n, pc = 0; m > 0;) {
    int k = (pc == 0) ? pmins[m] : pmaxs[m];

    //printf("%d ", k);
    for (int i = 0; i < k; i++) putchar(((pc + i) & 1) + '0');

    pc ^= ((k & 1) ^ 1);
    m -= k * k;
  }
  putchar('\n');
  return 0;
}
0