A. Saturday
题意:告诉你今天星期几,问距离周六还有几天。
思路:直接判断还有多少天即可,一共就五天
AC代码:
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const int N = 100010;
string s;
int main()
{
cin >> s;
if(s == "Monday") cout << 5;
if(s == "Tuesday") cout << 4;
if(s == "Wednesday") cout << 3;
if(s == "Thursday") cout << 2;
if(s == "Friday") cout << 1;
return 0;
}
B. Split?
题意:给定一个字符串分别代表每个小球的状态,如果是 0 表示小球被击倒,如果是 1 表示小球站立。球的摆放如已知图,如果小球 1 被击倒并且一个区间中存在一列小球全为黑色输出 YES ,反之输出 NO 。区间的左右端点必须至少存在一个白球。
思路:
先将字符转化为数字存放在 a[] 中,再进行判断:要满足题意,那么 1 号必须为 0 .
接下来逐个分析,要求至少有一列都为 0 ,且除了该列之外每一列都至少有一个 1 .
AC代码:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
string s;
int a[20];
int main()
{
cin >> s;
s = ' ' + s;
for (int i = 1; i <= 10; i ++)
a[i] = s[i] - '0';
if (s[1] == '0')
{
int sum = 0;
for (int i = 1; i <= 10; i++)
{
sum += a[i];
}
if (sum == 0)
{
cout << "No" << endl;
return 0;
}
if (a[3] + a[9] == 0)
{
cout << "Yes" << endl;
return 0;
}
if (a[2] + a[8] == 0)
{
cout << "Yes" << endl;
return 0;
}
if (a[5] == 0)
{
cout << "Yes" << endl;
return 0;
}
if (a[7] == 1 && a[4] == 0)
{
cout << "Yes" << endl;
return 0;
}
if (a[10] == 1 && a[6] == 0)
{
cout << "Yes" << endl;
return 0;
}
cout << "No" << endl;
}
else cout << "No";
return 0;
}
C. Index × A (Continuous ver.)
题意:
思路:很容易想到用双指针来做,维护一个长度为 m 的滑动窗口,每次移动,都是将后一个数加入并将第一个数移出,还要重新计算当前窗口内的数值,如果每次都这样操作一遍的话,很容易时间复杂度过高导致超时。但是我们发现每次移动前面都减少了一个从 l 到 r 的区间和,由此想到可以用一个前缀和数组来维护区间和。
AC代码:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 200010;
LL a[N];
LL s[N];
int n, m;
LL ans, res;
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i ++)
{
cin >> a[i];
s[i] = s[i - 1] + a[i]; //前缀和
}
for (int i = 1; i <= m; i ++) //初始窗口,前m个数
res += a[i] * i;
//双指针
ans = res;
for (int i = m + 1; i <= n; i ++)
{
int l = i - m + 1, r = i;
res += m * a[r] - (s[r - 1] - s[l - 2]);
ans = max(ans, res);
}
cout << ans << endl;
return 0;
}