刷题记录(M. MaratonIME returns home,牛可乐和魔法封印,NC24866 [USACO 2009 Dec S]Music Notes)

M. MaratonIME returns home

题目链接

关键点:

1、易错:盗贼可能有多个,且都要看,因为最佳答案可能是在抢劫了之后才得出的

2、左右方向要搞清楚

完整代码:

# include <cstdio>
# include <iostream>
# include <cstring>
# include <algorithm>
using namespace std;
const int N = 1010;
int n, m, posx, posy, maxx, flag, total;
char ch[N][N];
int main()
{
	cin>>n>>m;
	for (int i=1; i<=n; i++)
	{
		for (int j=1; j<=m; j++)
		{
			cin>>ch[i][j];
		}
	}
	for (int i=1; i<=n; i++)
	{
		if (i%2)
		{
			for (int j=1; j<=m; j++)
			{
				if (ch[i][j]=='.')
				total++;
				if (ch[i][j]=='L')
				{
					flag = 1;
					maxx = max(maxx, total);
					total = 0;
				}
			}
		}
		else
		{
			for (int j=m; j>=1; j--)
			{
				if (ch[i][j]=='.')
				total++;
				if (ch[i][j]=='L')
				{
					flag = 1;
					maxx = max(maxx, total);
					total = 0;
				}
			}
		}
	}
	maxx = max(maxx, total);
	cout<<maxx<<endl;
	return 0;
}

牛可乐和魔法封印

题目链接

关键点:

1、求大于等于x且小于等于y的个数,直接用二分函数,upper_bound - lower_bound来求出个数

2、upper_bound求出大于一个数的第一个的位置,lower_bound求出大于等于一个数的第一个位置

完整代码

# include <cstdio>
# include <iostream>
# include <algorithm>
using namespace std;
const int N = 100000+10;
int n, m;
int a[N];
int main()
{
    cin>>n;
    for (int i=1; i<=n; i++)
        cin>>a[i];
    cin>>m;
    for (int i=1; i<=m; i++)
    {
        int x, y;
        cin>>x>>y;
        int ans = upper_bound(a+1, a+1+n, y)-lower_bound(a+1, a+1+n, x);
        cout<<ans<<endl;
    }
    
    
    return 0;
}

[USACO 2009 Dec S]Music Notes

题目链接

关键点

1、用一个前缀和sum数组来存敲的音符总数,那么第i个字符的时长为sum[i]-sum[i-1],

2、又因为每次敲的音符时长为左开的区间,所以对于每一次的查找,在sum数组里找到第一个大于该查找时间的下标即为所求音符

完整代码

# include <iostream>
# include <cstdio>
# include <algorithm>
using namespace std;
const int N = 50000+10;
int n, q;
int sum[N];
int main()
{
    cin>>n>>q;
    for (int i=1; i<=n; i++)
    {
        int x;
        cin>>x;
        sum[i] = sum[i-1]+x;
    }
    for (int i=1; i<=q; i++)
    {
        int x;
        cin>>x;
        int ans = upper_bound(sum+1, sum+1+n, x)-sum;
        cout<<ans<<endl;
    }
    
    return 0;
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值