E Breaking the Wall
题意:n个数字代表n面墙的耐久度,每次可以挑选一个墙进行攻击,结果是这面墙减少两个耐久度,这面墙两边的墙减少一个耐久度。求把两面墙破坏掉要花费的最少的次数。
分析:这道题分类讨论,主要有三种情况,第一种【5,1000,1000,5】这种要破坏的墙的距离相差>1,这就需要两个分别破坏。第二种情况,【6,1000,4】,相差一个,这种情况的处理是攻击中间的墙,再处理耐久度大的墙。第三种情况,【1000,3,3,1000】,两墙相邻,对一个墙攻击的时候对第二个墙也会有伤害,如果一个墙的耐久是另一个墙的耐久两倍还多,那么攻击这个墙的时候,另一墙会被破坏掉,如果相差不大于两倍,那么总次数就是(a[i]+a[i+1]+2)/3,没解出这个题很大程度是因为忘了这个关系了。
题解:
就拿SSRS_大佬的代码吧。
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++){
cin >> a[i];
}
vector<int> a2 = a;
sort(a2.begin(), a2.end());
int ans = (a2[0] + 1) / 2 + (a2[1] + 1) / 2;
for (int i = 0; i < n - 2; i++){
if (a[i] <= a[i + 2]){
ans = min(ans, a[i] + (a[i + 2] - a[i] + 1) / 2);
} else {
ans = min(ans, a[i + 2] + (a[i] - a[i + 2] + 1) / 2);
}
}
for (int i = 0; i < n - 1; i++){
ans = min(ans, max({(a[i] + 1) / 2, (a[i + 1] + 1) / 2, (a[i] + a[i + 1] + 2) / 3}));
}
cout << ans << endl;
}
F Desktop Rearrangement
题意:桌面上图标使用’*‘表示,空白使用’.'表示,其中,good桌面指图标从左上角往下排列的整洁桌面。给你n行m列的桌面,q次变化查询,每次变化能把x,y位置的空白变成图标,图标变成空白,并且输出这个情形下,变成good桌面最少移动多少次。
分析:使用两个指针x,y记录应该放图标的地方已经有多少放了图标,如果变化在应该放图标的地方内外,分别对应两种不一样的操作,进行cnt的相应的加减。
题解:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
char s[1010][1010];
int main()
{
int n, m, q;
cin >> n >> m >> q;
for(int i=0; i<n; i++)
cin >> s[i];
int cnt = 0;
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
if(s[i][j] == '*')
cnt++;
int temp = cnt;
int x = 0, y = 0;
for(int i=0; i<temp; i++)
{
if(s[x][y] == '*')
cnt--;
x++;
if(x == n)
{
x = 0;
y++;
}
}
while(q--)
{
int a, b;
cin >> a >> b;
a--, b--;
if(s[a][b] == '*')
{
x--;
if(x == -1)
{
x = n - 1;
y--;
}
if(s[x][y] == '*')
cnt++;
s[a][b] = '.';
if(b > y || (b == y && a >= x))
cnt--;
}
else
{
if(s[x][y] == '*')
cnt--;
s[a][b] = '*';
x++;
if(x == n)
{
x = 0;
y++;
}
if(b > y || (b == y && a >= x))
cnt++;
}
printf("%d\n", cnt);
}
return 0;
}
G Remove Directed Edges
图 or dp,(开溜)~