tj<<512

· · 个人记录

T1: AAAAAAAAAAir

题意 :

给你几个区间,和几个线段,求花费最小的钱使得线段覆盖的区间满足条件

思路:

注意到 N<20 ,暴力

c0dE:

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
int n,m;
struct node{
    int s,t,c;
}a[205];
struct NODE{
    int m,a,b,p; 
}c[205];
int now[205],ans=0x3f3f3f3f3f;
bool ck(){
    rep(i,1,m){
        rep(j,a[i].s,a[i].t)if(now[j]<a[i].c)return 0;
    }
    return 1;
}
void dfs(int x,int sum){
    if(x>m){
        if(ck())ans=min(ans,sum);
        return;
    }
    rep(i,c[x].a,c[x].b){
        now[i]+=c[x].p;
    }
    dfs(x+1,sum+c[x].m);
    rep(i,c[x].a,c[x].b){
        now[i]-=c[x].p;
    }
    dfs(x+1,sum);
}
int main(){
    cin.tie(0);
    cin>>n>>m;
    rep(i,1,n)cin>>a[i].s>>a[i].t>>a[i].c;
    rep(i,1,m)cin>>c[i].a>>c[i].b>>c[i].p>>c[i].m;
    dfs(1,0);
    cout << ans;
    return 0;
} 

T2:

题意 :

求任意线段重叠后的联通块数量。

思路:

对所有线段的左端点排序,则我们只要考虑右端点。

考虑一条线段 i 作为一个块的最左边的贡献

假设有 x 个线段在 l_i r_i 这段区间内,则这些线段对当下答案的贡献为0,那么有 n-x 条线段在区间外这些线段可以任意挑选,就贡献了2^{n-x}

用树状数组维护当下的联通块的右端点。

c0dE:

#include<bits/stdc++.h>
using namespace std;
#define I_love return
#define in(a) a=read()
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define deap(i,a,b) for(int i=b;i>=a;i--)
#define pii pair<int,int>
#define mk(a,b) make_pair(a,b)
#define fi first
#define se second
#define pd(a) push_back(a)
#define Firefly !1;
//#define int long long
const int N = 1e5 + 5;
const int inf = INT_MAX;
const int M=1e9+7; 
inline int read(){
    int ch=getchar(),f=1,x=0;
    while('9'<ch||ch<'0'){
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while('0'<=ch&&ch<='9'){
        x=x*10+ch-'0';
        ch=getchar();
    }
   return x*f;
}
void fast() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}
struct node {
    int l,r;
}a[N];;
int tr[N<<1];
int n;
int mr=-1;
int p2w[N];
int lowbit(int x){
    return x&-x;
}
void add(int x,int v){
    while(x<=mr)tr[x]+=v,x+=lowbit(x);
}
int sum(int x){
    int p=0;
    while(x){
        p+=tr[x];
        x-=lowbit(x);
    }
    return p;
}
bool m28(node a,node b){
    return a.l<b.l; 
}
long long ans;
signed main(){
    fast();
    cin>>n;
    rep(i,1,n){
        cin>>a[i].l>>a[i].r;
        mr=max(mr,a[i].r);
    }
    sort(a+1,a+n+1,m28);
    p2w[0]=1;
    rep(i,1,n)p2w[i]=p2w[i-1]*2%M;
    rep(i,1,n){
        int open=(sum(mr)-sum(a[i].l-1))+1;
        ans+=p2w[n-open];
        ans%=M;
        add(a[i].r,1);
    }
    cout<<ans;
    I_love Firefly;
}


T3:

题意 :

有78只奶牛,每个奶牛有仰慕者,同一只奶牛被仰慕的奶牛喜欢的颜色相同,求颜色种类最大化的分配方案

思路:

启发式合并 and 查并集吧

考虑一对仰慕关系 a b

这里 b 是 a 的仰慕者

我们找到颜色是 color_a 的集合 S ,那么 S 内的元素的仰慕者的颜色也应该相同。

合并 S 与 颜色是 color_b的集合即可。

注意到合并一次会对仰慕者的仰慕者集合产生影响,要一直合并,最多 n-1 次。

c0dE:

#include<bits/stdc++.h>
using namespace std;
#define I_love return
#define in(a) a=read()
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define deap(i,a,b) for(int i=b;i>=a;i--)
#define pii pair<int,int>
#define mk(a,b) make_pair(a,b)
#define fi first
#define se second
#define pd(a) push_back(a)
#define Firefly !1;
//#define int long long
const int N =2e5 + 5;
const int inf = INT_MAX;
inline int read(){
    int ch=getchar(),f=1,x=0;
    while('9'<ch||ch<'0'){
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while('0'<=ch&&ch<='9'){
        x=x*10+ch-'0';
        ch=getchar();
    }
   return x*f;
}
void fast() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}
int n,m;
int fa[N],sz[N],lst[N],col[N];
int find(int x){
    if(fa[x]!=x)return fa[x]=find(fa[x]);
    return x;
}
int icebin;
void me(int x,int  y){
    if(x==0||y==0)return ;
    x=find(x),y=find(y);
    if(x==y)return;
    if(sz[x]<sz[y])swap(x,y);
    fa[y]=x,sz[x]+=sz[y],sz[y]=0;
    me(lst[x],lst[y]);
    if(lst[x]==0)lst[x]=lst[y];
}
signed main(){
    fast();
    cin>>n>>m;
    rep(i,1,n)fa[i]=i,sz[i]=1;
    rep(i,1,m){
        int l,r;
        cin>>l>>r;
        l=find(l),r=find(r);
        if(lst[l])me(r,lst[l]);
        lst[l]=r;
    }
    rep(i,1,n){
        int p=find(i);
        if(col[p]==0)col[p]=++icebin;
        cout<<col[p]<<'\n';
    }
    I_love Firefly;
}


T4:

题意 :

求把一串字符改为合法的最小花费。

思路:

pd

显然我们可以by floyd 求出一个字符转成其他字符的最小cost

那方程很显然了

dp_i=min(dp_j+cost(j+1,i,col)) 然后前缀和求颜色最小值,答案边为 $min(dp_j+c_i-c_j)$找 $dp_j-c_j$的最小值即可 ### c0dE: ``` #include<bits/stdc++.h> using namespace std; #define I_love return #define in(a) a=read() #define rep(i,a,b) for(int i=a;i<=b;i++) #define deap(i,a,b) for(int i=b;i>=a;i--) #define pii pair<int,int> #define mk(a,b) make_pair(a,b) #define fi first #define se second #define pd(a) push_back(a) #define Firefly !1; //#define int long long const int N = 1e1 + 5; const int inf = INT_MAX; inline int read(){ int ch=getchar(),f=1,x=0; while('9'<ch||ch<'0'){ if(ch=='-')f=-1; ch=getchar(); } while('0'<=ch&&ch<='9'){ x=x*10+ch-'0'; ch=getchar(); } return x*f; } void fast() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); } string s; int n,m,k; int step[101][101]; int dp[101],ans[100001]; int pre[100001][101]; signed main(){ fast(); cin>>n>>m>>k>>s; rep(i,1,m)rep(j,1,m)cin>>step[i][j]; rep(k,1,m)rep(i,1,m)rep(j,1,m)if(step[i][j]>step[i][k]+step[k][j])step[i][j]=step[i][k]+step[k][j]; rep(i,1,n)rep(j,1,m)pre[i][j]=pre[i-1][j]+step[s[i-1]-'a'+1][j]; memset(dp,0x3f,sizeof dp); memset(ans,0x3f,sizeof ans); ans[0]=0; rep(i,k,n)rep(j,1,m)dp[j]=min(dp[j]+step[s[i-1]-'a'+1][j],ans[i-k]+pre[i][j]-pre[i-k][j]),ans[i]=min(ans[i],dp[j]); cout<<ans[n]; I_love Firefly; } ``` ## T5: ### 题意 : 求区间 $l$ $r$ 中的满足 $a_i+a_j+a_k=0$的数量 ### 思路: 区间pd 设 $dp_{ij}$表示 $i$ $j$区间内的答案; 显然 $dp_{ij}=dp_{i-1{j}}+dp_{i{j-1}}-dp_{{i-1}{j-1}}

开桶处理即可

c0dE:

#include<bits/stdc++.h>
using namespace std;
#define I_love return
#define in(a) a=read()
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define deap(i,a,b) for(int i=b;i>=a;i--)
#define pii pair<int,int>
#define mk(a,b) make_pair(a,b)
#define fi first
#define se second
#define pd(a) push_back(a)
#define Firefly !1;
#define int long long
const int N = 1e1 + 5;
const int inf = INT_MAX;
inline int read(){
    int ch=getchar(),f=1,x=0;
    while('9'<ch||ch<'0'){
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while('0'<=ch&&ch<='9'){
        x=x*10+ch-'0';
        ch=getchar();
    }
   return x*f;
}
void fast() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}
int n,q,a[5005],cnt[4000005];
int dp[5005][5005];
signed main(){
    fast();
    int n,q;
    cin>>n>>q;
    rep(i,1,n)cin>>a[i];
    rep(i,1,n){
        rep(j,i+1,n){
            dp[i][j]=cnt[0-a[i]-a[j]+2000000];
            cnt[a[j]+2000000]++;
        }
        rep(j,i+1,n)cnt[a[j]+2000000]=0;
    }
    rep(l,3,n){
        rep(i,1,n-l+1){
            int j=i+l-1;
            dp[i][j]+=dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1]; 
        }
    }
    while(q--){
        int l,r;
        cin>>l>>r;
        cout<<dp[l][r]<<'\n';
    }
    I_love Firefly;
}