結果

問題 No.297 カードの数式
コンテスト
ユーザー naimonon77
提出日時 2016-01-08 17:36:01
言語 C++11
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 1,392 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,031 ms
コンパイル使用メモリ 184,372 KB
実行使用メモリ 7,972 KB
最終ジャッジ日時 2026-04-08 00:06:40
合計ジャッジ時間 2,361 ms
ジャッジサーバーID
(参考情報)
judge2_1 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2 WA * 1
other AC * 12 WA * 11
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
/* ここからが本編 */
/*  */
char num[18] = {0};
int N;
int num_top = 0;
ll s_max(int plus_cnt,int minus_cnt)
{
  char s_tmp[18];
  int tmp = N-2*plus_cnt-2*minus_cnt;
  strncpy(s_tmp,num,tmp);
  s_tmp[tmp] = 0;
  ll max = atoi(s_tmp);
  while(plus_cnt) {
	max += num[tmp++] - '0';
	plus_cnt--;
  }
  while(minus_cnt) {
	max -= num[tmp++] - '0';
	minus_cnt--;
  }
  return max;
}
ll s_min(int plus_cnt,int minus_cnt)
{
  char s_tmp[18];
  ll min;
  if(minus_cnt) {
	min = num[num_top-1] - '0';
	int tmp = N-2*plus_cnt-2*minus_cnt;
	minus_cnt--;
	strncpy(s_tmp,num,tmp);
	s_tmp[tmp] = 0;
	min -= atoi(s_tmp);
	
	while(minus_cnt) {
	  min -= num[tmp++] - '0';
	  minus_cnt--;
	}
	while(plus_cnt) {
	  min += num[tmp++] - '0';
	  plus_cnt--;
	}
  }
  else {
	return -1;
  }
  return min;
}
int main(void)
{
  int i,j,k;
  int plus_cnt = 0;
  int minus_cnt = 0;
  char c;
  
  cin >> N;
  for(i=0;i<N;i++) {
	scanf(" %c",&c);
	if(c == '+') plus_cnt++;
	else if(c == '-') minus_cnt++;
	else {
	  num[num_top] = c;
	  num_top++;
	}
  }
  sort(num,num+num_top,greater<int>());
  ll max = s_max(plus_cnt,minus_cnt);
  ll min = s_min(plus_cnt,minus_cnt);
  printf("%lld %lld\n",max,min);
  
  return 0;
}
0