22/7/16

1,cf Non-Coprime Partition;2,acwing 1144.连接格点;3,acwing 1146.新的开始(有细节);

4,北极通讯网络;5,理想的正方形(二维滑动窗口模型);


1,cf Non-Coprime Partition

题意:给一个数n,让你把1~n的正整数划分为两个不相交子集,使得两个子集的和的最大公因数>1;如果可以划分,输出Yes,并输出集合1的大小及元素,集合2的大小及元素;否则No;

思路:一开始直接想的就是偶数项划分在一起,奇数项划分在一起;怎么快速验证呢?打表;

 输出n为i的时候的最大公因数;

 很明显,最大公因数是递增的,一定大于1;

所以可以这样划分;

数学证明: 

 an就是n;所以(2n-1)*n 和 n*(2n+1)的最大公因数就是n;

#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define rep1(i,a,n) for(register int i=(a);i<(n);++i) 
#define rep2(i,a,n) for(register int i=(a);i<=(n);++i) 
#define per1(i,n,a) for(register int i=(n);i>(a);i--) 
#define per2(i,n,a) for(register int i=(n);i>=(a);i--)
#define quick_cin() cin.tie(0),cout.tie(0),ios::sync_with_stdio(false)
#define memset(a,i,b) memset((a),(i),sizeof (b))
#define memcpy(a,i,b) memcpy((a),(i),sizeof (b))
#define pro_q priority_queue
#define pb push_back
#define pf push_front
#define endl "\n"
#define lowbit(m) ((-m)&(m))
#define YES cout<<"YES\n"
#define NO cout<<"NO\n"
#define Yes cout<<"Yes\n"
#define No cout<<"No\n"
#define yes cout<<"yes\n"
#define no cout<<"no\n"
#define yi first
#define er second
#define INF 0x3f3f3f3f
#define tulun int e[N],ne[N],h[N],w[N],idx;
#define add2(a,b) e[idx]=b,ne[idx]=h[a],h[a]=idx++;
#define add3(a,b,c) w[idx]=c,e[idx]=b,ne[idx]=h[a],h[a]=idx++;
#define T_solve() int T;cin>>T;while(T--)solve();
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef double dob;
const int N=1e6+10;
int n;
signed main()
{
    quick_cin();
    cin>>n;
    if(n<=2)No;
    else
    {
        Yes;
        if(n&1)cout<<(n-1)/2<<" ";
        else cout<<n/2<<" ";
        rep2(i,1,n)if(!(i&1))cout<<i<<" ";
        cout<<endl;
        if(n&1)cout<<(n+1)/2<<" ";
        else cout<<n/2<<" ";
        rep2(i,1,n)if(i&1)cout<<i<<" ";
    }
    return 0;
}

2,连接格点;

 思路:对格点编号,

eg 3*3的矩阵:1 2 3

                       4 5 6

                       7 8 9

每个位置编号:坐标(x,y)编号就是m*(x-1)+y,然后遍历两次矩阵,分别加上横向边,权值是2,纵向边权值是 1;所以可以直接跑kruskal;

注意连续若干行的读入现在只能用while(~scanf())来做;

#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define rep1(i,a,n) for(register int i=(a);i<(n);++i) 
#define rep2(i,a,n) for(register int i=(a);i<=(n);++i) 
#define per1(i,n,a) for(register int i=(n);i>(a);i--) 
#define per2(i,n,a) for(register int i=(n);i>=(a);i--)
#define quick_cin() cin.tie(0),cout.tie(0),ios::sync_with_stdio(false)
#define memset(a,i,b) memset((a),(i),sizeof (b))
#define memcpy(a,i,b) memcpy((a),(i),sizeof (b))
#define pro_q priority_queue
#define pb push_back
#define pf push_front
#define endl "\n"
#define lowbit(m) ((-m)&(m))
#define YES cout<<"YES\n"
#define NO cout<<"NO\n"
#define Yes cout<<"Yes\n"
#define No cout<<"No\n"
#define yes cout<<"yes\n"
#define no cout<<"no\n"
#define yi first
#define er second
#define INF 0x3f3f3f3f
#define tulun int e[N],ne[N],h[N],w[N],idx;
#define add2(a,b) e[idx]=b,ne[idx]=h[a],h[a]=idx++;
#define add3(a,b,c) w[idx]=c,e[idx]=b,ne[idx]=h[a],h[a]=idx++;
#define T_solve() int T;cin>>T;while(T--)solve();
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef double dob;
const int N=2e6+10;
struct node 
{
    int a,b,w;
    bool operator <(const node&b)
    {
        return w<b.w;
    }
}edge[N];
int n,m;
int mp[1010][1010];
int p[N];
int find(int x){return p[x]==x?x:p[x]=find(p[x]);}
int cnt,num,ans;
void kruskal()
{
    sort(edge,edge+cnt);
    rep1(i,0,cnt)
    {
        int a=edge[i].a,b=edge[i].b,w=edge[i].w;
        if(num==n*m-1)return;
        a=find(a);
        b=find(b);
        if(a!=b)
        {
            num++;
            ans+=w;
            p[a]=b;
        }
    }
}
signed main()
{
   // quick_cin();
    cin>>n>>m;
    rep2(i,1,n*m)p[i]=i;
    rep2(i,1,n)
    {
        rep2(j,1,m)
        {
            mp[i][j]=m*(i-1)+j;
            if(j>1)
            {
                int a=mp[i][j-1],b=mp[i][j];
                edge[cnt++]={a,b,2};
            }
        }
    }
    rep2(j,1,m)
    {
        rep2(i,1,n)
        {
            if(i>1)
            {
                int a=mp[i-1][j];
                int b=mp[i][j];
                edge[cnt++]={a,b,1};
            }
        }
    }
    int x1,y1,x2,y2;
    while(~scanf("%d %d %d %d",&x1,&y1,&x2,&y2))
    {
        int a=m*(x1-1)+y1;
        int b=m*(x2-1)+y2;
        a=find(a);
        b=find(b);
        if(a!=b)
        {
            num++;
            p[a]=b;
        }
    }
    kruskal();
    cout<<ans;
    return 0;
}

3,新的开始;

思路:

首先是我做错的思路:直接先跑一个最小生成树,在加上最小的建造发电站的费用;但是这样会错在当连接费用大于建造费用的时候;而且题目也没说只可以造一个发电站;

正解:想象一个超级源点,建造发电站就是超级源点和矿井连一条边,权值为建造发电站的费用,这样就可以将建造发电站和连接矿井放在一起做;

#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define rep1(i,a,n) for(register int i=(a);i<(n);++i) 
#define rep2(i,a,n) for(register int i=(a);i<=(n);++i) 
#define per1(i,n,a) for(register int i=(n);i>(a);i--) 
#define per2(i,n,a) for(register int i=(n);i>=(a);i--)
#define quick_cin() cin.tie(0),cout.tie(0),ios::sync_with_stdio(false)
#define memset(a,i,b) memset((a),(i),sizeof (b))
#define memcpy(a,i,b) memcpy((a),(i),sizeof (b))
#define pro_q priority_queue
#define pb push_back
#define pf push_front
#define endl "\n"
#define lowbit(m) ((-m)&(m))
#define YES cout<<"YES\n"
#define NO cout<<"NO\n"
#define Yes cout<<"Yes\n"
#define No cout<<"No\n"
#define yes cout<<"yes\n"
#define no cout<<"no\n"
#define yi first
#define er second
#define INF 0x3f3f3f3f
#define tulun int e[N],ne[N],h[N],w[N],idx;
#define add2(a,b) e[idx]=b,ne[idx]=h[a],h[a]=idx++;
#define add3(a,b,c) w[idx]=c,e[idx]=b,ne[idx]=h[a],h[a]=idx++;
#define T_solve() int T;cin>>T;while(T--)solve();
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef double dob;
struct node 
{
    int a,b,w;
    bool operator <(const node&b)
    {
        return w<b.w;
    }
}edge[310*310];
int edge_num;
int n,m;
int cost[310][310];
int v[310];
int p[310*310];
int find(int x){return p[x]==x?x:p[x]=find(p[x]);}
int ans;
void kruskal()
{
   sort(edge,edge+edge_num);
   rep2(i,1,n+1)p[i]=i;
   rep1(i,0,edge_num)
   {
        int a=edge[i].a,b=edge[i].b,w=edge[i].w;
        a=find(a);
        b=find(b);
        if(a!=b)
        {
            p[a]=b;
            ans+=w;
        }
   }
}
signed main()
{
    quick_cin();
    cin>>n;
    rep2(i,1,n)
    {
        cin>>v[i];
        int a=i,b=n+1,w=v[i];
        edge[edge_num++]={a,b,w};
    }
    rep2(i,1,n)
    {
        rep2(j,1,n)
        {
            cin>>cost[i][j];
            if(j<i)
            {
                int a=i,b=j,w=cost[i][j];
                edge[edge_num++]={a,b,w};
            }
        }
    }
    kruskal();
    cout<<ans;
    return 0;
}

4,北极通讯网络;

 注意:卫星设备必须在两台及以上才能发挥实际作用;

思路:最好的就是把卫星设备贪心给最大边用,k台卫星设备可以删去k-1条边(仅在k>=2时)所以输出第n-k条边即为答案;

注意我对k=max(k,1),防止k=0的情况误判;

#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define rep1(i,a,n) for(register int i=(a);i<(n);++i) 
#define rep2(i,a,n) for(register int i=(a);i<=(n);++i) 
#define per1(i,n,a) for(register int i=(n);i>(a);i--) 
#define per2(i,n,a) for(register int i=(n);i>=(a);i--)
#define quick_cin() cin.tie(0),cout.tie(0),ios::sync_with_stdio(false)
#define memset(a,i,b) memset((a),(i),sizeof (b))
#define memcpy(a,i,b) memcpy((a),(i),sizeof (b))
#define pro_q priority_queue
#define pb push_back
#define pf push_front
#define endl "\n"
#define lowbit(m) ((-m)&(m))
#define YES cout<<"YES\n"
#define NO cout<<"NO\n"
#define Yes cout<<"Yes\n"
#define No cout<<"No\n"
#define yes cout<<"yes\n"
#define no cout<<"no\n"
#define yi first
#define er second
#define INF 0x3f3f3f3f
#define tulun int e[N],ne[N],h[N],w[N],idx;
#define add2(a,b) e[idx]=b,ne[idx]=h[a],h[a]=idx++;
#define add3(a,b,c) w[idx]=c,e[idx]=b,ne[idx]=h[a],h[a]=idx++;
#define T_solve() int T;cin>>T;while(T--)solve();
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef double dob;
const int N=500*500+10;
struct node 
{
    int a,b;
    dob w;
    bool operator <(const node&b)
    {
        return w<b.w;
    }
}edge[N];
PII ver[N];
int edge_num;
int p[N];
int find(int x){return p[x]==x?x:p[x]=find(p[x]);}
int n,k;
dob ans;
void kruskal()
{
    sort(edge,edge+edge_num);
    rep2(i,1,n)p[i]=i;
    int cnt=0;
    k=max(k,1);
    rep1(i,0,edge_num)
    {
        int a=edge[i].a,b=edge[i].b;
        dob w=edge[i].w;
        a=find(a);
        b=find(b);
        if(a!=b)
        {
            p[a]=b;
            cnt++;
        }
        if(cnt==n-k)
        {
            ans=w;
            break;
        }
    }
}
dob len(int x1,int y1,int x2,int y2)
{
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
signed main()
{
    quick_cin();
    cin>>n>>k;
    rep2(i,1,n)
    {
        int x,y;
        cin>>x>>y;
        ver[i]={x,y};
    }
    rep2(i,2,n)
    {
        rep2(j,1,i-1)
        {
            int a=i,b=j;
            int x1=ver[i].first,y1=ver[i].second;
            int x2=ver[j].first,y2=ver[j].second;
            dob w=len(x1,y1,x2,y2);
            edge[edge_num++]={a,b,w};
        }
    }
    kruskal();
    cout<<fixed<<setprecision(2)<<ans;
    return 0;
}

5,理想的正方形;

题意:

 思路:

我们先求出在行中,每个长度为n的长方形的最值,并存储到最右边的端点;

 

 大概就是这样,第一行中,每个红色的点存储的就是它左边3个长度的最值;

那我求一个3*3的正方形的最值的时候,其实也就是对列方向上这个存储最值的三个长度的矩形取个最值;如图所示;

 

 因此,就可以把二维的问题转化为两次一维的问题,先横着求一边行的最值, 在纵着对列求一遍最值;两次操作处理完后,正方形的最值就储存在它的右下角;遍历所有右下角即可得到最大值减去最小值;

#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define rep1(i,a,n) for(register int i=(a);i<(n);++i) 
#define rep2(i,a,n) for(register int i=(a);i<=(n);++i) 
#define per1(i,n,a) for(register int i=(n);i>(a);i--) 
#define per2(i,n,a) for(register int i=(n);i>=(a);i--)
#define quick_cin() cin.tie(0),cout.tie(0),ios::sync_with_stdio(false)
#define memset(a,i,b) memset((a),(i),sizeof (b))
#define memcpy(a,i,b) memcpy((a),(i),sizeof (b))
#define pro_q priority_queue
#define pb push_back
#define pf push_front
#define endl "\n"
#define lowbit(m) ((-m)&(m))
#define YES cout<<"YES\n"
#define NO cout<<"NO\n"
#define Yes cout<<"Yes\n"
#define No cout<<"No\n"
#define yes cout<<"yes\n"
#define no cout<<"no\n"
#define yi first
#define er second
#define INF 0x3f3f3f3f
#define tulun int e[N],ne[N],h[N],w[N],idx;
#define add2(a,b) e[idx]=b,ne[idx]=h[a],h[a]=idx++;
#define add3(a,b,c) w[idx]=c,e[idx]=b,ne[idx]=h[a],h[a]=idx++;
#define T_solve() int T;cin>>T;while(T--)solve();
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef double dob;
const int N=1e3+10;
int row_min[N][N],row_max[N][N];
int n,m,k;
int w[N][N];
int q[N];
void get_min(int a[],int b[],int tot)
{
    int hh=0,tt=-1;
    rep2(i,1,tot)
    {
        while(hh<=tt&&q[hh]<=i-k)hh++;
        while(hh<=tt&&a[i]<=a[q[tt]])tt--;
        q[++tt]=i;
        b[i]=a[q[hh]];
    }
}
void get_max(int a[],int b[],int tot)
{
    int hh=0,tt=-1;
    rep2(i,1,tot)
    {
        while(hh<=tt&&q[hh]<=i-k)hh++;
        while(hh<=tt&&a[i]>=a[q[tt]])tt--;
        q[++tt]=i;
        b[i]=a[q[hh]];
    }
}
int ans[N],col_min[N],col_max[N];
PII zuizhi[N][N];
signed main()
{
    quick_cin();
    cin>>n>>m>>k;
    rep2(i,1,n)
    rep2(j,1,m)cin>>w[i][j];
    rep2(i,1,n)
    {
        get_min(w[i],row_min[i],m);
        get_max(w[i],row_max[i],m);
    }
    rep2(j,k,m)
    {
        rep2(i,1,n)ans[i]=row_min[i][j];
        get_min(ans,col_min,n);
        rep2(i,1,n)ans[i]=row_max[i][j];
        get_max(ans,col_max,n);
        rep2(i,1,n)zuizhi[i][j]={col_max[i],col_min[i]};
    }
    int res=INT_MAX;
    rep2(i,k,n)
    rep2(j,k,m)
    res=min(res,zuizhi[i][j].first-zuizhi[i][j].second);
    cout<<res;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dull丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值