莫队板子题,太菜了看了好久ww
#include
using namespace std;
typedef long long LL;
const int N = 1e5 + 7, M = 207;
int a[N], ans[N], group[N], n, m;
struct node
{
int l, r, id;
} q[N];
int res = 0, block;
int numcnt[N];
int cntcnt[N];
void add(int pos)
{
int t = a[pos];
numcnt[t]++;
cntcnt[numcnt[t]]++;
res = max(res, numcnt[a[pos]]);
}
void del(int pos)
{
int t = a[pos];
cntcnt[numcnt[t]]--;
if (res == numcnt[t] && cntcnt[numcnt[t]] == 0)
{
res--;
}
numcnt[t]--;
}
bool cmp(node a, node b)
{
if (a.l / block == b.l / block)
{
return a.r < b.r;
}
return a.l < b.l;
}
int main()
{
cin >> n >> m;
block = sqrt(n); //块的长度
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
for (int i = 1; i <= m; i++)
{
cin >> q[i].l >> q[i].r;
q[i].id = i;
}
sort(q + 1, q + 1 + m, cmp);
int l = 1, r = 0;
for (int i = 1; i <= m; i++)
{
while (q[i].l < l)
{
add(--l);
}
while (q[i].r > r)
{
add(++r);
}
while (q[i].l > l)
{
del(l++);
}
while (q[i].r < r)
{
del(r--);
}
ans[q[i].id] = res;
}
for (int i = 1; i <= m; i++)
{
cout << ans[i] << endl;
}
return 0;
}
二分
#include
using namespace std;
typedef long long lli;
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
const int maxn = 100005;
const int maxm = 200005;
typedef long long ll;
#define gcd __gcd
int pos[200005];
int vis[200005];
int n, k, m, length;
bool check(int key)
{
int cnt = 0, ma = 0;
for (int i = 1; i <= key; i++)
{
vis[pos[i]] = 1;
}
int last = 0;
vis[n + 1] = 1;
for (int i = 1; i <= n + 1; i++)
{
if (vis[i])
{
int d = i - last - 1;
if (d >= length)
{
cnt++;
d -= length;
}
cnt += (d / (length + 1));
last = i;
}
}
memset(vis, 0, sizeof vis);
return cnt < k;
}
int main()
{
int ans = inf;
cin >> n >> k >> length >> m;
for (int i = 1; i <= m; i++)
{
scanf("%d", &pos[i]);
}
if (k * length + k - 1 > n)
{
cout << 0;
return 0;
}
int l = 1, r = m;
while (l <= r)
{
int mid = (l + r) >> 1;
if (check(mid))
{
r = mid - 1;
ans = mid;
}
else
{
l = mid + 1;
}
}
if (ans == inf)
{
cout << -1;
return 0;
}
cout << ans;
return 0;
}
DAG上dp
#include
using namespace std;
typedef long long lli;
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
const int maxn = 100005;
const int maxm = 200005;
typedef long long ll;
int w[2005], dp[2005][2005];
vector<int> edge[2005];
void addedge(int u, int v)
{
edge[v].push_back(u); //到达v点的上一个点
}
int main()
{
//通过dp计算有多少种方法 晚训好像写过一次(太菜了没写出来)
//(xqqdltxdy代码一看就懂%%%%%%%)
ll ans = 0;
int n, m, k, u, v;
cin >> n >> m >> k;
for (int i = 1; i <= n; i++)
{
cin >> w[i];
}
for (int i = 1; i <= m; i++)
{
cin >> u >> v;
addedge(u, v);
}
//dp[i][j]: 到第i间商店花费j元的方案数
dp[1][0] = 1;
dp[1][w[1]] = 1;
for (int i = 1; i <= n; i++)
{
//不同方法指 不同路径或购买的不同礼物
//所以需要枚举路径 与 是否购买当前礼物
for (int j = 0; j < edge[i].size(); j++)
{
int p = edge[i][j]; //枚举到达i点 上一个点有多少点(路径枚举)
for (int h = 0; h <= k; h++)
{
dp[i][h] = (dp[i][h] + dp[p][h]) % mod;
if (h - w[i] >= 0)
dp[i][h] = (dp[i][h] + dp[p][h - w[i]]) % mod;
}
}
}
for (int i = 0; i <= k; i++)
{
ans += dp[n][i];
ans %= mod;
}
cout << ans;
return 0;
}
#include
using namespace std;
typedef long long lli;
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
lli a[1000005],vis[10000005];
int main()
{
int n;
while(~scanf("%d",&n)){
if(n==0) break;
if(n&1) cout<<"Bob"<<'\n';
else cout<<"Alice"<<'\n';
}
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define LL long long
const int inf = 0x3f3f3f3f;
const int mod = 1e5;
int a[1000005], n1[1000005], n2[1000005];
int main()
{
int n, v, pos;
cin >> n >> v;
for (int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
if (a[i] > v)
{
a[i] = -1;
}
else if (a[i] < v)
{
a[i] = 1;
}
else
{
a[i] = 0;
pos = i;
}
}
int sum = 0, ans = 0, cnt = 0;
for (int i = pos; i >= 1; i--)
{
sum += a[i]; //zuo
if (!sum)
{
ans++;
cnt++;
}
if ((pos - i) % 2 == 0)
{
n1[mod + sum]++;
}
else
{
n2[mod + sum]++;
}
}
sum = 0; //you
for (int i = pos + 1; i <= n; i++)
{
sum += a[i];
if (!sum)
{
ans += cnt;
}
else if ((i - pos) % 2 == 0)
{
ans += n1[mod - sum];
}
else
{
ans += n2[mod - sum];
}
}
cout << ans;
return 0;
}
最大生成树
#include
using namespace std;
typedef long long lli;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const int maxn = 100005;
const int maxm = 200005;
int f[maxn];
lli num[maxm];
int n, m, k, co;
struct node
{
int u, v;
lli w;
} edge[maxm];
int tot = 0;
void addedge(int u, int v, int w)
{
edge[tot].u = u;
edge[tot].v = v;
edge[tot++].w = w;
}
bool cmp(node a, node b)
{
return a.w > b.w;
}
int find(int x)
{
if (f[x] == -1)
return x;
else
return f[x] = find(f[x]);
}
int main()
{
cin >> n >> m >> k;
int x, y, z;
for (int i = 1; i <= m; i++)
{
scanf("%d%d%d", &x, &y, &z);
addedge(x, y, z);
}
memset(f, -1, sizeof f);
sort(edge, edge + tot, cmp);
int cnt = 0;
int ans = 0;
vector<lli> mii;
vector<lli> maa;
for (int i = 0; i < tot; i++)
{
int u = edge[i].u;
int v = edge[i].v;
int w = edge[i].w;
int t1 = find(u);
int t2 = find(v);
if (t1 != t2)
{
if (w > k)
{
maa.push_back(w);
}
else if (w < k)
{
mii.push_back(w);
}
f[t1] = t2;
//cout << "%" << t1 << ' ' << t2 << " cost " << w << endl;
cnt++;
}
if (cnt == n - 1)
break;
}
lli res = 0;
if (mii.size())
{
for (auto i : mii)
{
res += k - i;
}
cout << res;
return 0;
}
else
{
res = inf;
for (int i = 0; i < tot; i++)
{
res = min(res, abs(edge[i].w - k));
}
cout << res;
return 0;
}
return 0;
}
三维dp,现在都还不太懂))
dp[i][j][k]表示在第i个数,第k个块gcd为j
#include
using namespace std;
typedef long long LL;
const int N = 1e4 + 7;
int a[N], dp[N][105][55];
int main()
{
//memset(dp, -1, sizeof(dp));
int n, k;
int ans = -1;
cin >> n >> k;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
dp[1][a[1]][1] = a[1];
for (int i = 2; i <= n; i++)
{
for (int j = 1; j <= 100; j++)
{
int g = __gcd(j, a[i]);
for (int h = 1; h <= min(k, i); h++)
{
//放到新一组
if (dp[i - 1][j][h - 1])
dp[i][a[i]][h] = max(dp[i][a[i]][h], dp[i - 1][j][h - 1] + a[i]);
//合并
if (dp[i - 1][j][h])
dp[i][g][h] = max(dp[i][g][h], dp[i - 1][j][h] - j + g);
}
}
}
for (int i = 1; i <= 100; i++)
{
ans = max(ans, dp[n][i][k]);
}
cout << ans;
return 0;
}