ICPC Asia HongKong 2017

南京已成炮灰,徐州加油!

A:java二分

import java.math.*;
import java.util.*;
public class Main {
	public static void main(String [] args){
		Scanner cin=new Scanner(System.in);
		int t,n;
		BigInteger z,zn,znjyn,ans,tans,x,X=BigInteger.ONE,Y=BigInteger.valueOf(2);
		t=cin.nextInt();
		while(t-->0){
			n=cin.nextInt();
			z=cin.nextBigInteger();
			zn=poww(z,n);
			ans=BigInteger.valueOf(-1);
			for(BigInteger y=find(z.divide(BigInteger.valueOf(2)).compareTo(BigInteger.valueOf(2))>0?z.divide(BigInteger.valueOf(2)):BigInteger.valueOf(2),z.subtract(BigInteger.ONE),zn.divide(BigInteger.valueOf(2)),n).add(BigInteger.ONE);y.compareTo(z)<0;y=y.add(BigInteger.ONE)){
				if(z.subtract(y).compareTo(y.subtract(BigInteger.ONE))>0)continue;
				znjyn=zn.subtract(poww(y,n));
				x=find(z.subtract(y),y.subtract(BigInteger.ONE),znjyn,n);
				tans=znjyn.subtract(poww(x,n));
				if(ans.compareTo(BigInteger.valueOf(-1))==0||ans.compareTo(tans)>0){
					ans=tans;
					X=x;
					Y=y;
				}
				if(x.compareTo(y.subtract(BigInteger.ONE))==0)continue;
				tans=poww(x.add(BigInteger.ONE),n).subtract(znjyn);
				if(ans.compareTo(BigInteger.valueOf(-1))==0||ans.compareTo(tans)>0){
					ans=tans;
					X=x.add(BigInteger.ONE);
					Y=y;
				}
			}
			System.out.println(X+" "+Y+" "+ans);
		}
	}
	static BigInteger poww(BigInteger a,int x){
		BigInteger ans=BigInteger.ONE;
		while(x>0){
			if((x&1)>0)ans=ans.multiply(a);
			a=a.multiply(a);
			x>>=1;
		}
		return ans;
	}
	static BigInteger find(BigInteger l,BigInteger r,BigInteger x,int n){
		BigInteger mid,ans=l;
		l=l.add(BigInteger.ONE);
		while(l.compareTo(r)<=0){
			mid=(l.add(r)).shiftRight(1);
			if(poww(mid,n).compareTo(x)<=0){
				ans=mid;
				l=mid.add(BigInteger.ONE);
			}
			else r=mid.subtract(BigInteger.ONE);
		}
		return ans;
	}
}

B:線段樹掃描線

#include <bits/stdc++.h>
using namespace std;
const int N = 10100;
struct SEG {
    int l, r, h;
    int op;
    bool operator <(const SEG &b) const {
        return h < b.h;
    }
}a[N * 2];
int len;
struct node {
    int l, r;
    int laz, sum, len;
}tree[N << 2];
int n, m;
void build(int l, int r, int cur) {
    tree[cur].l = l;
    tree[cur].r = r;
    tree[cur].laz = 0;
    tree[cur].len = r - l + 1;
    tree[cur].sum = 0;
    if(l == r) return;
    int mid = (l + r) >> 1;
    build(l, mid, cur << 1);
    build(mid + 1, r, cur << 1 | 1);
}
void pushdown(int cur) {
    if(tree[cur].laz) {
        tree[cur << 1].laz ^= 1;
        tree[cur << 1 | 1].laz ^= 1;
        tree[cur << 1].sum = tree[cur << 1].len - tree[cur << 1].sum;
        tree[cur << 1 | 1].sum = tree[cur << 1 | 1].len - tree[cur << 1 | 1].sum;
        tree[cur].laz = 0;
    }
}
void update(int pl, int pr, int cur) {
    if(pl <= tree[cur].l && tree[cur].r <= pr) {
        tree[cur].laz ^= 1;
        tree[cur].sum = tree[cur].len - tree[cur].sum;
        return;
    }
    pushdown(cur);
    if(pl <= tree[cur << 1].r) update(pl, pr, cur << 1);
    if(pr >= tree[cur << 1 | 1].l) update(pl, pr, cur << 1 | 1);
    tree[cur].sum = tree[cur << 1].sum + tree[cur << 1 | 1].sum;
}
int main() {
    int T;
    int ans;
    int x1, y1, x2, y2;
    scanf("%d", &T);
    while(T--) {
        scanf("%d %d", &n, &m);
        build(1, n, 1);
        len = 0;
        for(int i = 1; i <= m; i++) {
            scanf("%d %d %d %d", &x1, &x2, &y1, &y2);
            len++;
            a[len].l = x1, a[len].r = x2, a[len].h = y1, a[len].op = 1;
            len++;
            a[len].l = x1, a[len].r = x2, a[len].h = y2 + 1, a[len].op = -1;
        }
        sort(a + 1, a + len + 1);
        ans = 0;
        a[len + 1].h = n + 1;
        for(int i = 1, k; i <= len; ) {
            if(a[i].h > n) break;
            k = i;
            for(;k <= len && a[k].h == a[i].h; k++) {
                update(a[k].l, a[k].r, 1);
            }

            ans += tree[1].sum * (a[k].h - a[i].h);
            i = k;
        }
        printf("%d\n", ans);
    }
    return 0;
}

D:最短路

#include <bits/stdc++.h>
using namespace std;
const int N = 55;
#define INF 0x3f3f3f3f
struct node {
    int to, nex, d;
}e[N * N];
int sx, ex;
int n;
int len;
int head[N], dis[N], vis[N];
void add(int x, int y, int z) {
    e[len].to = y;
    e[len].nex = head[x];
    e[len].d = z;
    head[x] = len++;
}
struct node1 {
    int to, d;
    node1(int to_ = 0, int d_ = 0) {
        to = to_;
        d = d_;
    }
    bool operator <(const node1 &b)const {
        return d > b.d;
    }
};
void DIJ() {
    memset(dis, INF, sizeof(dis));
    memset(vis, 0, sizeof(vis));
    priority_queue<node1> q;
    node1 now;
    dis[sx] = 0;
    int to;
    q.push(node1(sx, 0));
    while(!q.empty()) {
        now = q.top();
        q.pop();
        if(vis[now.to]) continue;
        if(now.to == ex) {
            printf("%d\n", dis[ex]);
            return;
        }
        vis[now.to] = 1;
        for(int i = head[now.to]; i != -1; i = e[i].nex) {
           to = e[i].to;
           if(vis[to] == 0 && dis[to] > dis[now.to] + e[i].d) {
            dis[to] = dis[now.to] + e[i].d;
            q.push(node1(to, dis[to]));
           }
        }
    }
}
int main() {
    int x, y, z;
    while(~scanf("%d", &n)) {
        scanf("%d %d", &sx, &ex);
        memset(head, -1, sizeof(head));
        scanf("%d", &x);
        while(x) {
            scanf("%d %d", &y, &z);
            add(x, y, z);
            add(y, x, z);
            scanf("%d", &x);
        }
        DIJ();
    }

    return 0;
}

E:二分

#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
const int N = 100100;
int a[N], n, m;
bool judge(int x) {
    int pre = a[1], num = 1;
    for(int i = 2; i <= n; i++) {
        if(a[i] >= pre + x) {
            num++;
            pre = a[i];
        }
    }
    return num >= m;
}
int main() {
    while(~scanf("%d %d", &n, &m) && (n || m)) {
        for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
        sort(a + 1, a + 1 + n);
        int mid, ans, l = 1, r = 1000000;
        while(l <= r) {
            mid = (l + r) >> 1;
            //cout<< mid << endl;
            if(!judge(mid)) {
                r = mid - 1;
            } else {
                ans = mid;
                l = mid + 1;
            }
        }
        printf("%d\n", ans);
    }

    return 0;
}

F:計算幾何暴力

#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
const int N = 100100;
struct point {
    double x, y;
}a[N], b[N];
int n, m;
int main() {
    char op;
    double r;
    double x;
    int ans;
    while(~scanf("%d %d", &n, &m) && (n || m)) {
        for(int i = 1; i <= n; i++) {
            while((op = getchar()) != '(');
            scanf("%lf,%lf", &a[i].x, &a[i].y);
        }
        for(int i = 1; i <= m; i++) {
            while((op = getchar()) != '(');
            scanf("%lf,%lf", &b[i].x, &b[i].y);
        }
        while((op = getchar()) != '\n');
        for(int i = 1; i <= m; i++) {
            scanf("%lf", &x);
            ans = 0;
            for(int j = 1; j <= n; j++) {
                if((b[i].x - a[j].x) * (b[i].x - a[j].x) + (b[i].y - a[j].y) * (b[i].y - a[j].y) - eps <= x * x) ans++;

            }
            printf("%d%c", ans, " \n"[i == m]);
        }
    }

    return 0;
}

G:揹包

#include<cstdio>
#include<algorithm>
using namespace std;
const int N=4e3+11;
int n,w,v[15];
struct Node{
    int num,an[15];
    bool operator <(const Node &n1)const{
        if(num!=n1.num) return num<n1.num;
        for(int i=0;i<n;i++) if(an[i]!=n1.an[i]) an[i]>n1.an[i];
        return true;
    }
}dp[N],temp;
int main(){
    while(~scanf("%d%d",&w,&n)){
        for(int i=0;i<n;i++) scanf("%d",&v[i]);
        for(int i=0;i<=w;i++){
            dp[i].num=-1;
            for(int j=0;j<n;j++) dp[i].an[j]=0;
        }
        dp[0].num=0;
        for(int i=0;i<n;i++){
            for(int j=v[i];j<=w;j++){
                if(dp[j-v[i]].num==-1) continue;
                temp=dp[j-v[i]];temp.num++;temp.an[i]++;
                if(dp[j].num==-1) dp[j]=temp;
                else dp[j]=min(dp[j],temp);
            }
        }
        if(dp[w].num==-1) printf("-1\n");
        else for(int i=0;i<n;i++) printf("%d%c",dp[w].an[i]," \n"[i==n-1]);
    }
    return 0;
}

H:等比數列前n項和  Si - 3爲等比數列   Si爲第i個集合的大小


import java.math.BigInteger;
import java.util.Scanner;
public class Main {
	public static void main(String args[]){
		BigInteger n,k,m,cf2,ans,res,two=BigInteger.valueOf(2),thr=BigInteger.valueOf(3);
		Scanner sc=new Scanner(System.in);
		while(sc.hasNext()){
			n=sc.nextBigInteger();
			k=sc.nextBigInteger();
			if(n.compareTo(BigInteger.ONE)==0){
				if(k.compareTo(BigInteger.ONE)==0) System.out.println("1");
				else System.out.println("-1");
			}else if(n.compareTo(two)==0){
				if(k.compareTo(thr)<=0) System.out.println(k);
				else System.out.println("-1");
			}else if(n.compareTo(thr)==0){
				if(k.mod(thr)==BigInteger.ZERO) ans=poww(two,k.divide(thr)).add(BigInteger.ONE);
				else ans=poww(two,k.divide(thr).add(BigInteger.ONE)).add(k.mod(thr)).subtract(two);
				System.out.println(ans);
			}else{
				if(k.compareTo(n)<=0){
					System.out.println(k);
					continue;
				}
				m=BigInteger.ONE;cf2=two;
				while(true){
					res=n.subtract(thr).multiply(cf2.subtract(BigInteger.ONE)).add(m.multiply(thr));
					if(res.compareTo(k)>0) break;
					m=m.add(BigInteger.ONE);
					cf2=cf2.multiply(two);
				}
				cf2=cf2.divide(two);
				m=m.subtract(BigInteger.ONE);
				res=n.subtract(thr).multiply(cf2.subtract(BigInteger.ONE)).add(m.multiply(thr));
			//	System.out.println(m+" "+res);
				if(res.compareTo(k)==0){
					cf2=cf2.divide(two);
					m=m.subtract(BigInteger.ONE);
					res=n.subtract(thr).multiply(cf2.subtract(BigInteger.ONE)).add(m.multiply(thr));
					m=m.add(BigInteger.ONE);
				}else m=m.add(BigInteger.ONE);
				//System.out.println(m+" "+res);
				ans=poww(two,m).add(k.subtract(res)).subtract(two);
				System.out.println(ans);
			}
		}
	}
	static BigInteger poww(BigInteger x,BigInteger y){
		BigInteger ans=BigInteger.ONE;
		while(y.compareTo(BigInteger.ZERO)>0){
			if(y.and(BigInteger.ONE).compareTo(BigInteger.ONE)==0) ans=ans.multiply(x);
			x=x.multiply(x);
			y=y.shiftRight(1);
		}
		return ans;
	}
}

I:記憶化搜索

import java.util.*;
import java.math.*;
public class Main {
	static public Map<BigInteger,BigInteger> mp=new HashMap<BigInteger,BigInteger>(); 
	public static void main(String [] args){
		Scanner cin=new Scanner(System.in);
		BigInteger n;
		while(cin.hasNext()){
			n=cin.nextBigInteger();
			n=n.add(BigInteger.ONE);
			System.out.println(cal(n));
		}
	}
	static BigInteger biu(BigInteger a,BigInteger b){
		return (a.add(b)).multiply(b.subtract(a).add(BigInteger.ONE)).divide(BigInteger.valueOf(2));
	}
	static BigInteger cal(BigInteger n){
		//System.out.println(n+" "+now);//
		if(n.compareTo(BigInteger.valueOf(2))<=0)return BigInteger.ZERO;
		//System.out.println("*"+n);
		if(mp.containsKey(n))return mp.get(n);
		//System.out.println("#"+n);
		int len=n.bitLength();
		if(n.compareTo((BigInteger.ONE).shiftLeft(len-1))==0)len--;
		BigInteger now=(BigInteger.ONE).shiftLeft(len-1);
		//while(now.compareTo(n)>=0)now=now.shiftRight(1);
		if(n.compareTo(now.shiftLeft(1))==0){
			mp.put(n,(cal(n.shiftRight(1)).multiply(BigInteger.valueOf(3))).add(biu(BigInteger.ONE,now.subtract(BigInteger.ONE))));
			return mp.get(n);
		}
		else{
			BigInteger a1,a2,a3,a;
			a1=cal(n.subtract(now)).multiply(BigInteger.valueOf(2));
			a2=cal(now);
			a3=biu(now.add(now).subtract(n),now.subtract(BigInteger.ONE));
			a=a1;
			//if(n.compareTo(BigInteger.valueOf(5))==0)//
				//System.out.println(a1+" "+a2+" "+a3);
				//System.out.println(now.add(now).subtract(n)+" "+now.subtract(BigInteger.ONE));//
			a=a.add(a2);
			a=a.add(a3);
			mp.put(n,a);
			return mp.get(n);
		}
	}
	
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章