2016NOIP提高組複賽山東賽區滿分選手程式碼片段彙編
阿新 • • 發佈:2019-01-29
toy.cpp
#include <cstdio>
const int MAXN = 100005, MAXL = 13;
int n, m, dir[MAXN];
char name[MAXN][MAXL];
int f(int x){return x ? 1 : -1;}
int main(){
int i, cur, d, s, delta;
freopen("toy.in", "r", stdin);
freopen("toy.out", "w", stdout);
scanf("%d%d", &n, &m);
for(i = 0 ; i < n; ++ i)
scanf("%d%s", &dir[i], name[i]);
for(i = 1, cur = 0; i <= m; ++ i){
scanf("%d%d", &d, &s);
delta = f(d ^ dir[cur]) * s;
cur = (cur + delta + n) % n;
} printf("%s\n", name[cur]);
return 0;
}
problem.cpp
#include <cstdio>
#include <ctime>
const int MAXN = 2005;
int C[MAXN][MAXN], T, k;
int main(){
freopen("problem.in", "r", stdin);
freopen("problem.out", "w", stdout);
int i, j, n, m;
scanf("%d%d", &T, &k);
for(i = 0; i <= 2000; ++ i)
for(j = C[i][0] = 1; j <= i; ++ j)
if ((C[i][j] = C[i - 1][j] + C[i - 1][j - 1]) >= k)
C[i][j] -= k;
for(i = 0; i <= 2000; ++ i)
for(j = 0; j <= i; ++ j)
C[i][j] = (C[i][j] ? 0 : 1);
for(i = 1; i <= 2000; ++ i)
for(j = 1; j <= 2000; ++ j)
C[i][j] += C[i - 1][j] + C[i][j - 1] - C[i - 1][j - 1];
while(T --){
scanf("%d%d", &n, &m);
printf("%d\n", C[n][m]);
} return 0;
}
running.cpp
#include <cstdio>
const int MAXN = 300005, MAXM = 300005, MAXLOG = 21, MAXNODE = MAXN * MAXLOG * 4;
int g[MAXN], ins1[MAXN], ins2[MAXN], del1[MAXN], del2[MAXN];
int nxt[MAXN * 2], edgeSize, nodeSize, rht[MAXNODE];
int to[MAXN * 2], w[MAXN], cnt[MAXNODE], lft[MAXNODE];
int pre[MAXN][MAXLOG], fa[MAXN], dep[MAXN], n, m, ans[MAXN];
void addEdge(int u, int v){
nxt[++ edgeSize] = g[u];
to[g[u] = edgeSize] = v;
}
void dfsCalc(int x){
int p; pre[x][0] = fa[x];
for(p = 1; (1 << p) < dep[x]; ++ p)
pre[x][p] = pre[pre[x][p - 1]][p - 1];
for(p = g[x]; p; p = nxt[p]){
if(fa[x] == to[p]) continue;
fa[to[p]] = x;
dep[to[p]] = dep[x] + 1;
dfsCalc(to[p]);
}
}
void sgtIns(int &x, int l, int r, int p, int v){
if(!x) x = ++ nodeSize;
cnt[x] += v; if(l == r) return;
int mid = (l + r) >> 1;
if(p <= mid) sgtIns(lft[x], l, mid, p, v);
else sgtIns(rht[x], mid + 1, r, p, v);
}
int merge(int u, int v){
if(!u) return v;
if(!v) return u;
cnt[u] += cnt[v];
lft[u] = merge(lft[u], lft[v]);
rht[u] = merge(rht[u], rht[v]);
return u;
}
void add1(int s, int t, int v){
sgtIns(ins1[s], 1, 2 * n, v, 1);
sgtIns(del1[t], 1, 2 * n, v, -1);
}
void add2(int s, int t, int v){
sgtIns(ins2[s], 1, 2 * n, v + n, 1);
sgtIns(del2[t], 1, 2 * n, v + n, -1);
}
void getLCA(const int s, const int t){
if(s == t){
add1(s, s, dep[s]);
return;
} int p, u = s, v = t;
if(dep[u] > dep[v]){
for(p = 18; p >= 0; -- p)
if((dep[u] - (1 << p)) > dep[v])
u = pre[u][p];
if(pre[u][0] == v){
add1(s, t, dep[s]);
return;
} u = pre[u][0];
}else if(dep[v] > dep[u]){
for(p = 18; p >= 0; -- p)
if(dep[v] - (1 << p) > dep[u])
v = pre[v][p];
if(pre[v][0] == u){
add2(t, s, dep[s]);
return;
} v = pre[v][0];
}
for(p = 18; p >= 0; -- p)
if(pre[u][p] != pre[v][p])
u = pre[u][p], v = pre[v][p];
add1(s, pre[u][0], dep[s]);
add2(t, v, 2 * dep[pre[u][0]] - dep[s]);
}
int sgtQuery(int x, int l, int r, int p){
if(l == r) return cnt[x];
int mid = (l + r) >> 1;
if(p <= mid) return sgtQuery(lft[x], l, mid, p);
return sgtQuery(rht[x], mid + 1, r, p);
}
void dfsGet(int x){
int p;
for(p = g[x]; p; p = nxt[p]){
if(to[p] == fa[x]) continue;
dfsGet(to[p]);
ins1[x] = merge(ins1[x], ins1[to[p]]);
ins2[x] = merge(ins2[x], ins2[to[p]]);
}
ans[x] = sgtQuery(ins1[x], 1, 2 * n, dep[x] + w[x]) + sgtQuery(ins2[x], 1, 2 * n, dep[x] - w[x] + n);
ins1[x] = merge(ins1[x], del1[x]);
ins2[x] = merge(ins2[x], del2[x]);
}
inline int getInt(){
char x; int ret;
while((x = getchar()) < '0' || x > '9') ;
ret = x - '0';
while((x = getchar()) >= '0' && x <= '9')
ret = ret * 10 - '0' + x;
return ret;
}
int main(){
int i, u, v;
freopen("running.in", "r", stdin);
freopen("running.out", "w", stdout);
n = getInt(), m = getInt();
for(i = 1; i < n; ++ i){
u = getInt(), v = getInt();
addEdge(u, v), addEdge(v, u);
} dep[1] = 1, dfsCalc(1);
for(i = 1; i <= n; ++ i) w[i] = getInt();
for(i = 1; i <= m; ++ i){
u = getInt(), v = getInt();
getLCA(u, v);
} dfsGet(1);
for(i = 1; i <= n; ++ i)
printf("%d%c", ans[i], i == n ? '\n' : ' ');
return 0;
}
angrybirds.cpp
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
const int MAXN = 19;
int dp[1 << 18], n, type, nxt[MAXN][MAXN];
double x[MAXN], y[MAXN];
int DP(int st){
if(~dp[st]) return dp[st];
int &now = dp[st], i, j, cnt = 0, *Nxt;
now = 10000;
for(i = 0; i < n; ++ i){
if(st & (1 << i)) continue; ++ cnt;
for(j = i + 1, Nxt = nxt[i]; j < n; ++ j){
if(st & (1 << j)) continue;
if(!Nxt[j]) continue;
now = min(now, DP(st | Nxt[j]) + 1);
}
} return now = min(now, cnt);
}
void solve(){
int i, j, t; double k, a, b;
scanf("%d%d", &n, &type);
for(i = 0; i < n; ++ i)
cin >> x[i] >> y[i];
memset(nxt, 0, sizeof(nxt));
for(i = 0; i < n; ++ i)
for(j = i + 1; j < n; ++ j){
if(fabs(x[i] - x[j]) < 1e-8) continue;
k = (y[i] - y[j]) / (x[i] - x[j]);
a = k / x[j] - y[i] / x[i] / x[j];
b = k - a * (x[i] + x[j]);
if(a >= 0.0) continue;
for(t = j + 1, nxt[i][j] = (1 << i) | (1 << j); t < n; ++ t)
if(abs(a * x[t] * x[t] + b * x[t] - y[t]) < 1e-8)
nxt[i][j] |= 1 << t;
}
memset(dp, -1, sizeof(dp));
dp[(1 << n) - 1] = 0;
printf("%d\n", DP(0));
}
int main(){
freopen("angrybirds.in", "r", stdin);
freopen("angrybirds.out", "w", stdout);
int t; scanf("%d", &t);
while(t --) solve();
return 0;
}
classroom.cpp
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 2003;
int dis[303][303], n, m, v, e, c[N], d[N];
double f[N][N][2], k[N];
int main() {
freopen("classroom.in", "r", stdin);
freopen("classroom.out", "w", stdout);
scanf("%d%d%d%d", &n, &m, &v, &e);
for (int i = 1; i <= n; ++i) scanf("%d", c + i);
for (int i = 1; i <= n; ++i) scanf("%d", d + i);
for (int i = 1; i <= n; ++i) scanf("%lf", k + i);
int a, b, w;
for (int i = 1; i <= v; ++i)
for (int j = 1; j <= v; ++j)
dis[i][j] = 500000000;
for (int i = 1; i <= e; ++i) {
scanf("%d%d%d", &a, &b, &w);
dis[a][b] = min(dis[a][b], w);
dis[b][a] = min(dis[b][a], w);
}
for (int i = 1; i <= v; ++i) dis[i][i] = 0;
for (int kk = 1; kk <= v; ++kk)
for (int i = 1; i <= v; ++i)
for (int j = 1; j <= v; ++j)
dis[i][j] = min(dis[i][j], dis[i][kk] + dis[kk][j]);
// for (int i = 1; i <= n; ++i)
// for (int j = 0; j <= m; ++j)
// f[i][j][0] = f[i][j][1] = 100000000000000.0;
f[1][1][1] = f[1][0][0] = 0;
for (int i = 2; i <= n; ++i)
for (int j = 0; j <= m; ++j) {
if (j > i) break;
if (j < i) {
if (j > 0) {
f[i][j][0] = f[i - 1][j][1] + (1.0 - k[i - 1]) * dis[c[i - 1]][c[i]] + k[i - 1] * dis[d[i - 1]][c[i]];
if (j < i - 1)
f[i][j][0] = min(f[i][j][0], f[i - 1][j][0] + dis[c[i - 1]][c[i]]);
} else
f[i][j][0] = f[i - 1][j][0] + dis[c[i - 1]][c[i]];
}
if (j > 0) {
if (j > 1) {
f[i][j][1] =
f[i - 1][j - 1][1]
+ (1.0 - k[i]) *
((1.0 - k[i - 1]) * dis[c[i - 1]][c[i]] + k[i - 1] * dis[d[i - 1]][c[i]])
+ k[i] *
((1.0 - k[i - 1]) * dis[c[i - 1]][d[i]] + k[i - 1] * dis[d[i - 1]][d[i]]);
if (j < i)
f[i][j][1] = min(f[i][j][1],
f[i - 1][j - 1][0] + (1.0 - k[i]) * dis[c[i - 1]][c[i]] + k[i] * dis[c[i - 1]][d[i]]);
} else
f[i][j][1] =
f[i - 1][j - 1][0] + (1.0 - k[i]) * dis[c[i - 1]][c[i]] + k[i] * dis[c[i - 1]][d[i]];
}
}
double ans = f[n][0][0];
for (int j = 1; j <= m; ++j) {
if (j <= n)
ans = min(ans, f[n][j][1]);
if (j < n)
ans = min(ans, f[n][j][0]);
}
printf("%.2lf\n", ans);
return 0;
}
earthworm.cpp
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e5+10;
const int M=7*1e6+10;
int n,m,q,u,v,t;
int a[N],b[M],c[M];
int t1[M],t2[M];
int lb,rb,lc,rc,la;
bool cmp(int a,int b)
{
return a>b;
}
int check(int ti)
{
int opt=0;
int ans;
if(la<=n) opt=1,ans=(ti-1)*q+a[la];
if(lb<=rb)
{
if(opt==0) opt=2,ans=(ti-t1[lb])*q+b[lb];
else
{
if(ans<(ti-t1[lb])*q+b[lb])
{
opt=2;
ans=(ti-t1[lb])*q+b[lb];
}
}
}
if(lc<=rc)
{
if(opt==0||ans<(ti-t2[lc])*q+c[lc])
{
opt=3;
ans=(ti-t2[lc])*q+c[lc];
}
}
if(opt==1) la++;
else if(opt==2) lb++;
else lc++;
return ans;
}
int main()
{
freopen("earthworm.in","r",stdin);
freopen("earthworm.out","w",stdout);
scanf("%d%d%d%d%d%d",&n,&m,&q,&u,&v,&t);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
sort(a+1,a+n+1,cmp);
la=lb=lc=1;
for(int i=1;i<=m;i++)
{
long long now=check(i);
b[++rb]=now*u/v;
c[++rc]=now-b[rb];
t1[rb]=t2[rc]=i+1;
if(i%t==0) printf("%d ",now);
}
cout<<endl;
for(int i=1;i<=n+m;i++)
{
int now=check(m+1);
if(i%t==0) printf("%d ",now);
}
fclose(stdin);
fclose(stdout);
return 0;
}