10月21日联考题解
sword7level · · 个人记录
T1\ tri
题目是说给定一个正奇数
三角形的贡献肯定是有很多不同情况的,首先我们考虑如何分类,我们可以发现使用原图形在三角形中相同边数的情况是可以一起统计的,我们就按使用原图中的边数来分类统计三角形的个数。
首先使用
然后是使用一条原图边的三角形的个数,我们不难发现有两种情况,第一种是相邻两个顶点射出的边和它们之间的原图边组成的三角形,第二种是当前顶点射出的边以及对边和对边某顶点射出的边组成的三角形,由于是封闭图形,最后一个顶点和第一个顶点相邻,我们设
我们发现只有
我们直接组合数计算线性求解即可。
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e6+10,mod=1e9+7;
int n;
ll a[N],frac[N];
inline ll qpow(ll a,ll b){
ll res=1,base=a;
while(b){
if(b&1) res*=base,res%=mod;
base*=base;
base%=mod;
b>>=1;
}
return res;
}
inline ll C(ll m,ll n){
if(n<0 || n>m) return 0;
return frac[m]*qpow(frac[n],mod-2)%mod*qpow(frac[m-n],mod-2)%mod;
}
inline int calc(int x){
if(x<=n/2) return x+n/2;
else return ((x-n/2-1)==0?n:(x-n/2-1));
}
signed main(){
cin.tie(0);cout.tie(0);
ios::sync_with_stdio(false);
freopen("tri.in","r",stdin);
freopen("tri.out","w",stdout);
cin>>n;
ll tot=0;
for(int i=1;i<=n;i++) cin>>a[i],tot+=a[i],tot%=mod;
ll ans=(tot-2)*(tot-1)%mod*tot%mod*qpow(6,mod-2)%mod;
if(tot<3) ans=0;
//cout<<ans<<'\n';
frac[0]=1;
for(int i=1;i<=N-10;i++) frac[i]=(frac[i-1]*i)%mod;
a[n+1]=a[1];
for(int i=1;i<=n;i++){
(ans+=a[i]*a[i+1])%=mod;
(ans+=C(a[i],2))%=mod;
ans=(ans-C(a[i],3)+mod)%mod;
(ans+=a[i]*(a[calc(i)]+a[calc(i)+1])%mod)%=mod;
}
if(n==3) (ans+=2*tot+1)%=mod;
cout<<ans;
return 0;
}
T2\ path
题目是说给定一个
发现必然存在一组最优解中有一条边是在
考虑枚举那条边,然后从
题目来源(超级弱化版):
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int long long
const int N = 2000 + 30;
const ll INF = 0x3f3f3f3f3f3f3f;
int n, m, q;
struct A {
ll to, b, e, c;
};
struct C {
int s, t, b, e, c;
} edge[N];
vector<A> G[N], G2[N];
ll dis[N][N], dis2[N][N];
struct B {
ll x, d;
bool operator<(const B &rhs) const { return d > rhs.d; }
};
priority_queue<B> pq;
bool vis[N];
void solve(int id, int s, ll dd) {
memset(vis, 0, sizeof vis);
dis[s][id] = 0;
pq.push({ s, 0 });
while (!pq.empty()) {
int x = pq.top().x, d = pq.top().d;
pq.pop();
if (dis[x][id] < d)
continue;
vis[x] = 1;
for (auto edge : G[x]) {
ll to = edge.to, t = edge.c, b = edge.b, e = edge.e;
if (vis[to] || dis[x][id] + dd + t > e || b + t > e)
continue;
ll et = max(dis[x][id] + dd, b) + t;
if (et - dd < dis[to][id]) {
dis[to][id] = et - dd;
pq.push({ to, dis[to][id] });
}
}
}
}
void solve2(int id, int s, ll dd) {
memset(vis, 0, sizeof vis);
dis2[s][id] = 0;
pq.push({ s, 0 });
while (!pq.empty()) {
int x = pq.top().x, d = pq.top().d;
pq.pop();
if (dis2[x][id] < d)
continue;
vis[x] = 1;
for (auto edge : G2[x]) {
ll to = edge.to, t = edge.c, b = edge.b, e = edge.e;
if (vis[to] || dd - dis2[x][id] - t < b || b + t > e)
continue;
ll et = min(dd - dis2[x][id], e) - t;
if (dd - et < dis2[to][id]) {
dis2[to][id] = dd - et;
pq.push({ to, dis2[to][id] });
}
}
}
}
signed main() {
freopen("path.in", "r", stdin);
freopen("path.out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> q;
for (int i = 1; i <= m; i++) {
ll s, t, b, e, c;
cin >> s >> t >> b >> e >> c;
edge[i] = { s, t, b, e, c };
G[s].push_back({ t, b, e, c });
G2[t].push_back({ s, b, e, c });
}
memset(dis, 0x3f, sizeof dis);
memset(dis2, 0x3f, sizeof dis2);
for (int i = 1; i <= m; i++) {
int s = edge[i].s, t = edge[i].t, e = edge[i].e, c = edge[i].c;
solve(i, t, e);
solve2(i, s, e - c);
}
for (int i = 1, u, v; i <= q; i++) {
cin >> u >> v;
ll ans = INF;
for (int j = 1; j <= m; j++) {
int s = edge[j].s, t = edge[j].t, e = edge[j].e, c = edge[j].c;
ans = min(ans, dis[v][j] + dis2[u][j] + c);
}
if (ans >= INF / 2) cout << -1 << '\n';
else cout << ans << '\n';
}
return 0;
}
T3\ esc
题目是说有
首先我们考虑机器人不能跳过某个洞,那么很显然每个机器人只能从它坐标的相邻两个洞离开。我们可以先把只有一边有洞的机器人剔除,因为它们只有一种贡献。对于两边都有洞的机器人,设向左移出的状态为
这里需要注意,不能把两个机器人的限制静态地写成“若
对每个机器人
但同一个方案可能对应多条路径,例如全走左时,路径可以先向右走一段再向上,也可以一直向右走。为了得到一一对应,我们取规范折线:在所有能分开该方案的单调路径中,取最靠左上的一条,也就是取所有走右点的左上包络。这样得到的规范折线由方案唯一确定。反过来,任意一条规范折线也唯一确定一种合法选择:折线左上方机器人走左,右下方机器人走右,折线上的点走右。因此合法方案与规范折线一一对应。
规范折线经过的点按
因此,把所有去重后的二元组按
设排序后的
其中前面的
前面的
再把
题目来源:
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e5+10,mod=1e9+7;
int n,m,a[N],b[N],maxn=0;
ll dp[N];
pair<int,int> dot[N];
struct BIT{
ll tree[N];
inline int lowbit(int x){
return x&(-x);
}
inline void add(int x,ll k){
while(x<=maxn){
(tree[x]+=k)%=mod;
x+=lowbit(x);
}
}
inline ll query(int x){
ll res=0;
while(x){
(res+=tree[x])%=mod;
x-=lowbit(x);
}
return res;
}
}tree;
vector<int> x,y;
inline bool cmp(pair<int,int> x,pair<int,int> y){
if(x.first==y.first) return x.second>y.second;
return x.first<y.first;
}
signed main(){
cin.tie(0);cout.tie(0);
ios::sync_with_stdio(false);
freopen("esc.in","r",stdin);
freopen("esc.out","w",stdout);
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<=m;i++) cin>>b[i];
int cnt=0;
for(int i=1;i<=n;i++){
if(a[i]<b[1] || a[i]>b[m]) continue;
int pos=lower_bound(b+1,b+1+m,a[i])-b;
dot[++cnt]={a[i]-b[pos-1],b[pos]-a[i]};
x.push_back(dot[cnt].first);
y.push_back(dot[cnt].second);
}
sort(x.begin(),x.end());sort(y.begin(),y.end());
x.erase(unique(x.begin(),x.end()),x.end());
y.erase(unique(y.begin(),y.end()),y.end());
for(int i=1;i<=cnt;i++){
dot[i].first=lower_bound(x.begin(),x.end(),dot[i].first)-x.begin()+1;
dot[i].second=lower_bound(y.begin(),y.end(),dot[i].second)-y.begin()+1;
}
maxn=y.size();
sort(dot+1,dot+1+cnt,cmp);
cnt=unique(dot+1,dot+1+cnt)-dot-1;
ll ans=1;
for(int i=1;i<=cnt;i++){
ll add=tree.query(dot[i].second-1)+1;
(ans+=add)%=mod;
tree.add(dot[i].second,add);
}
cout<<ans;
return 0;
}