Last active
December 30, 2015 11:18
-
-
Save sayurin/7821234 to your computer and use it in GitHub Desktop.
ブログ記事 http://sayurin.blogspot.com/2013/12/c.html 新人女子プログラマの書いたコードを直すだけの簡単なお仕事です!|paizaオンラインハッカソンVol.1 https://paiza.jp/poh/ec-campaign
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.InteropServices; | |
static class CsPoh { | |
static int Calculate(byte[] buffer, byte[] prices) { | |
int ii = 0, oi = 0, temp; | |
var itemCount = 0; | |
while ((temp = buffer[ii++] - '0') >= 0) | |
itemCount = itemCount * 10 + temp; | |
var days = 0; | |
while ((temp = buffer[ii++] - '0') >= 0) | |
days = days * 10 + temp; | |
do { | |
var price = 0; | |
while ((temp = buffer[ii++] - '0') >= 0) | |
price = price * 10 + temp; | |
prices[price] = 1; | |
} while (--itemCount > 0); | |
var low0 = 10; | |
while (prices[low0] == 0) | |
low0++; | |
do { | |
var targetPrice = 0; | |
while ((temp = buffer[ii++] - '0') >= 0) | |
targetPrice = targetPrice * 10 + temp; | |
var low = low0; | |
var high = targetPrice - low0; | |
while (prices[high] == 0) | |
high--; | |
var price = 0; | |
do { | |
temp = low + high; | |
if (temp == targetPrice) { | |
price = targetPrice; | |
break; | |
} | |
if (temp < targetPrice) { | |
if (price < temp) | |
price = temp; | |
while (prices[++low] == 0) | |
; | |
} else | |
while (prices[--high] == 0) | |
; | |
} while (low < high); | |
if (100000 <= price) | |
goto l100000; | |
if (10000 <= price) | |
goto l10000; | |
if (1000 <= price) | |
goto l1000; | |
goto l100; | |
l100000: | |
buffer[oi++] = (byte)('0' + Math.DivRem(price, 100000, out price)); | |
l10000: | |
buffer[oi++] = (byte)('0' + Math.DivRem(price, 10000, out price)); | |
l1000: | |
buffer[oi++] = (byte)('0' + Math.DivRem(price, 1000, out price)); | |
l100: | |
buffer[oi++] = (byte)('0' + Math.DivRem(price, 100, out price)); | |
buffer[oi++] = (byte)('0' + Math.DivRem(price, 10, out price)); | |
buffer[oi++] = (byte)('0' + price); | |
buffer[oi++] = 10; | |
} while (--days > 0); | |
return oi; | |
} | |
[DllImport("libc")] | |
static extern int read(int fd, IntPtr buf, int count); | |
[DllImport("libc")] | |
static extern int write(int fd, IntPtr buf, int count); | |
static void Main() { | |
const int valueLength = 2 + 200000 + 75; | |
var buffer = new byte[valueLength * 7]; | |
var handle = GCHandle.Alloc(buffer, GCHandleType.Pinned); | |
var pbuf = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, 0); | |
read(0, pbuf, valueLength * 7); | |
var length = Calculate(buffer, new byte[1000001]); | |
write(1, pbuf, length); | |
handle.Free(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment