跳到正文

个人记录

总结

Ch_Elowen约 9 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 9 分钟

版本

  1. v2当前
  2. v1

P6408 [COCI 2008/2009 #3] PET

题意:

求各分数中的最高总分并输出编号

思路:

在输入中存一位选手的总分,同时用结构体将编号也记下来,排序后输出第一名和他的编号

错因:

无

代码:

C++
#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遍历求个数,再挨个输出

错因:

在输出时输出的是原始数据,导致第一次测样例时出错

代码:

C++
#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的正方形最多能覆盖到多少个公共场所

思路:

输入数组,枚举正方形的左端点和右端点,再求区间内的最大和

错因:

下标应该从零枚举,不然会漏掉一些答案

代码:

C++
#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] 第二题

题意:

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

思路:

找出规律:

  • 每交换一次就可以让cnt,cnt1各减一

  • 如果有一个cnt是奇数,则无解

  • 如果两个都是奇数,结果就要加二

错因:

将%写成&

代码:

C++
#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] 书的复制

题意:

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

思路:

二分答案求结果

错因:

无,讲解完直接更正了

代码:

C++
#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会遇到多少人

错因:

无

代码:

C++
#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 ;
}
原文