---
# System prepended metadata

title: IONC RE TEST
tags: [IONC]

---

> 目前只提供 *C 或 CPP* 的程式碼
> 想提供更快 *cp* 值更高的解法請再使用下方的 *Gmail* 聯繫我
> 主旨請打 : *IONC HackMD* 筆記程式碼並註明是哪一題
> 附上 *Code* 跟 *AC* 截圖就有機會登板喔 !

> * 題目總覽 : [***PDF***](https://drive.google.com/file/d/12Fq4iqB-OnV0GytPIOkdwIWURYHhplrt/view?usp=sharing)
> * 聯繫方式 : ***ivypeter1546@gmail.com***
# ***Q1*** *A1*

```clike=
#include <stdio.h>

int main()
{
    printf("He11o, IONC!\n");
    return 0;
}
```

# ***Q2*** *A2*
```clike=
// 確認的點=>如果一直勝利逆轉事件要繼續加嗎
#include <stdio.h>
#include <stdlib.h>
int arr[300];

int main()
{
    int n, even = 1, turn = 0, LCwin = 0, ACwin = 0, max = 0;
    scanf("%d", &n);
    int temp, checkfirst;
    int now;
    scanf("%d", &now);

    if (now == 1)
        temp = 1;
    else if (now == 2)
        temp = 2;

    arr[0]++;
    checkfirst = temp;
    for (int j = 0; j < n - 1; j++)
    {
        scanf("%d", &now);
        if (now == 1 && temp == 1)
        {
            arr[turn]++;
        }
        else if (now == 2 && temp == 1)
        {
            turn++;
            temp = 2;
            arr[turn]++;
        }
        else if (now == 1 && temp == 2)
        {
            turn++;
            temp = 1;
            arr[turn]++;
        }
        else if (now == 2 && temp == 2)
        {
            arr[turn]++;
        }
    }

    if (checkfirst == 1)
    {
        for (int j = 0; j <= turn; j++)
        {
            if (j % 2 == 0)
                ACwin += arr[j];
            else
                LCwin += arr[j];

            if (checkfirst == 1)
            {
                if (LCwin > ACwin)
                {
                    checkfirst = 2;
                    even++;
                    if (arr[j] > max)
                        max = arr[j];
                }
            }
            else if (checkfirst == 2)
            {
                if (ACwin > LCwin)
                {
                    checkfirst = 1;
                    even++;
                    if (arr[j] > max)
                        max = arr[j];
                }
            }

            if (ACwin == LCwin)
                even++;
        }
    }
    else if (checkfirst == 2)
    {
        for (int j = 0; j <= turn; j++)
        {
            if (j % 2 == 0)
                LCwin += arr[j];
            else
                ACwin += arr[j];

            if (checkfirst == 1)
            {
                if (LCwin > ACwin)
                {
                    checkfirst = 2;
                    even++;
                    if (arr[j] > max)
                        max = arr[j];
                }
            }
            else if (checkfirst == 2)
            {
                if (ACwin > LCwin)
                {
                    checkfirst = 1;
                    even++;
                    if (arr[j] > max)
                        max = arr[j];
                }
            }
            if (ACwin == LCwin)
                even++;
        }
    }

    printf("%d %d\n", ACwin, LCwin);
    printf("%d\n%d\n", even, max);

    return 0;
}
```

# ***Q3*** *A3*
```clike=
// a,b可能是0
#include <stdio.h>
#include <string.h>
char a[1000005];
char b[1000005];

int main()
{
    scanf("%s", a);
    scanf("%s", b);
    // i,j => a,b長度
    // al,bl => a,b起始值
    // ar,br => a,b結尾
    /*
    al =>i
    bl =>j
    i =>ar
    j =>br
    ar =>al
    br =>bl
    */
    int i = 0;
    int j = 0;
    int ar, br;
    int change = 1;
    for (ar = 0; a[ar] != '\0'; ar++)
    {
        if (change)
        {
            if (a[ar] != '0')
            {
                i += 1;
                change = 0;
            }
        }
        else
            i += 1;
    }
    change = 1;
    for (br = 0; b[br] != '\0'; br++)
    {
        if (change)
        {
            if (b[br] != '0')
            {
                j += 1;
                change = 0;
            }
        }
        else
            j += 1;
    }

    int al = ar - i;
    int bl = br - j;
    // printf("i=%d j=%d\n", i, j);
    // printf("al=%d ar=%d\n", al, ar);
    // printf("bl=%d br=%d\n", bl, br);
    if (i == 0 && j == 0)
    {
        printf("=\n");
    }
    else if (i > j)
    {
        printf(">\n");
        // printf("check 1\n");
    }
    else if (j > i)
    {
        printf("<\n");
        // printf("check 2\n");
    }
    else
    {
        int check = 1;
        int count = 0;
        while (check)
        {
            if (a[al] > b[bl])
            {
                printf(">\n");
                check = 0;
            }
            else if (a[al] < b[bl])
            {
                printf("<\n");
                check = 0;
            }
            else
            {
                count += 1;
                // printf("count=%d\n", count);
                if (count == i)
                {
                    printf("=\n");
                    check = 0;
                }
            }
            al++;
            bl++;
        }
        // printf("check 3\n");
    }
    return 0;
}

```

# ***Q4*** *A4*
```clike=
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int input[200005];
int cmp(const void *a, const void *b)
{
    return (*(int *)a - *(int *)b);
}

int main()
{
    int n, k;
    scanf("%d %d", &n, &k);
    // input[0] = 0;
    for (int i = 0; i < n; i++)
        scanf("%d", &input[i]);
    qsort(input, n, sizeof(int), cmp);

    /*for (int i = 0; i < n; i++)
        printf("%d ", input[i]);
    printf("\n");*/
    // 1~n

    // 比較優先順序

    // 最強
    if (k == n)
    {
        printf("%d\n", input[k - 1]);
    }
    // 最爛
    else if (k == 0)
    {
        if (input[k] == 1)
            printf("-1\n");
        else
            printf("%d", input[k] - 1);
    }
    // 中間中間
    else if ((input[k] - input[k - 1]) < 2)
    {
        if (input[k] - input[k - 1] == 1)
            printf("%d\n", input[k - 1]);
        else
            printf("-1\n");
    }
    else
        printf("%d\n", input[k] - 1);
    return 0;
}
```


# ***Q5*** *B1*
```clike=
#include <stdio.h>
#include <stdlib.h>
// p[n] n個點, n-1條邊
// c[n] n個點, n條邊
// k[n] n個點, Cn取2條邊
// K[n][m] n+m個點, n*m條邊
#define int long long

int main()
{
    int t;
    scanf("%lld", &t);
    while (t--)
    {
        char ch;
        int n, m;
        scanf(" %c", &ch);
        if (ch == 'P')
        {
            scanf("[%lld]", &n);
            printf("%lld %lld\n", n, n - 1);
        }
        else if (ch == 'C')
        {
            scanf("[%lld]", &n);
            printf("%lld %lld\n", n, n);
        }
        else if (ch == 'K')
        {
            scanf("[%lld]", &n);
            char tmp;
            scanf("%c", &tmp);
            if (tmp == '[')
            {
                scanf("%lld]", &m);
                printf("%lld %lld\n", n + m, n * m);
            }
            else
            {
                if (n < 2)
                    printf("%lld 0\n", n);
                else
                    printf("%lld %lld\n", n, ((n - 1) * n) / 2);
            }
        }
    }

    return 0;
}
```


# ***Q6*** *B2*
```clike=
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int input[1000001];
char ch[5];

int main()
{
    int n;
    scanf("%d", &n);
    int low = 1, up = n;
    int guess;
    int check = 1;
    int nc = 1;
    while (check)
    {
        // 如果前一次範圍完全沒變=>猜別的數字
        if (nc)
            guess = (low + up) / 2;
        else
        {
            nc = 1;
            if (guess + 1 <= up)
                guess += 1;
            else if (guess - 1 >= low)
                guess -= 1;
            else
            {
                printf("! %d\n", guess);
                break;
            }
        }
        printf("%d\n", guess);
        fflush(stdout);

        // 初始化ch
        for (int i = 0; i < 5; i++)
            ch[i] = '\0';
        scanf("%s", ch);

        // 判斷式
        if (ch[0] == '=')
        {
            printf("! %d\n", guess);
            check = 0;
        }
        else if (ch[0] == '<')
        {
            if (ch[1] == '=')
            {
                if (guess == up)
                    nc = 0;
                up = guess;
            }
            else
                up = guess - 1;
        }
        else if (ch[0] == '>')
        {
            if (ch[1] == '=')
            {
                if (guess == low)
                    nc = 0;
                low = guess;
            }
            else
                low = guess + 1;
        }

        if (up == low)
        {
            printf("! %d\n", low);
            check = 0;
        }
    }

    return 0;
}
```


# ***Q7*** *B3*
> 註 : 此題還卡在 *TLE* 地獄中，程式碼僅做參考
```clike=
#include <stdio.h>
#include <stdlib.h>
#define int unsigned
int ind = 1;
int for_count[100005][5];
int add[100005];
int check = 0;

int main()
{
    int x = 0;
    char point[5];
    // int number = 1;

    int l;
    scanf("%u", &l);

    for (int i = 1; i <= l; i++)
    {
        scanf(" %s", point);
        if (check != 1 && point[0] == 'a')
            add[i] = 1;

        else if (check != 1 && point[0] == 'f')
        {
            for_count[ind][0] = i;
            scanf("%u", &for_count[ind][1]);
            ind += 1;
        }

        else if (check != 1 && point[0] == 'e')
        {
            for (int j = (for_count[ind - 1][0] + 1); j < i; j++)
            {
                if (add[j] != 0)
                {
                    if (for_count[ind - 1][1] > (4294967295 / add[j]))
                    {
                        check = 1;
                        break;
                    }
                    else
                        add[j] *= for_count[ind - 1][1];
                }
            }
            ind -= 1;
        }
        /*
        printf("inde=%d\n", ind);

        for (int j = 1; j <= i; j++)
        {
            printf("add[%d]=%d ", j, add[j]);
        }
        printf("\n");

        for (int j = 1; j < ind; j++)
        {
            printf("for_count[%d]=(%d,%d) ", j, for_count[j][0], for_count[j][1]);
        }
        printf("\n");*/
    }

    if (check != 1)
    {
        for (int i = 1; i <= l; i++)
        {
            if (add[i] > (4294967295 - x))
            {
                check = 1;
                break;
            }
            else
                x += add[i];
        }
    }

    if (check)
    {
        printf("OVERFLOW!!!\n");
    }
    else
        printf("%u\n", x);
    return 0;
}
```
