总结

· · 个人记录

P6408 [COCI 2008/2009 #3] PET

题意:

有五个选手,各有四个分数,求总分中的最高分及其编号

思路:

输入五行四个数,表示每个选手,每个裁判给的分数,在输入过程中记录总分,用maxn记录最大总分,用id记录最大总分的选手的编号,循环完毕后输出

错因:

无

代码:
#include <bits/stdc++.h>
using namespace std ;
struct node
{
    int f , id ;
} ;
node a[10] ;
bool cmp(node x , node y)
{
    return x.f > y.f ;
}
int s ;
int main()
{
    for(int i = 1 ; i <= 5 ; i++)
    {
        a[i].id = i ;
        for(int j = 1 ; j <= 4 ; j++)
        {
            cin >> s ;
            a[i].f += s ;
        }
    }
    sort(a + 1 , a + 5 + 1 , cmp) ;
    cout << a[1].id << " " << a[1].f ;
return 0 ;
} 

P1428 小鱼比可爱

题意:

小鱼只能看见自己的左手边,求他视线范围内有多少个人的可爱值不如自己

思路:

先用数组输入每一只小鱼的可爱值,再遍历数组,用双重循环遍历每一只小鱼的左手边,也就是1~i-1,用cnt记录 比第i只小的个数,也就是a[i] > a[j]的个数,每次循环后输出cnt,并清空cnt变量

错因:

在每一次输出时输出的是原始数据(a[i]),导致第一次测样例时出错

代码:
#include <bits/stdc++.h>
using namespace std ;
int n , a[105] ;
int ans[105] ;
int main()
{
    cin >> n ;
    for(int i = 1 ; i <= n ; i++)
    {
        cin >> a[i] ;
        for(int j = i ; j >= 1 ; j--)
        {
            if(a[j] < a[i]) ans[i]++ ; 
        }
    }
    for(int i = 1 ; i <= n ; i++)
    {
        cout << ans[i] << " " ;
    }
return 0 ;
}

P2038 [NOIP 2014 提高组] 无线网络发射器选址

题意:

求边长为2d的正方形最多能覆盖到多少个公共场所

思路:

输入公共场所的位置和个数,用一个数组记录下来,也就是a[x][y] += z 。然后用四重循环遍历矩阵的左上角和右下角,遍历矩阵,用变量记录覆盖到的个数后用maxn记录最大值

错因:

下标应该从零枚举,不然会漏掉一些答案,然后一开始cnt和maxn的顺序输出反了,导致爆零

代码:
#include <bits/stdc++.h>
using namespace std ;
int d , n , x , y , k ;
int f[133][133] ;
int maxn , cnt , sum ;
int main()
{
    cin >> d >> n ;
    while(n--)
    {
        cin >> x >> y >> k ;
        f[x][y] += k ;
    }
    for(int i = 0 ; i <= 128 ; i++)
    {
        for(int j = 0 ; j <= 128 ; j++)
        {
            sum = 0 ;
            for(int ax = max(0 , i - d) ; ax <= min(128 , i + d) ; ax++)
            {
                for(int ay = max(0 , j - d) ; ay <= min(128 , j + d) ; ay++)
                {
                    sum += f[ax][ay] ;
                }
            }
            if(sum >= maxn)
            {
                if(sum == maxn) cnt++ ;
                else cnt = 1 ;
                maxn = sum ;
            }
        }
    }
    cout << cnt << " " << maxn ;
return 0 ;
}

[蓝桥杯青少年组国赛 2024] 第二题

题意:

有两排石头,每次可以从上下两排交换两个石头,求能否是每一列的石头都相同

思路:

找出规律:

用这个规律算,就可以避免超时的问题

错因:

将%写成&

代码:
#include <bits/stdc++.h>
using namespace std ;
int t , n , cnt , cnt1 ;
int a[10005] , b[10005] ;
int main()
{
    cin >> t ;
    while(t--)
    {
        cnt = 0, cnt1 = 0;
        cin >> n ;
        for(int i = 1 ; i <= n ; i++)
        {
            cin >> a[i] ;
        }
        for(int i = 1 ; i <= n ; i++)
        {
            cin >> b[i] ;
            if(a[i] == 1 && b[i] == 0) cnt++ ;
            if(a[i] == 0 && b[i] == 1) cnt1++ ;
        }

        if((cnt1 % 2 == 0 && cnt % 2 != 0) || (cnt1 % 2 != 0 && cnt % 2 == 0)) cout << -1 << endl ;
        else if(cnt1 % 2 == 1 && cnt % 2 == 1) cout << cnt1 / 2 + cnt / 2 + 2 << endl ;
        else cout << cnt1 / 2 + cnt / 2 << endl ;
    }

return 0 ;
}

P1281 [CERC1998] 书的复制

题意:

每人只能复制连续的书,求把所有书复制完的最少时间

思路:

二分答案求结果

check函数用来判断中间方案是否可行

二分完后,还需要对r进行一次check,否则答案可能不行,会出现错误

错因:

无,讲解完直接更正了

代码:
#include <bits/stdc++.h>
using namespace std ;
int m , k , minn = 1000000000 ;
int a[505] , nex[505] , pre[505] ;
bool check(int mid)
{
    int pos = k , sum = a[m] ;
    nex[pos] = m , pre[pos] = m ;
    for(int i = m - 1 ; i >= 1 ; i--)
    {
        if(sum + a[i] <= mid)
        {
            pre[pos] = i ;
            sum += a[i] ;
        }
        else
        {
            pos-- ;
            if(pos == 0) return false ;
            nex[pos] = i , pre[pos] = i ;
            sum = a[i] ;
        }
    }
    return true ;
}
int main()
{
    cin >> m >> k ;
    int l = 0 , r = 0 ;
    for(int i = 1 ; i <= m ; i++)
    {
        cin >> a[i] ;
        r += a[i] ;
        l = max(l , a[i]) ;
    }
    l-- , r++ ;
    while(l + 1 < r)
    {
        int mid = (l + r) / 2 ;
        if(check(mid)) r = mid ;
        else l = mid ;
    }
    check(r) ;
    for(int i = 1 ; i <= k ; i++)
    {
        cout << pre[i] << " " << nex[i] << endl ;
    }
return 0 ;
}

[ABC429D] On AtCoder Conference(Easy Version)

题意:

求一个人走一路,不少于c会遇到多少人

思路:

用二分答案求结果。

check函数可以不写,因为只要判断还能不能遇见就可以了,可以直接用q[pos] - q[i] >= c来判断

结果存在r变量里面,因为l和r是相邻的两个数,所以用l来当结果下标会少1

错因:

将结果下标用l来当,所以不行,下表会少一

代码:
#include <bits/stdc++.h>
using namespace std ;
long long n , m , c , ans ;
int a[1000005] , s , q[1000005] ;
int main()
{
    cin >> n >> m >> c ;
    for(int i = 1 ; i <= n ; i++)
    {
        cin >> s ;
        s ++;
        a[s]++ ;
        a[s + m] ++;
    }
    for(int i = 1 ; i <= 2 * m ; i++)
    {
        q[i] = q[i - 1] + a[i] ;
    }
    for(int i = 1; i <= m ; i++)
    {
        int l = i , r = m * 2+1 ;
        while(l + 1 < r)
        {
            int pos = (l + r) / 2 ;
            if(q[pos] - q[i] >= c) r = pos ;
            else l = pos ;
        }
        ans += q[r] - q[i] ;
    }
    cout << ans ;
return 0 ;
}