AtCoder Beginner Contest 183(A-E)
A - ReLU
def main():
x = int(input())
print(x if x >= 0 else 0)
if __name__ = "__main__":
main()
B - Billiards
def main():
a,b,c,d = map(int,input().split())
print((b*c+a*d)/(b+d))
C - Travel
\(O(n!)\)暴力
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 2e5 + 10;
typedef long long ll;
int T[100][100];
int main()
{
int n , k;
int a[10];
scanf("%d%d",&n,&k);
for(register int i = 1;i <= n;i++)
for(register int j = 1;j <= n;j++)
scanf("%d",&T[i][j]);
for(register int i = 2,j = 1;i <= n;i++)
{
a[j] = i;
j++;
}
int ans = 0;
do{
int tot = 0;
tot += T[1][a[1]];
for(register int i = 2;i < n;i++)
tot += T[a[i]][a[i-1]];
tot += T[a[n-1]][1];
if(tot == k) ans++;
}while(next_permutation(a+1,a+n));
printf("%d\n",ans);
return 0;
}
D - Water Heater
前缀和
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int NMAX = 2e5 + 10;
ll liter[NMAX];
int main()
{
int n, max_time = 0;ll L;
scanf("%d%lld",&n,&L);
for(register int i = 1;i <= n;i++)
{
int l, r;
ll lit;
scanf("%d%d%lld",&l,&r,&lit);
liter[l + 1] += lit;
liter[r + 1] -= lit;
max_time = max(max_time , r + 1);
}
bool flag = true;
for(register int i = 1;flag &&i <= max_time;i++)
{
liter[i] += liter[i - 1];
if(liter[i] > L)
{
// printf("%d\n",liter[i]);
flag = false;
}
}
printf("%s\n",flag?"Yes":"No");
return 0;
}
E - Queen on Grid
动态规划,维护行、列、对角线的前缀和
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 2e3 + 10;
const int MOD = 1e9 +7;
typedef long long ll;
char str[NMAX][NMAX];
ll col_sum[NMAX][NMAX], row_sum[NMAX][NMAX],diag_sum[2*NMAX][NMAX]; // lie hang duijiaoxian
int col_max[NMAX], row_max[NMAX], diag_max[2*NMAX];
ll colSum(int index,int l,int r)
{
return (col_sum[index][r] - col_sum[index][l] + MOD)%MOD;
}
ll rowSum(int index,int l,int r)
{
return (row_sum[index][r] - row_sum[index][l] + MOD)%MOD;
}
ll diagSum(int index,int l,int r)
{
return (diag_sum[index][r] - diag_sum[index][l] + MOD)%MOD;
}
int main()
{
int h, w;
scanf("%d%d",&h,&w);
for(register int i = 1;i <= h;i++)
scanf("%s",str[i] + 1);
ll ans = 0;
for(register int i = 1;i <= h;i++)
for(register int j = 1;j <= w;j++)
{
if(i == j && i == 1)
{
row_sum[1][1] = col_sum[1][1] = diag_sum[w][1] = 1;
continue;
}
if(str[i][j] == '#')
{
ans = 0;col_max[j] = i;row_max[i] = j;diag_max[i - j + w] = min(i,j);
}
else
{
ans = (colSum(j,col_max[j],i - 1) + rowSum(i,row_max[i],j - 1) + diagSum(i - j + w,diag_max[(i - j + w)],min(i,j)-1))%MOD;
}
row_sum[i][j] = (row_sum[i][j-1] + ans)%MOD;
col_sum[j][i] = (col_sum[j][i-1] + ans)%MOD;
diag_sum[i - j + w][min(i,j)] = (diag_sum[i - j + w][min(i,j)-1] + ans)%MOD;
// printf("%lld%c",ans,j == w?'\n':' ');
}
printf("%lld\n",ans);
return 0;
}