2018icpc南京

A题 莽结论,注意特判0

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int N=1e5+10;
int main(){
    ll n,k;
    cin>>n>>k;
    if(n==0){
        cout<<"Austin"<<endl;
        return 0;
    }
    if(k==1){
        if(n%2==1)cout<<"Adrien"<<endl;
        else cout<<"Austin"<<endl;
    }else cout<<"Adrien"<<endl;
    return 0;
}
View Code

D题 最小球覆盖,使用模板或者模拟退火算法

#include<bits/stdc++.h>
using namespace std;
#pragma warning(disable:4996)
const double eps = 1e-8;
struct point3D
{
    double x, y, z;
} p[105];
int n;
double dis(point3D a, point3D b)
{
    return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y) + (a.z - b.z)*(a.z - b.z));
}
double solve()
{
    double step = 10000, ans = 1e30, mt;
    point3D z;
    z.x = z.y = z.z = 0;
    int s = 0;
    while (step > eps)
    {
        for (int i = 0; i < n; i++)
            if (dis(z, p[s]) < dis(z, p[i]))
                s = i;
        mt = dis(z, p[s]);
        ans = min(ans, mt);
        z.x += (p[s].x - z.x) / mt * step;
        z.y += (p[s].y - z.y) / mt * step;
        z.z += (p[s].z - z.z) / mt * step;
        step *= 0.98;
    }
    return ans;
}
int main()
{
    double ans;
    while (scanf("%d", &n) != EOF)
    {
        for (int i = 0; i < n; i++)
            scanf("%lf%lf%lf", &p[i].x, &p[i].y, &p[i].z);
        ans = solve();
        printf("%.7f\n", ans);
    }
    return 0;
}
View Code

I 最大流

#include<bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f;
const int N=1e4+10,M=2e5+10;
int h[N],ne[M],e[M],w[M],idx;
int n,m,S,T,E;
int d[N],f[M],cur[N];
int k;
void add(int a,int b,int c){
    e[idx]=b,ne[idx]=h[a],f[idx]=c,h[a]=idx++;
    e[idx]=a,ne[idx]=h[b],f[idx]=0,h[b]=idx++;
}
int bfs(){
    memset(d,-1,sizeof d);
    d[S]=0;
    cur[S]=h[S];
    queue<int> q;
    q.push(S);
    while(q.size()){
        int t=q.front();
        q.pop();
        for(int i=h[t];i!=-1;i=ne[i]){
            int j=e[i];
            if(d[j]==-1&&f[i]){
                cur[j]=h[j];
                d[j]=d[t]+1;
                if(j==T)
                return true;
                q.push(j);
            }
        }
    }
    return false;
}
int find(int u,int limit){
    if(u==T){
        return limit;
    }
    int i;
    int flow=0;
    for(i=cur[u];i!=-1&&flow<limit;i=ne[i]){
        cur[u]=i;
        int j=e[i];
        if(d[j]==d[u]+1&&f[i]){
            int t=find(j,min(f[i],limit-flow));
            if(!t)
            d[j]=-1;
            else{
                f[i]-=t;
                f[i^1]+=t;
                flow+=t;
            }
 
        }
    }
    return flow;
}
int dinic(){
    int flow;
    int r=0;
    while(bfs()){
        while(flow=find(S,inf))
            r+=flow;
    }
    return r;
}
int main(){
    memset(h,-1,sizeof h);
    int i;
    cin>>n>>m>>k;
    S=0,T=n+m+1,E=n+m+2;
    for(int i=1;i<=n;i++){
        add(0,i,1);
        add(E,i,1);
    }
    add(0,E,k);
    for(int i=1;i<=n;i++){
        int t;
        cin>>t;
        for(int j=1;j<=t;j++){
            int x;
            cin>>x;
            add(i,n+x,1);
        }
    }
    for(int i=1;i<=m;i++){
        add(n+i,T,1);
    }
    cout<<dinic()<<endl;
}
View Code

J 对于每一位算区间贡献

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int N=1e6+10;
ll a[N][2];//a[n][0]->pre  a[n][1]->new
ll vis[N];
ll p[N];
int cnt;
void init(){
    int i;
    vis[1]=1;
    a[1][0]=-1;
    for(i=2;i<=1e6;i++){
        if(!vis[i]){
            a[i][0]=-1;
            a[i][1]=i;
            p[++cnt]=i;
        }
        for(int j=1;j<=cnt&&p[j]*i<=1000000;j++){
            vis[p[j]*i]=1;
            a[p[j]*i][0]=i;
            a[p[j]*i][1]=0;
            if(i%p[j]==0)break;
            a[p[j]*i][0]=i;
            a[p[j]*i][1]=p[j];
        }
    }
}
ll g[N];
int main(){
    init();
    int n;scanf("%d",&n);
    ll sum=0;
    for(ll i=1;i<=n;i++){
        int x;    scanf("%d",&x);
        do{
            int t=a[x][1];
            if(t!=0)
            sum+=(i-g[t])*(n-i+1);
            g[t]=i;
            x=a[x][0];
            if(x==-1)break;
        }while(1);
    }
    cout<<sum<<endl;
    return 0;
}
View Code

K 随机化算法

#include<bits/stdc++.h>
using namespace std;
int main(void)
{
    int n;
    srand(time(0));
    n = 50000;
    string s;
    char a[5]= "UDRL";
    while(n--) s += a[rand()%4];
    cout<<s<<endl;
   return 0;
}
View Code

 

posted @ 2020-10-19 22:01  朝暮不思  阅读(205)  评论(0编辑  收藏  举报