A:
题目大意: Alice和Bob分别按照不同规则从一堆石子中取石子,问至少需要多少次能够把石子取玩。
#include<stdio.h>
#include <string.h>
int g[10000+100];
int f[10000+100];
int min(int x,int y)
{
if(x<y) return x;
return y;
}
void solve()
{
g[1]=1;
f[1]=1;
int i,j;
for(i=2;i<=10000;i++)
{
f[i]=i;
for(j=1;j<=i;j=j*2)
{
f[i]=min(f[i],g[i-j]+1);
}
g[i]=i;
for(j=1;j<=i;j=3*j)
{
g[i]=min(g[i],f[i-j]+1);
}
}
}
int main()
{
int t,n;
solve();
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
printf("%d\n",f[n]);
}
return 0;
}
B:
题目大意:给定一棵带值二叉树,问至少需要修改多少节点的值能够使得二叉树成为二叉排序树。
解题思路:先使用中序遍历获得整个序列,然后求这个序列的最长上升子序列即可。
Ans = n – 最长上升子序列长度
时间复杂度:O(nlogn)
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 5010;
const int inf = 0x3f3f3f3f;
int g[N],d[N];
struct node{
int key,lch,rch;
}Q[N];
int a[N],pre[N],n,top;
int idx[N],m;
void dfs(int x)
{
if(Q[x].lch!=0) dfs(Q[x].lch);
a[++top]=Q[x].key;
if(Q[x].rch!=0) dfs(Q[x].rch);
// a[++top]=Q[x].key;
}
int lis()
{
for(int i=1;i<=n;i++)
g[i]=inf;
int ans=0;
for(int i=1;i<=n;i++)
{
int k=lower_bound(g+1,g+1+n,a[i])-g;
d[i]=k;
g[k]=a[i];
ans=max(ans,d[i]);
}
return n-ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(pre,0,sizeof(pre));
for(int i=1;i<=n;i++)
{
scanf("%d%d%d",&Q[i].key,&Q[i].lch,&Q[i].rch);
pre[Q[i].lch]=i;
pre[Q[i].rch]=i;
}
int rt;
for(int i = 1;i <= n; i++)
if(pre[i]==0)
{
rt=i;
break;
}
top=0;
dfs(rt);
// for(int i = 1; i <= n; i++)
// printf("%d ", a[i] ); puts("");
printf("%d\n",lis());
}
return 0;
}
D DNA
字典树+dp
package com.wy.tools;
import java.util.HashMap;
import java.util.Scanner;
public class Main {
HashMap<Character,Integer> map=new HashMap<Character,Integer>();
Node root;
long[] f;
private Node tmp;
static Scanner s=new Scanner(System.in);
public Main()
{
String st;
map.put('A', 0);
map.put('T', 1);
map.put('G', 2);
map.put('C', 3);
int x;
int n;
f=new long[110000];
root =new Node();
n=s.nextInt();
while(n--!=0)
{
st=s.next();
StringBuffer sb=new StringBuffer(st);
sb.reverse();
x=s.nextInt();
// insert(st,x);
insert(sb.toString(),x);
}
st=s.next();
int p;
for(int i=0;i<st.length();i++)
{
tmp=root;
p=i;
if(i!=0)
f[i]=f[i-1];
while(p>=0)
{
tmp=tmp.son[map.get(st.charAt(p))];
if(tmp==null)
break;
if(tmp.val!=-1)
{
if(p==0)
f[i]=Math.max(f[i], tmp.val);
else
f[i]=Math.max(f[p-1]+tmp.val,f[i] );
}
p=p-1;
}
}
// for(int i=0;i<st.length();i++)
// System.out.println(f[i]);
System.out.println(f[st.length()-1]);
}
public void insert(String s,int x)
{
tmp=root;
for(int i=0;i<s.length();i++)
{
int idx=map.get(s.charAt(i));
if(tmp.son[idx]==null)
{
tmp.son[idx]=new Node();
}
tmp=tmp.son[idx];
}
tmp.val=Math.max(tmp.val, x);
}
public static void main(String[] args) {
int t=s.nextInt();
while(t--!=0)
{
new Main();
}
}
}
class Node
{
Node [] son=new Node[4];
int val=-1;
}
/*
*
3
GG 100
ACG 50
GGAC 10000
GGGGACTACGG
GGCAGGTACGG
*/
E Edges of Triangle(未过)
#include <stdio.h>
#include <string.h>
#include <math.h>
struct Point
{
double x,y;
}point[5];
struct Line
{
Point a,b;
}line[5];
/*
Point intersection(Line u,Line v){
Point ret=u.a;
double t=((u.a.x-v.a.x)*(v.b.y-v.a.y)-(u.a.y-v.a.y)*(v.b.x-v.a.x))
/((u.a.x-u.b.x)*(v.b.y-v.a.y)-(u.a.y-u.b.y)*(v.b.x-v.a.x));
ret.x+=(u.b.x-u.a.x)*t;
ret.y+=(u.b.y-u.a.y)*t;
return ret;
}
*/
Point intersection(double a1,double b1,double c1,double a2,double b2,double c2){
Point ret;
ret.x=(b2*c1-b1*c2)/(a1*b2-a2*b1);
if(b1!=0)
ret.y = (c1-a1*ret.x)/b1;
else if(b2!=0)
ret.y = (c2-a2*ret.x)/b2;
return ret;
}
long long gcd(long long a,long long b)
{
if(b==0) return a;
return gcd(b,a%b);
}
long long Onside(int n)
{
int i=0,ret=0;
for(i=0;i<n;i++)
ret+=gcd( (long long)100000000000*fabs(point[(i+1)%n].x-point[i%n].x) ,(long long)100000000000*fabs(point[(i+1)%n].y-point[i%n].y) );
return ret/100000000000;
}
int main()
{
int t,i;
double a[5],b[5],c[5];
scanf("%d",&t);
while(t--)
{
//scanf("%lf%lf%lf%lf%lf%lf",&a1,&b1,&a2,&b2,&a3,&b3);
for(i=1;i<=3;i++)
{
scanf("%lf%lf%lf",&a[i],&b[i],&c[i]);
// printf("(a)%lf (b)%lf (c)%lf\n",a,b,c);
}
point[0]= intersection(a[1],b[1],c[1],a[2],b[2],c[2]);
point[1]= intersection(a[2],b[2],c[2],a[3],b[3],c[3]);
point[2]= intersection(a[1],b[1],c[1],a[3],b[3],c[3]);
// for(i=0;i<3;i++)
//{
// printf("%lf %lf\n",point[i].x,point[i].y);
// }
printf("%lld\n",Onside(3));
}
return 0;
}
F:Five Tigers
模拟
H: Hurry up(三分)
#include <stdio.h>
#include <string.h>
#include <math.h>
double vr,vt;
double x3,y3;
double x4,y4;
double distance(double x1,double y1,double x2,double y2)
{
return sqrt( (x2-x1)*(x2-x1)+(y2-y1)*(y2-y1) );
}
double f(double x)
{
double t1= distance(x3,y3,x,0)/vr;
double t2= distance(x,0,x4,y4)/vt;
return t1+t2;
}
double min(double x,double y)
{
if(x<y) return x;
return y;
}
int main()
{
int t;
double t1;
scanf("%d",&t);
while(t--)
{
scanf("%lf%lf%lf%lf%lf%lf",&x3,&y3,&x4,&y4,&vr,&vt);
t1= distance(x3,y3,x4,y4)/vr;
double l=x3,r=x4;
for(int i=0;i<100;i++)
{
double m1=l + (r-l)/3;
double m2=r - (r-l)/3;
if(f(m1)<f(m2)) r=m2;
else l=m1;
}
printf("%.2lf\n",min(t1,f(l)));
}
return 0;
}
I:I Love Military Chess(模拟)
J:Jack’s Sequence
类型:构造
题目大意:给定一个合法的括号序列,求下一个字典序排列最小的合法括号序列。
解题思路:找出最后一个符合(xx(xx)xx)xx模型的子串并将其改为(xx)(((..)))即可,xx表示任意合法的括号序列。
(())()
()(())
可参考下面的博客
http://www.cnblogs.com/yefeng1627/archive/2013/05/12/3074443.html