牛客练习赛69

Powered by:AB_IN 局外人

A 时间复杂度

纯模拟

for _ in range(int(input())):
    t=int(input())
    shi=t*0.5
    fen=t*6%360
    cha=abs(shi-fen)
    if cha>360-cha:
        cha=360-cha
    if cha-int(cha)>=0.5:
        cha=int(cha)+1
    else:
        cha=int(cha)
    print(cha)

B 划分

v a l ( i , j ) val(i,j) val(i,j) 就是前 i × j i\times j i×j的数的和

#include
#include
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")

using namespace std;

#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long
#define ull unsigned long long
#define rint register int
#define ld long double
#define db double
#define rep(i, l, r) for (rint i = l; i <= r; i++)
#define rep1(i,a,n) for (rint i=a;i
#define per(i, l, r) for (rint i = l; i >= r; i--)
#define per1(i,a,n) for (rint i=a;i>n;i--)
#define mset(s, _) memset(s, _, sizeof(s))
#define pb push_back
#define pii pair 
#define mp(a, b) make_pair(a, b)
#define sd(x) scanf("%d",&(x))
#define slld(x) scanf("%lld",&(x))
#define sdd(x,y) scanf("%d%d",&(x),&(y))
#define sc(s) scanf("%s",(s))
#define pd(x) printf("%d\n",(x))
#define plld(x) printf("%lld\n",(x))
#define pdk(x) printf("%d ",(x))
const int inf=0x3f3f3f3f;

namespace IO{
     
    char ibuf[1<<21],*ip=ibuf,*ip_=ibuf;
    char obuf[1<<21],*op=obuf,*op_=obuf+(1<<21);
    inline char gc(){
     
        if(ip!=ip_)return *ip++;
        ip=ibuf;ip_=ip+fread(ibuf,1,1<<21,stdin);
        return ip==ip_?EOF:*ip++;
    }
    inline void pc(char c){
     
        if(op==op_)fwrite(obuf,1,1<<21,stdout),op=obuf;
        *op++=c;
    }
    inline ll read(){
     
        register ll x=0,ch=gc(),w=1;
        for(;ch<'0'||ch>'9';ch=gc())if(ch=='-')w=-1;
        for(;ch>='0'&&ch<='9';ch=gc())x=x*10+ch-48;
        return w*x;
    }
    template<class I>
    inline void write(I x){
     
        if(x<0)pc('-'),x=-x;
        if(x>9)write(x/10);pc(x%10+'0');
    }
    class flusher_{
     
    public:
        ~flusher_(){
     if(op!=obuf)fwrite(obuf,1,op-obuf,stdout);}
    }IO_flusher;
}
using namespace IO;
const int N=1e5+10;
ll n,a[N],x,y,b[N],ans;
int main()
{
     
	n=read();
    rep(i, 1, n) a[i]=read();
    x=read();y=read();
    sort(a+1,a+1+n,greater<int>());
    rep(i, 1, n) b[i]=b[i-1]+a[i];
    rep(i, 1, x){
     
        rep(j, 1, y){
     
            ans+=b[i*j];
        }
    }
    write(ans);
	return 0;
}

C 旅行

  • 最小生成树能够保证首先是树(对于 n n n个顶点的图只有 n − 1 n-1 n1条边),其次保证任意两个顶点之间都可达,再次保证这棵树的边权值之和为最小,但不能保证任意两点之间是最短路径;( 1 − > n 1->n 1>n最后求出来的路径长度是经过n个顶点的)
  • 最短路保证从源点 S S S到目地点 D D D的路径最小(有向图中不要求终点能到起点),不保证任意两个顶点都可达;( 1 − > n 1->n 1>n最后求出来的路径长度不一定过n个顶点)
  • 最小生成树是到一群点(所有点)的路径代价和最小,是一个 n − 1 n-1 n1条边的树,最短路是从一个点到另一个点的最短路径;

这里是求最大路径,要经过 n n n个顶点

那就是最大生成树,把 c m p cmp cmp里的符号改一下就好了,变成从大到小的顺序。

#include 
#define rep(i,x,y) for (int i=(x);i<=(y);i++)
using namespace std;
typedef long long ll;
const int maxn=1e6+10;
ll n,m,u,v,w,ans,cnt;

ll fa[maxn];

struct sa{
     
    ll u,v,w;
}e[maxn];

bool cmp(struct sa x,struct sa y){
     
    return x.w>y.w;
}

ll find(ll x) {
     
    if(fa[x] == x) return x;
    return fa[x] = find(fa[x]);
}

int main()
{
     
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin>>n>>m;
    rep(i,1,m) cin>>e[i].u>>e[i].v>>e[i].w;
    sort(e+1,e+1+m,cmp);
    rep(i,1,n) fa[i]=i;
    rep(i,1,m){
     
        if(cnt==n-1) break;
        w=e[i].w; u=find(e[i].u);v=find(e[i].v);
        if(u!=v){
     
            fa[u]=v;
            ans+=w;
            cnt++;
        }
    }
    cout<<ans<<endl;
    return 0;
}

完结。

你可能感兴趣的:(ACM,比赛题解,算法)