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;
}