关闭

2012 Tianjing Online (HDU4278 - 4287)

437人阅读 评论(0) 收藏 举报
分类:


You has solved this problem :-) 4278  Faulty Odometer 70.17%(748/1066)
You has solved this problem :-) 4279  Number 27.74%(713/2570)
       
You has solved this problem :-) 4281  Judges' response 56.48%(266/471)
You has solved this problem :-) 4282  A very hard mathematic problem 29.56%(1076/3640)
You has solved this problem :-) 4283  You Are the One 46.46%(591/1272)
You has solved this problem :-) 4284  Travel 27.36%(738/2697)
       
You has solved this problem :-) 4286  Data Handler 25.06%(510/2035)
You has solved this problem :-) 4287  Intelligent IME 48.75%(937/1922)

Faulty Odometer

Code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<math.h>

using namespace std;

#define nMax 100010
#define sf scanf
#define pf printf
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)

long long s,n;

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
#endif

	while(cin >> n,n) {
		s = 0;
		long long k = n;
		long long t = 1,m;
		while(n) {
			m = n%10;
			if(m>=0 && m<=2) s += m*t;
			else if(m>=4&&m<=7) s += (m-1)*t;
			else s += (m-2)*t;
			n /= 10;
			t *= 8;
		}
		cout << k << ": " <<  s << endl;
	}
	return 0;
}
Number

Code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<math.h>

using namespace std;

#define nMax 100010
#define sf scanf
#define pf printf
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)


int phi(int x) {
	int ret = x;
	int s = 1;
	for(int i=2;i<=x;i++) if(x%i==0) {
		ret = ret/i*(i-1);
		int nu = 0;
		while(x%i ==0 ) {
			x /= i;
			nu ++;
		}
		s *= (nu+1);
	}

	return (ret+s)%2;
}
int ok[nMax];
int sum[nMax];
void baoli() {
	for(int i=1;i<=100;i++) {
		if((i+1+phi(i))%2==1) ok[i]=1;
		else ok[i]=0;
	}
	sum[0] =0;
	for(int i=1;i<=100;i++) {
		sum[i] = sum[i-1] + ok[i];
		pf("%d := %d %d\n",i,i/2-1,sum[i]);
	}
}
#define ll long long
ll f(ll x) {
	if(x<=5ll) return 0;
//	if(x<=15ll) return x/2ll-1ll;
	ll y = sqrt(x);
//	cout << "f" << endl;
	if(y*y == x) {
		if(y%2 == 0) {
			return x/2-2;
		}else return x/2-1;
	}else {
		if(y%2 == 0) {
			return x/2-2;
		}else return x/2-1;
	}
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
#endif
	//baoli();
	ll a,b;
	int T;
	cin >> T;
	while(T--) {
		cin >> a >> b;
	//	cout << f(b) << endl;
		cout << f(b) - f(a-1) << endl;
	}

	return 0;
}
C ...

Judges' response

Code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
using namespace std;
#define A first
#define B second
#define pii pair<int,int>
#define pb push_back
#define sqr(x) ((x)*(x))
const int maxn=20;
const int maxm=1<<16;
const int none=1<<30;
int n,m,all;
int c[maxn];
pii a[maxn];
int cost[maxm];
int f[maxm];
int ans1,ans2;
int g[maxn][maxm];

int dis(pii a,pii b){
    return ceil(sqrt(sqr(a.A-b.A)+sqr(a.B-b.B)));
}
int dfs(int S){
    //cout<<S<<' '<<cost[S]<<endl;
    if (cost[S]<=m) return 1;
    if (f[S]!=-1) return f[S];
    int ans=none;
    for (register int s=S;s;s=(s-1)&S) if (cost[s]<=m)
        ans=min(ans,dfs(S^s)+1);
    return f[S]=ans;
}
int init(){
    n--;
    memset(cost,0,sizeof(cost));
    scanf("%d%d",&a[n].A,&a[n].B);
    for (int i=0;i<n;i++) scanf("%d%d",&a[i].A,&a[i].B);
    scanf("%d",&c[n]);
    for (int i=0;i<n;i++) scanf("%d",&c[i]);
    for (int i=0;i<n;i++) if (c[i]>m) return 0;
    all=1<<n ; all--;
    for (register int i=0;i<=all; i++){
        for (register int j=0;j<n;j++)
        if (i>>j&1) cost[i]+=c[j];
    }
    return 1;
}
int get(int S){
    if (S==0) return 0;
    if (f[S]!=-1) return f[S];
    int ans=none;
    for (register int s=S;s;s=(s-1)&S) if (cost[s]!=none)
        ans=min(ans,get(S^s)+cost[s]);
    return f[S]=ans;
}

queue<pii> q;
int vis[maxn][maxm];
int run(){
    for (register int i=0;i<n;i++)
        for (register int j=0;j<=all;j++) g[i][j]=-1;
    for (register int i=0;i<n;i++){
        g[i][1<<i]=dis(a[n],a[i]);
        q.push(pii(i,1<<i));
        vis[i][1<<i]=1;
    }
    while (q.size()){
        int pos=q.front().A,st=q.front().B;
        vis[pos][st]=0; q.pop();
        for (int i=0;i<n;i++){
            if ((st>>i&1)) continue;
            if (c[i]+cost[st]>m) continue;
            int ns=st|(1<<i);
            if (g[i][ns]==-1||g[i][ns]>g[pos][st]+dis(a[pos],a[i])){
                g[i][ns]=g[pos][st]+dis(a[pos],a[i]);
                if (!vis[i][ns]){
                    vis[i][ns]=1;
                    q.push(pii(i,ns));
                }
            }
        }
    }
    for (register int i=0;i<=all;i++){
        int t=none;
        for (int j=0;j<n;j++) if (g[j][i]!=-1)
            t=min(t,g[j][i]+dis(a[j],a[n]));
        cost[i]=t;
    }
    for (register int i=0;i<=all;i++) f[i]=-1;
    return get(all);
}

int main(){
    //freopen("input.txt","r",stdin);
    while (scanf("%d%d",&n,&m)>0){
        if (init()){
            for (int i=0;i<=all;i++) f[i]=-1;
            ans1=dfs(all);
            ans2=run();
            printf("%d %d\n",ans1,ans2);
        } else printf("-1 -1\n");
    }
}
A very hard mathematic problem

Code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
ll X,Y,Z,K,ans;
ll cheak1(){
    ll t=(ll)sqrt(K+0.5);
    if (t*t==K) return (t-1)/2;
    return 0ll;
}
ll mi(ll a,ll n){
    ll y=1;
    while (n){
        if (n&1) y*=a;
        a=a*a;
        n>>=1;
    }
    return y;
}
ll sum(){
    return mi(X,Z)+mi(Y,Z)+X*Y*Z;
}
ll find(){
    Y=X+1; if (sum()>K) return -1;
    for (Y=X+1;;Y++){
        if (sum()==K) return Y;
        if (sum()>K) return 0;
    }
}

void run(){
    for (Z=3;;Z++){
        //if (mi(2ll,Z)>K) break;
        X=1ll;Y=2ll;
        if (sum()>K) break;
        for (X=1;;X++){
            Y=find();
            //if (X==2&&Z==4) cout<<Y<<endl;
            if (Y==-1) break;
            if (X<Y) ans++;
            //if (X<Y) cout<<X<<" "<<Y<<' '<<Z<<endl;
        }
    }
}
int main(){
    //freopen("input.txt","r",stdin);
    while (true){
        scanf("%I64d",&K); if (K==0) break;
        ans=0; ans+=cheak1();
        run();
        printf("%I64d\n",ans);
    }
}
You Are the One

Code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<math.h>

using namespace std;

#define nMax 110
#define sf scanf
#define pf printf
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)

int a[nMax];
int dp[nMax][nMax],sum[nMax];
int const oo = 1000000000;

int inline min(int x,int y) {
	return x < y ? x : y;
}

int dfs(int i,int j) {
	if(i>=j) return dp[i][j] = 0;
	if(dp[i][j] != oo) return dp[i][j];
	dp[i][j] = min(dp[i][j],dfs(i+1,j)+sum[j]-sum[i]);
	dp[i][j] = min(dp[i][j],dfs(i+1,j)+a[i]*(j-i));
	for(int k=i+1;k<=j;k++) {
		dp[i][j] = min(dp[i][j],dfs(i,k-1)+dfs(k+1,j)+a[k]*(j-i)+(sum[j]-sum[k])*(k-i));
	}
	return dp[i][j];
}
int n;

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
#endif
	
	int T,cas=1;
	cin >> T;
	while(T--) {
		sf("%d",&n);
		for(int i=0;i<n;i++) sf("%d",&a[i]);
		sum[0]=a[0];
		for(int i=1;i<n;i++) sum[i]=sum[i-1]+a[i];
		for(int i=0;i<n;i++) for(int j=0;j<n;j++) dp[i][j] = oo;
		//cout << dp[0][n-1] << endl;
		dfs(0,n-1);
		pf("Case #%d: %d\n",cas++,dp[0][n-1]);
	}
	return 0;
}

Data Handler

Code :

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<math.h>

using namespace std;

#define nMax 1000010
#define sf scanf
#define pf printf
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)

#define bug(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl;
struct T1{
	int to[2];
	int val;
	void init(int x) {
		to[0]=to[1]=-1;
		val = x;
	}
};
int l,r,nu;
int lp,rp;

T1 a[nMax];
void mov(int p,int d) { // 0 for left 1 for right
	if(p==0) {
		if(d==0) {
			int tmp = lp;
			if(a[tmp].to[0]==l) lp = a[tmp].to[1];
			else lp = a[tmp].to[0];
			l = tmp;
		}else {
			int tmp = l;
			if(a[tmp].to[0] == lp) l = a[tmp].to[1];
			else l = a[tmp].to[0];
			lp = tmp;
		}
	}else {
		if(d==0) {
			int tmp = r;
			if(a[tmp].to[0] == rp) r = a[tmp].to[1];
			else r = a[tmp].to[0];
			rp = tmp;
		}else {
			int tmp = rp;
			if(a[tmp].to[0] == r) rp = a[tmp].to[1];
			else rp = a[tmp].to[0];
			r = tmp;
		}
	}

}
void ins(int p,int val) {
	a[nu].init(val);
	if(p==0) {
		a[nu].to[0] = l;
		a[nu].to[1] = lp;
		if(a[l].to[0] == lp) a[l].to[0] = nu;
		else a[l].to[1] = nu;
		if(a[lp].to[0] == l) a[lp].to[0] = nu;
		else a[lp].to[1] = nu;
		l = nu;

	}else {
		a[nu].to[0] = r;
		a[nu].to[1] = rp;
		if(a[r].to[0] == rp) a[r].to[0] = nu;
		else a[r].to[1] = nu;
		if(a[rp].to[0] == r) a[rp].to[0] = nu;
		else a[rp].to[1] = nu;
		r = nu;
	}
	nu ++;
}
void del(int p) {
	if(p==0) {
		int R,L;
		if(a[l].to[0] == lp) {
			R = a[l].to[1];
			if(a[lp].to[0] == l) a[lp].to[0] = R;
			else a[lp].to[1] = R;
			if(a[R].to[0] == l) a[R].to[0] = lp;
			else a[R].to[1] = lp;
			l = R;
		}else {
			R = a[l].to[0];
			if(a[lp].to[0] == l) a[lp].to[0] = R;
			else a[lp].to[1] = R;
			if(a[R].to[0] == l) a[R].to[0] = lp;
			else a[R].to[1] = lp;
			l = R;
		}
	}
	else {
		int L,R;
		if(a[r].to[0] == rp) {
			L = a[r].to[1];
			if(a[rp].to[0] == r) a[rp].to[0] = L;
			else a[rp].to[1] = L;
			if(a[L].to[0] == r) a[L].to[0] = rp;
			else a[L].to[1] = rp;
			r = L;
		}else {
			L = a[r].to[0];
			if(a[rp].to[0] == r) a[rp].to[0] = L;
			else a[rp].to[1] = L;
			if(a[L].to[0] == r) a[L].to[0] = rp;
			else a[L].to[1] = rp;
			r = L;
		}
	}
}
void revers() {
	int L , R;
	/*if(a[l].to[0] == lp) L = a[l].to[1];
	else L = a[l].to[0];
	if(a[r].to[0] == rp) R = a[r].to[1];
	else R = a[r].to[0];
	*/
	L = l,R = r;
	if(a[lp].to[0] == L) a[lp].to[0] = R;
	else a[lp].to[1] = R;
	if(a[rp].to[0] == R) a[rp].to[0] = L;
	else a[rp].to[1] = L;
	if(a[L].to[0] == lp) a[L].to[0] = rp;
	else a[L].to[1] = rp;
	if(a[R].to[0] == rp) a[R].to[0] = lp;
	else a[R].to[1] = lp;
	swap(l,r);
}

void out() {
	while(lp!=0) {
		mov(0,0);
	}
	vector<int> ans;
	ans.clear();
	int step = 0;
	while(l!=0) {
		ans.push_back(a[l].val);
		mov(0,1);
		//pf("l=%d lp=%d\n",l,lp);
		//step ++;
	}
	for(int i=0,len=ans.size();i<len;i++){
		if(i) pf(" ");
		pf("%d",ans[i]);
	}
	pf("\n");
}

int n,m;
char s[nMax];

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
#endif

	int T;
	cin >> T;
	while(T--) {
		sf("%d",&n);
		nu = 1;
		int x;
		for(nu=1;nu<=n;nu++) {
			sf("%d",&x);
			a[nu].init(x);
			a[nu].to[0] = nu-1;
			a[nu].to[1] = nu+1;
		}
		a[n].to[1] = 0;
		sf("%d%d",&l,&r);
		lp = l-1; rp = r+1;
		if(rp == n+1) rp = 0;
		sf("%d",&m);
		while(m--) {
			sf("%s",s);
			if(s[0] == 'M') {
				int d = s[4]=='L'?0:1;
				sf("%s",s) ;
				mov(s[0]=='R'?1:0,d);
			}else if(s[0] == 'I') {
				sf("%s%d",s,&x);
				ins(s[0]=='L'?0:1,x);
			}else if(s[0] == 'D') {
				sf("%s",s);
				del(s[0]=='R'?1:0);
			}else {
				revers();
			}
			//pf("l=%d lp=%d r=%d rp=%d\n",l,lp,r,rp);
		}
		out();
	}

	return 0;
}

Code :

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<math.h>

using namespace std;

#define nMax 50000
#define sf scanf
#define pf printf
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)

int inline f(char ch) {
	if(ch >= 'a' && ch <= 'c') return 2;
	if(ch >= 'd' && ch <= 'f') return 3;
	if(ch >= 'g' && ch <= 'i') return 4;
	if(ch >= 'j' && ch <= 'l') return 5;
	if(ch >= 'm' && ch <= 'o') return 6;
	if(ch >= 'p' && ch <= 's') return 7;
	if(ch >= 't' && ch <= 'v') return 8;
	if(ch >= 'w' && ch <= 'z') return 9;
}

int node[nMax][15];
int ok[nMax][15];
int nu;
void init() {
	memset(node,-1,sizeof(node));
	memset(ok,0,sizeof(ok));
	nu = 1;
};
void ins(char s[]) {
	int p = 0;
	for(int i=0;s[i];i++) {
		s[i] = f(s[i]);
	}
	int i,n=strlen(s);
	for(i=0;i<n;i++) {
		if(i+1==n) {
			ok[p][s[i]] ++;
			break;
		}
		if(node[p][s[i]]==-1) {
			node[p][s[i]] = nu++;
		}
		p = node[p][s[i]];
	}
}
int find(char s[]) {
	int n=strlen(s);
	rep(i,n) s[i] -= '0';
	int p = 0;
	for(int i=0;i<n;i++) {
		if(i+1 == n) return ok[p][s[i]];
		if(node[p][s[i]] == -1) return 0;
		p = node[p][s[i]];
	}
}
char ask[nMax][10];
char s[nMax];
int n,m,ans;

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
#endif

	int T;
	cin >> T;
	while(T--) {
		sf("%d%d",&n,&m);
		rep(i,n) sf("%s",ask[i]);
		init();
		rep(i,m) {
			sf("%s",s);
			ins(s);
		}
		rep(i,n) {
			ans = find(ask[i]);
			pf("%d\n",ans);
		}
	}
	return 0;
}






0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:129817次
    • 积分:3550
    • 等级:
    • 排名:第9296名
    • 原创:241篇
    • 转载:0篇
    • 译文:0篇
    • 评论:11条
    文章分类
    最新评论