結果

問題 No.4 おもりと天秤
ユーザー mafuyu-akimafuyu-aki
提出日時 2017-04-14 23:13:19
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,046 bytes
コンパイル時間 236 ms
コンパイル使用メモリ 27,972 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-08 17:23:59
合計ジャッジ時間 1,175 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define READ_BUFSIZE ( 3*100+100+1 )
#define READ_DELIMITER ( " " )


//標準入力取得(1行)

int GetStdin( char* pszStr, int lMaxLen )
{
	int lLen = 0;

    memset( pszStr, 0, lMaxLen );
    if( fgets( pszStr, lMaxLen, stdin ) )
    {
	    lLen = strlen( pszStr );
	    if( lLen >= 1 )
	    {
			if( pszStr[ lLen - 1 ] == 0x0A )
			{
				pszStr[ lLen - 1 ] = 0;
				lLen--;
			}
		}
	}
	
	return( lLen );
}

int split(char *pszStr, int lStrMax, const char *pszDelim, int *palOutList, int &lMax) 
{
    char *pszToken;
    int count = 0;
    char *pszNext_token = 0;
	int lLen = 0;
	int lNum = 0;
	int lSum = 0;
	lMax = 0;

    memset( pszStr, 0, lStrMax );
    lLen = GetStdin( pszStr, lStrMax );
    
	if( lLen > 0 )
	{
		pszToken = strtok( pszStr, pszDelim );

	    while ( pszToken != NULL ) 
	    {
            *palOutList = atoi( pszToken );
			lSum += *palOutList;

			if( *palOutList > lMax )
				lMax = *palOutList;
			lNum++;

			//	        pszToken = strtok_s(NULL, pszDelim, &pszNext_token);
			pszToken = strtok( NULL, pszDelim );
			palOutList++;
	    }
	}
    return lSum;
}




int main(int argc, char *argv[]) 
{
	char szRead[READ_BUFSIZE] = "";
	//char* psOutbuf[10000] = { 0 };
	int lInput[4] = {0};

	GetStdin( szRead, READ_BUFSIZE );
	int N = atoi( szRead );

	int W[100+1] = {0};
	bool possible = false;

	int max = 0;
	int sum = split( szRead, READ_BUFSIZE, READ_DELIMITER, W, max );
	int T = sum / 2;

	if( sum % 2 || ( max > T  ) )
	{
		printf( "impossible\n" );
		return 0;
	}

	int* pP = new int[ T + 1];
	memset( pP, 0, sizeof( int )*(T+1) );
	pP[0] = 1;

	for( int i = 0; i < N && !possible; i++ )
	{
		for( int j = T; j >=0 && !possible ; j-- )
		{
			if( pP[ j ] != 0 )
			{
				if( W[i] + j <= T && pP[W[i] + j] == 0 ) 
				{
					pP[ W[i] + j ] = 1;
//					printf( "%d", W[i] + j  );
				}

				if( W[i] + j == T )
					possible = true;
			}
		}
	}

	if( possible )
		printf( "possible\n" );
	else
		printf( "impossible\n" );


	return 0;

}

0