statement
1. 工作安排(T1,时限1s,内存512MB)
题目描述
Alice有一家公司,她手下有 名员工,每名员工每天可以完成一项任务。 在接下来的 天里,每天有 个( )任务待完成,Alice需要在第 天选择 个员工完成这些任务。 如果一个员工连续 天( )都在工作,那么他在这 天中一共会产生 点疲劳值。 Alice想安排一个优秀的上班方案,使得在这 天内所有员工产生的疲劳值之和尽可能小。
输入格式
输入一行两个整数 、。 接下来一行输入 个整数 。
输出格式
输出一行一个整数,表示这 天内所有员工产生的疲劳值之和的最小值。
输入输出样例
样例输入1:
4 7
1 2 3 4 3 2 1样例输出1:
8说明/提示
- 对于 的数据,
- 对于另外 的数据,
- 对于另外 的数据,
- 对于全部数据,,,
题解
考虑贪心。只要某一人第 天与第 都工作了,就会产生 的贡献。 只需要这一天的活尽量不让上一天干活的人做就行了,上一天不得不做这一天的活的人数即为这一天所带来的贡献。
代码
自己的:
#include<cstdio>
#include<iostream>
#define ll long long
#define re register
#define endl '\n'
using namespace std ;
template<class T>inline void read(T &x){x=0;T f=1;char c=getchar();while(c<'0'||c>'9'){if (c=='-'){f=-1;}c=getchar();}while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+(c^48);c=getchar();}x*=f;}
template<class T>inline void nout(T x){if(x<0){x=-x;putchar('-');}if(x>9){nout(x/10);}putchar(x%10+'0');}
template<class T>inline void write(T x,char c){nout(x),putchar(c);}
const int M = 1e6 + 2 ;
ll ans ;
int a[M],m,n ;
int main () {
read(n),read(m) ;
for (re int i = 1 ; i <= m ; ++i) {
read(a[i]) ;
if (i == 1) continue ;//第一天跳过
ans += max(a[i] - n + a[i - 1],0) ;
//a[i] - (n - a[i - 1])若>0,则有贡献,否则没有贡献(上一天的人都可以不用干这一天的活)
}
nout(ans) ;
return 0 ;
}std:
//与自己的大致一样,故略2. 找排列(T2,时限1s,内存512MB)
题目描述
有一个长度为 的序列:。 你需要删除若干序列中的元素,将剩余元素按顺序链接为一个新的序列,使得在这个新的序列中存在一个子段为长度为 的排列。
补充定义:
- 长度为 的排列:一个长度为 的序列中包含 的数字各一个。
- 子段:删除一个序列开头和结尾任意多个元素(可以是零个)后得到的序列。
输入格式
第一行两个整数 、。 接下来一行 个整数 。
输出格式
输出一行一个整数表示最少删除元素的数量;如果无论如何都无法出现长度为 的子段,则输出 。
输入输出样例
样例输入1:
6 3
1 1 4 2 2 3样例输出1:
2说明/提示
- 对于 的数据,
- 对于另外 的数据,
- 对于另外 的数据,
- 对于全部数据,,,
题解
规定 删掉若干个元素后为 。
先找一个 的子段 , 满足 每个数都至少在 中出现一次,最小化 长度 ,答案即为 。
- 找到这个 最小的 ,目的是让 这 个数之外的数保留的最多。
- 删掉 个数后,剩下的就是 的排列。
- 此时这个排列正好是 的子段。
- 因为我们的 找的是最小的,所以 一定是最小的,满足题意。
找 的实现考虑用双指针。
代码
自己的
#include<cstdio>
#include<iostream>
#include<cstring>
#define ll long long
#define re register
#define endl '\n'
using namespace std ;
template<class T>inline void read(T &x){x=0;T f=1;char c=getchar();while(c<'0'||c>'9'){if(c=='-'){f=-1;}c=getchar();}while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+(c^48);c=getchar();}x*=f;}
template<class T>inline void nout(T x){if(x<0){x=-x;putchar('-');}if(x>9){nout(x/10);}putchar(x%10+'0');}
template<class T>inline void write(T x,char c){nout(x),putchar(c);}
const int N = 1e6 + 2 ;
int a[N],n,m,t[N],cnt,ans = 0x3f3f3f3f ;
//cnt记录1~m已经在子段中的个数,ans记录答案
int main () {
//freopen("arrangement.in","r",stdin);
//freopen("arrangement.out","w",stdout);
read(n),read(m) ;
for (re int i = 1 ; i <= n ; ++i) read(a[i]),++t[a[i]] ;
for (re int i = 1 ; i <= m ; ++i) { //如果连1~m都凑不齐,直接-1
if (!t[i]) {
nout(-1) ;
return 0 ;
}
}
memset(t,0,sizeof(t)) ; //清空,用于记录当前子段中1~m的出现次数
int l = 1,r = 1 ; //双指针
while (r <= n) {
if (a[r] <= m) { //当前数<=m
if (t[a[r]] == 0) ++cnt ; //考虑是否在子段中第一次出现
++t[a[r]] ; //出现次数++
}
while (cnt == m) { //在保持子段中有1~m的同时收缩左指针
ans = min(ans,r - l + 1) ; //更新答案
if (a[l] <= m) { //碰到<=m的数
--t[a[l]] ; //出现次数--
if (t[a[l]] == 0) --cnt ; //减完后这个数在子段中已经没了,--cnt
}
++l ;
}
++r ; //右指针增加,收缩左指针
}
if(ans == 0x3f3f3f3f) nout(-1) ; //没有答案
else nout(ans - m) ; //最后是(子段长度-m)
return 0;
}std(二分做法):
#include <bits/stdc++.h>
using namespace std;
const int N=1.01e6;
int n,m,a[N],ans=-1,t[N];
bool ck(int x){
for(int i=1;i<=n;i++)t[i]=0;
int c=0;
for(int i=1;i<=m+x;i++){
++t[a[i]];
if(a[i]<=m&&t[a[i]]==1)++c;
}
if(c==m)return true;
for(int i=m+x+1;i<=n;i++){
++t[a[i]];
if(a[i]<=m&&t[a[i]]==1)++c;
--t[a[i-m-x]];
if(a[i-m-x]<=m&&t[a[i-m-x]]==0)--c;
if(c==m)return true;
}
return false;
}
signed main(){
//freopen("arrangement.in","r",stdin);
//freopen("arrangement.out","w",stdout);
cin>>n>>m;
for(int i=1;i<=n;i++)cin>>a[i];
int L=0,R=n-m;
while(L<=R){
int M=(L+R)/2;
if(ck(M))ans=M,R=M-1;
else L=M+1;
}
printf("%d\n",ans);
fclose(stdin);
fclose(stdout);
return 0;
}
3. 混乱字符(T3,时限1s,内存512MB)
题目描述
有一个长度为 的小写字母组成的字符串 ,将原本字符串的字符顺序打乱得到字符串 ,原字符串中每个字符移动距离不会超过 。
形式化定义:设 中第 个字符打乱后在 中的位置是 ( 为一个排列),且满足 。
打乱后有些位置的字符不清楚了,不清楚的位置记作问号(?),可能是任意字符。
给你原本的字符串 和打乱后的字符串 ,有多少种可能的排列 ?由于答案较大,你只需要输出答案在模 意义下的结果即可。
输入格式
第一行两个整数 、,表示字符串的长度和最远的移动距离。 接下来两行分别输入字符串 、。
输出格式
一行一个整数,表示可能的排列的数量对 取模的答案。
输入输出样例
样例输入1:
2 1
ab
?a样例输出1:
1样例输入2:
2 1
ab
??样例输出2:
2样例输入3:
4 1
abab
?a??样例输出3:
7样例输入4:
32 3
aabaaabaacbabcbaabbbadbcacdcbcbb
???ab???ba???aa?????a?????????c?样例输出4:
959403474说明/提示
- 字符串由小写字母和问号组成。
- 对于 的数据,
- 对于另外 的数据, 中仅包含 。
- 对于另外 的数据, 中没有问号
- 对于全部数据:,, 由小写字母构成, 由小写字母和问号构成
题解
代码
自己的:
std(状压DP):
#include<cstdio>
#include<iostream>
#define ll long long
#define re register
#define endl '\n'
using namespace std ;
template<class T>inline void read(T &x){x=0;T f=1;char c=getchar();while(c<'0'||c>'9'){if (c=='-'){f=-1;}c=getchar();}while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+(c^48);c=getchar();}x*=f;}
template<class T>inline void nout(T x){if(x<0){x=-x;putchar('-');}if(x>9){nout(x/10);}putchar(x%10+'0');}
template<class T>inline void write(T x,char c){nout(x),putchar(c);}
const int N = 1e5 + 2 ;
const int MOD = 998244353 ;
int n,k,len,f[N][128] ;
char a[N],b[N] ;
int main () {
//freopen("string.in","r",stdin) ;
//freopen("string.out","w",stdout) ;
read(n),read(k) ;
len = 2 * k + 1 ;
scanf("%s %s",a + 1,b + 1) ;
f[0][(1 << (k + 1)) - 1] = 1 ;
for (int i = 1 ; i <= n ; i++) {
for (int x = 0 ; x < (1 << len) ; x++)
if (x & 1) {
int y = x / 2 ;
for(int j = -k ; j <= k ; j++)
if ((!(y & (1 << (j + k)))) && (b[i] == '?' || b[i] == a[i + j])) {
f[i][y | (1 << (j + k))] += f[i - 1][x] ;
f[i][y | (1 << (j + k))] %= MOD ;
}
}
}
nout(f[n][(1 << (k + 1)) - 1]) ;
return 0 ;
}4. 椴树(T4,时限1s,内存512MB)
题目背景
椴树(Tilia tuan Szyszyl.)是锦葵科椴属乔木植物。
题目描述
给定一棵有根树, 号节点为根节点。阿拉伯诗人 zqc 做了 个梦,在每个梦中,这棵树被割断了 条边,zqc 想知道这棵树所有连通的点对距离和是多少。
具体来说:对于树上的所有点对 ,满足 并且 、 连通,zqc想知道 和 在树上简单路径的长度之和。 注意:每一场梦是独立的,树不会真的被割断。
输入格式
第一行输入两个数 、,表示树的大小和梦的数量。 接下来 行,每行两个数 、,表示树上一条边。 接下来 行,每行若干整数:第一个数 表示这场梦被割断的边的数量,后面有 个节点的标号 ,表示这个节点和父节点连接的边被断开了。
输出格式
输出共 行,第 行表示第 场梦中所有连通的点对距离和是多少。
输入输出样例
样例输入1:
5 5
1 2
2 3
1 4
2 5
3 4 2 3
1 5
3 5 2 3
3 4 5 2
1 3样例输出1:
1
10
1
1
10样例输入2:
8 8
1 2
2 3
2 4
3 5
1 6
3 7
4 8
2 4 8
2 7 3
1 7
2 7 2
3 8 3 7
3 4 5 7
3 3 5 4
1 7样例输出2:
32
21
48
21
11
11
6
48说明/提示
- 对于 的数据,,
- 对于另外 的数据,,
- 对于另外 的数据,
- 对于另外 的数据,
- 对于全部数据,,,,
题解
代码
自己的:
std:
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <iostream>
#include <cstdlib>
#include <ctime>
#define int long long
#define ull unsigned long long
using namespace std;
inline int read(){int qrx=0,qry=1;char qrc=getchar();
while(qrc<'0'||qrc>'9'){if(qrc=='-')qry=-1;qrc=getchar();}
while(qrc>='0'&&qrc<='9')qrx=qrx*10+qrc-48,qrc=getchar();return qrx*qry;}
const int N = 4e5 + 7,Mod = 998244353,INF = 1e9 + 7 ;
signed n,m,hd[N],nxt[N],to[N],ec ;
int f[N],g[N],Sum,ans,k,sz[N] ;
signed fa[N],dep[N],son[N],dfn[N],top[N],Time ;
vector<signed> ch[N] ;
signed cl[N],tot ;
inline void link (int u,int v) {
if (!ch[u].size()) cl[++tot] = u ;
ch[u].push_back(v) ;
return ;
}
struct node{
signed x,y ;
node(signed x = 0,signed y = 0) : x(x),y(y) {} ;
bool operator < (const node& A) const {return y < A.y ;}
}p[N] ;
inline void add (signed u,signed v) {
nxt[++ec] = hd[u],hd[u] = ec,to[ec] = v ;
return ;
}
void dfs0 (signed u,signed from) {
dep[u] = dep[from] + 1,sz[u] = 1,fa[u] = from ;
for (signed i = hd[u],v ; i ; i = nxt[i]) {
v = to[i] ;
if (v == from) continue ;
dfs0(v,u) ;
if(sz[son[u]] < sz[v]) son[u] = v ;
f[u] += f[v] + sz[v],sz[u] += sz[v] ;
}
return ;
}
void dfs1 (signed u,signed from) {
if (u == 1) g[u] = f[u] ;
else g[u] = g[from] - sz[u] + n - sz[u] ;
Sum += g[u] ;
for (signed i = hd[u],v ; i ; i = nxt[i]) {
v = to[i] ;
if (v == from) continue ;
dfs1(v,u) ;
}
return ;
}
void dfs2 (signed u,signed from,signed t) {
dfn[u] = ++Time,top[u] = t ;
if (son[u]) dfs2(son[u],u,t) ;
for (signed i = hd[u],v ; i ; i = nxt[i]) {
v = to[i] ;
if (v == from || v == son[u]) continue ;
dfs2(v,u,v) ;
}
return ;
}
node dfs3 (signed u,signed from) {
vector<node> a ;
if (!ch[u].size()) return node(f[u],sz[u]) ;
else {
int sx = 0,sy = 0 ;
for (unsigned i = 0 ; i < ch[u].size() ; i++) {
signed v = ch[u][i] ;
node p = dfs3(v,u) ;
p.x += p.y * (dep[v] - dep[u]) ;
a.push_back(p),sx += p.x,sy += p.y ;
}
node res = node(sx,sy) ;
for(unsigned i = 0;i < a.size() ; i++)
sx -= a[i].x,sy -= a[i].y,ans += sx*a[i].y + sy*a[i].x ;
return res ;
}
}
inline signed LCA (signed x,signed y) {
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x,y) ;
x = fa[top[x]] ;
}
return dep[x] < dep[y] ? x : y ;
}
int F[N],S[N] ;
signed stk[N],tp ;
void sol (signed L,signed R) {
vector<node> a ;
signed pl = L + 1,pr = 0,u = p[L].x ;
F[u] = f[u],S[u] = sz[u] ;
while (pl <= R) {
signed l = pl,r = R,v = p[pl].x,mid = 0 ;
while (l <= r) {
mid = (l + r) / 2 ;
if (p[mid].y < dfn[v] + sz[v]) pr = mid,l = mid + 1 ;
else r = mid - 1 ;
}
sol(pl,pr) ;
F[u] = F[u] - f[v] - sz[v] * (dep[v] - dep[u]) ;
S[u] = S[u] - sz[v] ;
a.push_back(node(v,dfn[v])) ;
pl = pr + 1 ;
}
ans -= F[u] * (n - sz[u]) + (g[u] - f[u]) * S[u] ;
sort(a.begin(),a.end()),tp = 0 ;
stk[++tp] = u ;
for (unsigned i = 0 ; i < a.size() ; i++) {
int p = a[i].x,lca = LCA(stk[tp],p) ;
while (tp > 1 && dep[lca] <= dep[stk[tp - 1]])
link(stk[tp - 1],stk[tp]),--tp ;
if (dep[lca] < dep[stk[tp]])
link(lca,stk[tp]),--tp,stk[++tp] = lca ;
stk[++tp] = p ;
}
for (signed i = 1 ; i < tp ; i++) link(stk[i],stk[i + 1]) ;
dfs3(u,0) ;
for (signed i = 1 ; i <= tot ; i++) ch[cl[i]].resize(0) ;
tot = 0 ;
return ;
}
#define pc putchar
inline void WRI(int x){if(x<0)pc(45),x=-x;if(x>=10)WRI(x/10);pc(48+x%10);return;}
signed main(){
freopen("linden.in","r",stdin);
freopen("linden.out","w",stdout);
n = read(),m = read() ;
for (signed i = 1,u,v ; i < n ; i++) u = read(),v = read(),add(u,v),add(v,u) ;
dfs0(1,0) ;
dfs1(1,0) ;
dfs2(1,0,1) ;
Sum /= 2 ;
while (m--) {
k = read(),ans = Sum ;
for(signed i = 1,x ; i <= k ; i++) x = read(),p[i] = node(x,dfn[x]) ;
sort(p + 1 ,p + k + 1) ;
p[0] = node(1,1) ;
sol(0,k) ;
WRI(ans),putchar('\n') ;
}
return 0 ;
}