#24227: 優化IO


fire5386 (becaidorz)

學校 : 國立清華大學
編號 : 115822
來源 : [140.114.218.50]
最後登入時間 :
2024-05-17 10:36:18
b938. kevin 愛殺殺 | From: [61.230.2.126] | 發表日期 : 2021-01-28 22:22

成功用優化壓到1秒內 AC (0.9s, 47.1MB)

可以使用getchar_unlocked()加速讀取和putchar_unlocked()加速輸出,其他的部分我用STL的set操作

優化程式碼:

void readint(int* loc) {

char c;

int ret = 0;

while(1) {

c = getchar_unlocked();

if(c >= '0' && c <= '9')

ret = (ret * 10) + (c - '0');

else {

*loc = ret;

return;

}

}

}

 

void printint(int n) {

char c;

int temp = n;

int arr[10];

int i;

for(i = 0, temp = n; temp > 0; i++, temp /= 10)

arr[i] = temp % 10;

i--;

while(i >= 0)

putchar_unlocked(arr[i] + '0'), i--;

putchar_unlocked('\n');

return;

}

 

void printstr(string s) {

for(int i = 0; i < s.size(); i++)

putchar_unlocked(s[i]);

putchar_unlocked('\n');

return;

}

 

完整程式碼:

https://66lemon66.blogspot.com/2021/01/zerojudge-b938-kevin-c.html

 
ZeroJudge Forum