Gym 101147补题
D - Popcorn(排列组合)
就是求\(C^x_y\)
代码1:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
#define ll long long
void init(){
cin.tie();
cout.tie();
std::ios::sync_with_stdio(false);
}
int main()
{
// freopen("popcorn.in","r",stdin);
init();
int n; scanf("%d",&n);
for(int i=0;i<n;i++){
ll ans[25];
ll x,y;
scanf("%lld %lld",&x,&y);
ans[0]=1;
for(int i=1;i<=x;i++){
ans[i]=ans[i-1]*(x-i+1)/i;
cout<<ans[i]<<endl;
}
printf("%lld\n",ans[y]);
// cout<<ans[y]<<endl;
}
return 0;
}
代码2:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
#define ll long long
void init(){
cin.tie();
cout.tie();
std::ios::sync_with_stdio(false);
}
int main()
{ init();
freopen("popcorn.in","r",stdin);
ll n;cin>>n;
for(ll i=0;i<n;i++){
ll x,y;cin>>x>>y;
ll ans=1;
for(ll i=x;i>=x-y+1;i--){
ans*=i;
}
for(ll i=y;i>=1;i--){
ans/=i;
}
cout<<ans<<endl;
}
}
H - Commandos Gym - 101147H
简单dp
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
#define ll long long
const int N = 11;
int a[N][N][N];
int dp[N][N];
int main()
{
// freopen("commandos.in","r",stdin);
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
memset(dp, 0, sizeof dp);
memset(a, 0, sizeof a);
for (int i = 0; i < n; i++)
{
int x, y, z, w;
cin >> x >> y >> z >> w;
a[x][y][z] += w;
}
for (int i = 10; i >= 1; i--)
{
for (int j = 1; j <= 10; j++)
{
for (int o = 1; o <= 10; o++)
{
dp[j][o]=max(dp[j][o]+ a[i][j][o],
max(dp[j - 1][o] + a[i][j][o],dp[j][o - 1] + a[i][j][o]));
// dp[j][o]=max(dp[j][o], );
// if(j==5&&i==10){cout<<a[i][j][o]<<" "<<dp[j][o]<<endl;}
}
}
}
cout << dp[10][10] << endl;
}
return 0;
}
E - Jumping Gym - 101147E 变形最短路
单边,从最后向前遍历,因为这样可以一遍得到从n->(1~n-1)的最短路
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5 + 10;
#define pii pair<int, int>
const int INF = 0x3f3f3f3f;
vector<pii> g[N];
int dis[N];
int vis[N];
int n;
void dj()
{
priority_queue<pii, vector<pii>, greater<pii>> q;
memset(dis, 0x3f, sizeof dis);
// memset(vis, 0, sizeof vis);
q.push({0, n});
dis[n] = 0;
while (q.size())
{
pii k = q.top();
q.pop();
// if (vis[k.second])
// continue;
// vis[k.second] = 1;
for (pii t : g[k.second])
{
int dist = k.first + t.first;
if (dis[t.second] > dist)
{
dis[t.second] = dist;
q.push({dist, t.second});
}
}
}
for (int i = 1; i <= n; i++)
{
if (dis[i] >= INF)
{
printf("-1\n");
}
else
printf("%d\n", dis[i]);
}
}
int main()
{
// freopen("jumping.in","r",stdin);
int t;
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
for (int i = 0; i < N; i++)
{
g[i].clear();
}
for (int i = 1; i <= n; i++)
{
int t;
scanf("%d", &t);
if (i - t >= 1)
{
// g[i].push_back({1, i - t});
g[i - t].push_back({1, i});
}
if (i + t <= n)
{
// g[i].push_back({1, i + t});
g[i + t].push_back({1, i});
}
}
dj();
}
return 0;
}