每日一题一期题单
- 1~10题
- 01[数码](https://ac.nowcoder.com/acm/problem/13221)
- 02[二分图染色(弱化版)](https://ac.nowcoder.com/acm/problem/13229)
- 03[合并回文子串](https://ac.nowcoder.com/acm/problem/13230)
- 04[黑白树](https://ac.nowcoder.com/acm/problem/13249)
- 05[城市网络](https://ac.nowcoder.com/acm/problem/13331)
- 06[树](https://ac.nowcoder.com/acm/problem/13611)
- 07[Shortest Path](https://ac.nowcoder.com/acm/problem/13886)
- 08[Contest](https://ac.nowcoder.com/acm/problem/13947)
- 09[Xorto](https://ac.nowcoder.com/acm/problem/14247)
- 10[Treepath](https://ac.nowcoder.com/acm/problem/14248)
- 11~20题
- 11[K-th Number](https://ac.nowcoder.com/acm/problem/14301)
- 12[小AA的数列](https://ac.nowcoder.com/acm/problem/14414)
- 13[糖糖别胡说,我真的不是签到题目](https://ac.nowcoder.com/acm/problem/14583)
- 14[储物点的距离](https://ac.nowcoder.com/acm/problem/14683)
- 15[模拟战役](https://ac.nowcoder.com/acm/problem/14698)
- 16[美味菜肴](https://ac.nowcoder.com/acm/problem/14704)
- 17[逆序对](https://ac.nowcoder.com/acm/problem/14731)
- 18[比赛](https://ac.nowcoder.com/acm/problem/14734)
- 19[codeJan与旅行](https://ac.nowcoder.com/acm/problem/14844)
- 20[德玛西亚万岁](https://ac.nowcoder.com/acm/problem/15034)
- 21~30
- 31~40
- 41~50
1~10题
01数码
02二分图染色(弱化版)
任意两条红边不共享端点,同时任意两条蓝边也不共享端点。
如果映射到二维坐标系上,红点所在的行与列没有其他的红点
蓝点所在的行与列没有其他的蓝点,其他的点均为绿色。
而且,这是一个完全二部图,说明对于任意的一个x节点,都与所有的y
节点对应。所以,这是一个n行n列的图。
重点是推公式,其他的都是模板了。
雨巨的推导与证明
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7, N = 1e7 + 7;
ll f[N], fac[N], inv[N];
void init(ll n);
ll A(ll n, ll m);
ll C(ll n, ll m);
ll qmi(ll n, ll m);
int main()
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
init(1e7);
int n; cin >> n;
ll ans = 0;
for (ll i = 0; i <= n; i ++ )
{
ll k = i & 1 ? -1 : 1;
ans += k * C(n, i) * A(n, i) % mod * f[n - i] % mod * f[n - i] % mod;
ans %= mod;
}
cout << (ans + mod) % mod << endl;
return 0;
}
void init(ll n)
{
fac[0] = 1;
for (ll i = 1; i <= n; i ++ )
fac[i] = fac[i - 1] * i % mod;
inv[n] = qmi(fac[n], mod - 2);
for (ll i = n - 1; i >= 0; i -- )
inv[i] = inv[i + 1] * (i + 1) % mod;
f[0] = 1, f[1] = 2;
for (ll i = 2; i <= n; i ++ )
{
f[i] = (2 * i * f[i - 1] - (i - 1) % mod * (i - 1) % mod * f[i - 2]) % mod;
f[i] = (f[i] + mod) % mod;
}
}
ll A(ll n, ll m)
{
return fac[n] * inv[n - m] % mod;
}
ll C(ll n, ll m)
{
return fac[n] * inv[m] % mod * inv[n - m] % mod;
}
ll qmi(ll a, ll b)
{
ll ans = 1;
while (b)
{
if (b & 1) ans = ans * a % mod;
a = a * a % mod;
b >>= 1;
}
return ans;
}
03合并回文子串
04黑白树
05城市网络
06树
07Shortest Path
雨巨题解
我们发现,在满足min的条件下,每条边都只被选一次,就算被选两次,我们亦可以化为只选一次这种更优的方案。
首先,叶子节点一定都与父亲节点连接。
然后,如果子树为偶数个,可以内部完成
如果子树节点个数为奇数的话,需要向父亲节点连一条。
那么,我们可以进行dfs操作,只讲树的大小为奇数的边加入答案,这里的tot数组记录的就是树的大小。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Node
{
int t, w, next;
}edge[21000];
int n, cnt;
int head[10010];
int tot[10010];
ll ans;
void add(int a, int b, int c)
{
// e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
edge[++ cnt].t = b;
edge[cnt].w = c;
edge[cnt].next = head[a];
head[a] = cnt;
}
void dfs(int x, int fa, int len)
{
tot[x] = 1;
for (int i = head[x]; ~i; i = edge[i].next)
{
int y = edge[i].t;
if (y == fa) continue;
dfs(y, x, edge[i].w);
tot[x] += tot[y];//计算当前子树的节点个数
// cout << "for内的:" << tot[x] << endl;
// printf("for_in_x = %d for_in_val = %d\n", x, tot[x]);
}
if (tot[x] % 2 == 1) ans += len;//已经求好了,在回溯了。
// cout << "for外的:" << tot[x] << endl;
// printf("for_out_x = %d for_out_val = %d\n", x, tot[x]);
}
/*
1
6
1 3 5
3 2 3
4 5 4
4 3 9
4 6 10
for_out_x = 6 for_out_val = 1
for_in_x = 4 for_in_val = 2
for_out_x = 5 for_out_val = 1
for_in_x = 4 for_in_val = 3
for_out_x = 4 for_out_val = 3
for_in_x = 3 for_in_val = 4
for_out_x = 2 for_out_val = 1
for_in_x = 3 for_in_val = 5
for_out_x = 3 for_out_val = 5
for_in_x = 1 for_in_val = 6
for_out_x = 1 for_out_val = 6
31
*/
void work()
{
ans = 0;
cnt = 0;
scanf("%d", &n);
memset(head, -1, sizeof head);
memset(tot, 0, sizeof tot);
for (int i = 1; i < n; i ++ )
{
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
add(x, y, z);
add(y, x, z);
}
dfs(1, 0, 0);
printf("%lld\n", ans);
}
int main()
{
int T; cin >> T;
while (T -- )
{
work();
}
return 0;
}
08Contest
题目要求,求有多少组存在x队排名高于y队,且y队也高于x队。
也就是,存在两场x队大于y队,或者存在两场y队大于x队。
我们单拿出第一场和第二场来看,合法的方案为第一场我的排名比你的高,但是第二场我的排名却比你i小,这不就是树状数组了嘛。
我们给第一组按升序排序,第二组随着变化,出现的逆序对,就是我们要的合法方案数喽。
//i<j,a[i]>a[j].以第一场的排名进行排序,这样排序后i,j就表示第一场排名,a[i],a[j]就表示第二场排名
#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+7;
typedef long long ll;
struct node
{
int x,y;
node(){}
node(int x,int y):x(x),y(y){}
bool operator<(const node o)const{
return x<o.x;
}
}s[maxn];
int n;
int a[maxn],b[maxn],c[maxn];
ll C[maxn];
void add(int i,int val)
{
while(i<maxn)
{
C[i]+=val;
i+=(i&(-1));
}
}
ll sum(int i)
{
ll ans=0;
while(i)
{
ans+=C[i];
i-=(i&(-i));
}
return ans;
}
ll solve()
{
memset(C,0,sizeof(C));
ll ans=0;
for(int i=n-1;i>=0;i--)
{
add(s[i].y,1);
ans+=sum(s[i].y-1);
}
return ans;
}
int main()
{
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d%d%d",&a[i],&b[i],&c[i]);
}
ll ans=0;
//a,b
for(int i=0;i<n;i++)
{
s[i]=node(a[i],b[i]);
}
sort(s,s+n);
ans+=solve();
//a,c
for(int i=0;i<n;i++)
{
s[i]=node(a[i],c[i]);
}
sort(s,s+n);
ans+=solve();
//b,c
for(int i=0;i<n;i++)
{
s[i]=node(b[i],c[i]);
}
sort(s,s+n);
ans+=solve();
printf("%lld\n",ans/2);
}