2015年2月28日 星期六

[UVA] 12428 - Enemy at the Gates

#include<bits/stdc++.h>
using namespace std;
int main(){
    long long int t,n,m,x,y,i,j;
    scanf("%lld",&t);
    while(t--){
        scanf("%lld%lld",&n,&m);
        long long int L=0,R=n+1,M;
        while(L+1!=R){
            M=(L+R)/2;
            if(M*(M-1)/2+n-M>=m) R=M;
            else L=M;
        }
        printf("%lld\n",n-R);
    }
}

[UVA] 12694 - Meeting Room Arrangement

#include<bits/stdc++.h>
using namespace std;
struct P{
    int x,y;
};
int cmp(P a,P b){
    return a.y<b.y;
}
int main(){
    int t,n,m,x,y,i,j;
    scanf("%d",&t);
    while(t--){
        vector<P> arr;
        while(scanf("%d%d",&x,&y) && x+y){
            arr.push_back((P){x,y});
        }
        sort(arr.begin(),arr.end(),cmp);
        int dp[15]={0};
        for(i=1,j=0;i<=10;i++){
            dp[i]=dp[i-1];
            while(j<arr.size() && arr[j].y==i){
                dp[i]=max(dp[i],dp[arr[j].x]+1);
                j++;
            }
        }
        printf("%d\n",dp[10]);
    }
}

[UVA] 12650 - Dangerous Dive

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,m,x,i,j;
    while(scanf("%d%d",&n,&m)!=EOF){
    int is[10005]={0};
        for(i=0;i<m;i++){
            scanf("%d",&x);
            is[x]=1;
        }
        int flag=0;
        for(i=1;i<=n;i++){
            if(is[i]==0){
                flag=1;
                printf("%d ",i);
            }
        }
        if(flag==0) putchar('*');
        puts("");
    }
}

[UVA] 11714 - Blind Sorting

#include<bits/stdc++.h>
using namespace std;
int main(){
    long long int n,m,add;
    while(scanf("%lld",&n)!=EOF){
        m=1,add=0;
        while(m<n){
            m*=2,add++;
        }
        printf("%lld\n",n-1+add-1);
    }
}

[UVA] 11730 - Number Transformation

#include<bits/stdc++.h>
using namespace std;
struct P{
    int u,add;
}in,out;
int is[1005];
vector<int> pri;
int main(){
    int t,n,m,i,j,C=0;
    for(i=2;i<=1000;i++){
        if(is[i]==0){
            for(j=i+i;j<=1000;j+=i){
                is[j]=1;
            }
            pri.push_back(i);
        }
    }
    while(scanf("%d%d",&n,&m)!=EOF && n&&m){
        queue<P> que;
        que.push((P){n,0});
        int vi[1005]={0},flag=0;
        vi[n]=1;
        while(que.size()){
            out=que.front();
            que.pop();
            if(out.u==m){
                flag=1;
                break;
            }
            for(i=0;i<pri.size() && pri[i]<out.u;i++){
                in.add=out.add+1;
                if(out.u%pri[i]) continue;
                in.u=out.u+pri[i];
                if(vi[in.u] || in.u>m) continue;
                vi[in.u]=1;
                que.push(in);
            }
        }
        printf("Case %d: %d\n",++C,flag?out.add:-1);
    }
}

[UVA] 11764 - Jumping Mario

#include<bits/stdc++.h>
using namespace std;
int s[55];
int main(){
    int t,n,i,j,C=0;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(i=0;i<n;i++){
            scanf("%d",&s[i]);
        }
        int a=0,b=0;
        for(i=1;i<n;i++){
            if(s[i]>s[i-1]) a++;
            if(s[i]<s[i-1]) b++;
        }
        printf("Case %d: %d %d\n",++C,a,b);
    }
}

[UVA] 10420 - List of Conquests

#include<bits/stdc++.h>
using namespace std;
map<string,int> ma;
map<string,int>::iterator it;
char s1[100],s2[100];
int main(){
    int n;
    scanf("%d",&n);
    getchar();
    while(n--){
        gets(s1);
        sscanf(s1,"%s",s2);
        ma[string(s2)]++;
    }
    for(it=ma.begin();it!=ma.end();it++){
        printf("%s %d\n",it->first.c_str(),it->second);
    }
}

[UVA] 11547 - Automatic Answer

#include<bits/stdc++.h>
using namespace std;
int main(){
    int t,n;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        n=(n*567/9+7492)*235/47-498;
        if(n<0) n*=-1;
        printf("%d\n",n/10%10);
    }
}