【AtCoder】diverta 2019 Programming Contest 2
diverta 2019 Programming Contest 2
A - Ball Distribution
特判一下一个人的,否则是\(N - (K - 1) - 1\)
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define ba 47
#define MAXN 100005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int N,K;
void Solve() {
read(N);read(K);
if(K == 1) puts("0");
else {
out((N - (K - 1)) - 1);enter;
}
}
int main(){
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}
B - Picking Up
枚举p,q(就是枚举一个点对计算p和q),判哪一种情况最优即可
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define ba 47
#define MAXN 100005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int N;
map<pii,int> zz;
pii poi[55];
void Solve() {
read(N);
int x,y;
for(int i = 1 ; i <= N ; ++i) {
read(poi[i].fi);read(poi[i].se);
zz[poi[i]] = 1;
}
int ans = N;
for(int i = 1 ; i <= N ; ++i) {
for(int j = 1 ; j <= N ; ++j) {
if(i == j) continue;
int p = poi[i].fi - poi[j].fi,q = poi[i].se - poi[j].se;
int tmp = N;
for(int h = 1 ; h <= N ; ++h) {
tmp -= zz[mp(poi[h].fi - p,poi[h].se - q)];
}
ans = min(ans,tmp);
}
}
out(ans);enter;
}
int main(){
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}
C - Successive Subtraction
又有负数又有正数
先挑出一个正数一个负数,用这个负数减遍所有正数(除了挑出来的),再用这个正数减遍所有负数(包括挑出来的)
就可以获得所有数的绝对值和
只有正数
会牺牲一个最小的正数,方法是挑出最小的正数,再挑一个正数,用最小的正数减遍所有的正数(除了挑出来的),再用挑的正数减掉最小的正数当前的值
只有负数
会牺牲一个最小的负数,用这个负数减遍其余负数即可
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define ba 47
#define MAXN 100005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int N,a[100005],cnt[2];
vector<pii > ans;
int64 res = 0;
void Solve() {
read(N);
for(int i = 1 ; i <= N ; ++i) {
read(a[i]);
if(a[i] < 0) cnt[0]++;
else cnt[1]++;
}
if(cnt[0] && cnt[1]) {
int s,t;
for(int i = 1 ; i <= N ; ++i) res += abs(a[i]);
for(int i = 1 ; i <= N ; ++i) {
if(a[i] >= 0) s = i;
if(a[i] < 0) t = i;
}
for(int i = 1 ; i <= N ; ++i) {
if(a[i] >= 0 && i != s) {
ans.pb(mp(a[t],a[i]));
a[t] -= a[i];
}
}
for(int i = 1 ; i <= N ; ++i) {
if(a[i] < 0) {
ans.pb(mp(a[s],a[i]));
a[s] -= a[i];
}
}
}
else if(cnt[0]) {
int p = 1;
for(int i = 2 ; i <= N ; ++i) {
if(a[i] > a[p]) p = i;
}
res += a[p];
for(int i = 1 ; i <= N ; ++i) {
if(i != p) res += abs(a[i]);
}
for(int i = 1 ; i <= N ; ++i) {
if(i != p) {
ans.pb(mp(a[p],a[i]));
a[p] -= a[i];
}
}
}
else {
int p = 1,q;
for(int i = 2 ; i <= N ; ++i) {
if(a[i] < a[p]) p = i;
}
res -= a[p];
for(int i = 1 ; i <= N ; ++i) {
if(i != p) res += a[i];
}
if(p == 1) q = 2;
else q = 1;
for(int i = 1 ; i <= N ; ++i) {
if(i != p && i != q) {
ans.pb(mp(a[p],a[i]));
a[p] -= a[i];
}
}
ans.pb(mp(a[q],a[p]));
}
out(res);enter;
for(auto t : ans) {
out(t.fi);space;out(t.se);enter;
}
}
int main(){
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}
D - Squirrel Merchant
设\(f[i]\)表示有\(i\)个松果最多可以换成几个,用金银铜做背包就好了
不过这个背包大小最多可以是2500000……
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define ba 47
#define MAXN 100005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int64 f[25000005],N;
int g[5][5];
void Solve() {
read(N);
for(int i = 0 ; i < 2 ; ++i) {
for(int j = 0 ; j < 3 ; ++j) {
read(g[i][j]);
}
}
for(int i = 1 ; i <= N ; ++i) f[i] = i;
for(int i = 0 ; i < 3 ; ++i) {
for(int s = g[0][i] ; s <= N ; ++s) {
f[s] = max(f[s],f[s - g[0][i]] + g[1][i]);
}
}
int all = f[N];
for(int i = 1 ; i <= all ; ++i) f[i] = i;
for(int i = 0 ; i < 3 ; ++i) {
for(int s = g[1][i] ; s <= all ; ++s) {
f[s] = max(f[s],f[s - g[1][i]] + g[0][i]);
}
}
out(f[all]);enter;
}
int main(){
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}
E - Balanced Piles
计数水平不行……
这个就是,从\(0,N\)表示最大值是0,有\(N\)个数
并且我们认为相同的值选择顺序被我们确定了
如果\(D = 1\)
那么从\(x,y\),转移\(x + 1,0\)的时候,系数为1
从\(x,y\)转移到\(x,y + 1\)的时候,系数为\(y + 1\),因为我们要计数这个特定的选择顺序,在\(y\)个数的排列里插上一个数,要乘上\(y + 1\)
显然我们开头的部分需要一个\(N!\),但是由于\(H\)的\(N!\)不必要但是被算了,所以需要\(\frac{1}{N!}\),那么两个抵消了,所以可以直接这样计数
我们把转移画成一张图,发现每层转移到下一层的方案数是
\((1! + 2! +3!+4!....N!)\)
然后有\(H\)层,那么方案数是(第一层的方案是1)
\((1! + 2!+3!+4!+5!....N!)^{H - 1}N!\)
那么回到原来的问题,如果我们最大值交替一共过了\(K\)个,那么方案数是
\((1! + 2!+3!+4!+5!....N!)^{K - 1}N!\)
所以我们只要做一个路径计数,每走一步乘一个\(1! + 2!+3!+4!+5!....N!\),最后乘上一个\(\frac{N}{1!+2!+3!+4!...N!}\)
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define ba 47
#define MAXN 100005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
const int MOD = 1000000007;
int N,H,D;
int fac[1000005],dp[1000006],sum[1000006];
int inc(int a,int b) {
return a + b >= MOD ? a + b - MOD : a + b;
}
int mul(int a,int b) {
return 1LL * a * b % MOD;
}
void update(int &x,int y) {
x = inc(x,y);
}
int fpow(int x,int c) {
int res = 1,t = x;
while(c) {
if(c & 1) res = mul(res,t);
t = mul(t,t);
c >>= 1;
}
return res;
}
void Solve() {
read(N);read(H);read(D);
fac[0] = 1;
int c = 0;
for(int i = 1 ; i <= N ; ++i) {
fac[i] = mul(fac[i - 1],i);
update(c,fac[i]);
}
dp[0] = c;sum[0] = 1;
for(int i = 1 ; i <= H ; ++i) {
int t = sum[i - 1];
if(i - D > 0) update(t,MOD - sum[i - D - 1]);
dp[i] = mul(t,c);
sum[i] = inc(sum[i - 1],dp[i]);
}
int ans = mul(dp[H],fac[N]);ans = mul(ans,fpow(c,MOD - 2));
out(ans);enter;
}
int main(){
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}
F - Diverta City
水平不行,想不到
还是类似数学归纳法那么构造,假如构造了\(i\)个点的完全图使得所有哈密顿路径不同,我们找到哈密顿路径最大的那个是\(M\)
然后选择一个数列
1,2,4,7,12,20,29,38,53,73
设\(M\)为i个点中最长哈密顿路径最大的那个
新加一个点\(i +1\)的时候向\(j\)连一条长度为\((M + 1)a_{j}\)的边
这个数列任意两个数相加的值不同,且不等于其中任意一个数
这样每条路径经过了两条或一条这样的边,剩余的部分的边权不足以使得两个不同的\((M + 1)k\)相等
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define ba 47
#define MAXN 100005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int N,tot;
int64 w[15][15],M,a[] = {0,1,2,4,7,12,20,29,38,53,73};
bool vis[15];
void dfs(int dep,int pre,int64 sum) {
if(dep > tot) {
M = max(M,sum);
return;
}
for(int i = 1 ; i <= tot ; ++i) {
if(!vis[i]) {
vis[i] = 1;
dfs(dep + 1,i,sum + w[pre][i]);
vis[i] = 0;
}
}
}
void Solve() {
read(N);
M = 0;
for(int i = 2 ; i <= N ; ++i) {
for(int j = 1 ; j < i ; ++j) {
w[i][j] = w[j][i] = (M + 1) * a[j];
}
tot = i;
dfs(1,0,0);
}
for(int i = 1 ; i <= N ; ++i) {
for(int j = 1 ; j <= N ; ++j) {
out(w[i][j]);space;
}
enter;
}
}
int main(){
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}