2018/8/9

一:

 
Since 1945, when the first nuclear bomb was exploded by the Manhattan Project team in the US, the number of nuclear weapons have soared across the globe. 
Nowadays,the crazy boy in FZU named AekdyCoin possesses some nuclear weapons and wanna destroy our world. Fortunately, our mysterious spy-net has gotten his plan. Now, we need to stop it. 
But the arduous task is obviously not easy. First of all, we know that the operating system of the nuclear weapon consists of some connected electric stations, which forms a huge and complex electric network. Every electric station has its power value. To start the nuclear weapon, it must cost half of the electric network's power. So first of all, we need to make more than half of the power diasbled. Our tanks are ready for our action in the base(ID is 0), and we must drive them on the road. As for a electric station, we control them if and only if our tanks stop there. 1 unit distance costs 1 unit oil. And we have enough tanks to use. 
Now our commander wants to know the minimal oil cost in this action.

Input

The first line of the input contains a single integer T, specifying the number of testcase in the file. 
For each case, first line is the integer n(1<= n<= 100), m(1<= m<= 10000), specifying the number of the stations(the IDs are 1,2,3...n), and the number of the roads between the station(bi-direction). 
Then m lines follow, each line is interger st(0<= st<= n), ed(0<= ed<= n), dis(0<= dis<= 100), specifying the start point, end point, and the distance between. 
Then n lines follow, each line is a interger pow(1<= pow<= 100), specifying the electric station's power by ID order.

Output

The minimal oil cost in this action. 
If not exist print "impossible"(without quotes).

Sample Input

2
2 3
0 2 9
2 1 3
1 0 2
1
3
2 1
2 1 3
1
3

Sample Output

5
impossible

最短路+01背包,用了floyd算法和一维滚动数组dp解决

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<memory.h>
#define INF 0x3f3f3f3f
using namespace std;
int n,m;
int a[10010];
int mp[305][305];
int dp[1000005];
void floyd()
{
    for(int k=0;k<=n;k++)
        for(int i=0;i<=n;i++)
           for(int j=0;j<=n;j++)
              mp[i][j]=min(mp[i][j],mp[i][k]+mp[k][j]);
}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        cin>>n>>m;
        for(int i=0;i<=n;i++)
           {
               for(int j=0;j<=n;j++)
            {
                mp[i][j]=INF;

            }mp[i][i]=0;
           }
        for(int i=0;i<m;i++)
        {
            int a,b,x;
            scanf("%d%d%d",&a,&b,&x);
            if(mp[a][b]>x)
            {
            mp[a][b]=x;
            mp[b][a]=x;}
        }
        floyd();
        //printf("1***\n");
        int sum=0;
        int maxx=0;
        for(int i=1;i<=n;i++)
            {
                scanf("%d",&a[i]);
                sum+=a[i];
                if(mp[0][i]!=INF)
                    maxx+=mp[0][i];
            }

        memset(dp,0,sizeof(dp));
       // printf("2***\n");
       // printf("%d\n%d",mp[0][1],maxx);
        for(int i=1;i<=n;i++)
        {
            for(int j=maxx;j>=mp[0][i];j--)
                if(dp[j-mp[0][i]]+a[i]>dp[j])
                dp[j]=dp[j-mp[0][i]]+a[i];
        }
      //  printf("3***\n");
        int flag=0;
        for(int i=1;i<=maxx;i++)
        {
            if(dp[i]>sum/2.0)
            {
                flag=1;
                printf("%d\n",i);
                break;
            }
        }
        if(flag==0)
            printf("impossible\n");

    // printf("4***\n");
    }
    return 0;
}

二:

?? gets an sequence S with n intergers(0 < n <= 100000,0<= S[i] <= 1000000).?? has a magic so that he can change 0 to any interger(He does not need to change all 0 to the same interger).?? wants you to help him to find out the length of the longest increasing (strictly) subsequence he can get.

Input

The first line contains an interger T,denoting the number of the test cases.(T <= 10) 
For each case,the first line contains an interger n,which is the length of the array s. 
The next line contains n intergers separated by a single space, denote each number in S. 

Output

For each test case, output one line containing “Case #x: y”(without quotes), where x is the test case number(starting from 1) and y is the length of the longest increasing subsequence he can get.

Sample Input

2
7
2 0 2 1 2 0 5
6
1 2 3 3 0 0

Sample Output

Case #1: 5
Case #2: 5


        
  

Hint

In the first case,you can change the second 0 to 3.So the longest increasing subsequence is 0 1 2 3 5.
        

//最长上升子序列做一些修改

#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
int a[100005];
int lo[100005];
int dp[100005];
int main()
{
    int T;
    cin>>T;
    for(int i=1;i<=T;i++)
    {
        int n;
        cin>>n;
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        lo[0]=-1000000;
        lo[1]=a[1];
        int sum=0;
        int len=1;
        for(int i=2;i<=n;i++)
        {
            if(a[i]>lo[len])
                lo[++len]=a[i];
            else
                if(a[i]==0)
                {
                    for(int j=len;j>=sum;j--)
                    {
                        lo[j+1]=lo[j]+1;
                    }
                    len++;
                    sum++;
                }
                else
                {
                    int p=lower_bound(lo,lo+len,a[i])-lo;
                    lo[p]=a[i];
                }
        }
        printf("Case #%d: %d\n",i,len);

    }
    return 0;

}

三:

Bobo has nn tuples (a1,b1,c1),(a2,b2,c2),…,(an,bn,cn)(a1,b1,c1),(a2,b2,c2),…,(an,bn,cn). 
He would like to find the lexicographically smallest permutation p1,p2,…,pnp1,p2,…,pn of 1,2,…,n1,2,…,n such that for i∈{2,3,…,n}i∈{2,3,…,n} it holds that 

api−1+bpi−1api−1+bpi−1+cpi−1≤api+bpiapi+bpi+cpi.api−1+bpi−1api−1+bpi−1+cpi−1≤api+bpiapi+bpi+cpi.

 

Input

The input consists of several test cases and is terminated by end-of-file. 

The first line of each test case contains an integer nn. 
The ii-th of the following nn lines contains 33 integers aiai, bibi and cici. 

Output

For each test case, print nn integers p1,p2,…,pnp1,p2,…,pn seperated by spaces. 
DO NOT print trailing spaces. 

## Constraint 

* 1≤n≤1031≤n≤103 
* 1≤ai,bi,ci≤2×1091≤ai,bi,ci≤2×109 
* The sum of nn does not exceed 104104. 

Sample Input

2
1 1 1
1 1 2
2
1 1 2
1 1 1
3
1 3 1
2 2 1
3 1 1

Sample Output

2 1
1 2
1 2 3

//普通的排序,用sort就解决了,但是精度要求很高!

#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
typedef unsigned long long ull;
struct node
{
    int a,b,c,t;
}p[1100];
bool cmp(node x,node y)
{
    ull aa=(ull)x.c * ((ull)y.a + y.c + y.b);
    ull bb=(ull)y.c * ((ull)x.a + x.c + x.b);
    if(aa!=bb)return aa>bb;
    else return x.t<y.t;
}
int n;
int main()
{
    while(~scanf("%d",&n))
    {
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d%d",&p[i].a,&p[i].b,&p[i].c);
        p[i].t=i;
    }
    sort(p+1,p+n+1,cmp);
    printf("%d",p[1].t);
    for(int i=2;i<=n;i++)
        printf(" %d",p[i].t);
   puts("");
    }
    return 0;
}

四:

Little Q and Little T are playing a game on a tree. There are nnvertices on the tree, labeled by 1,2,...,n1,2,...,n, connected by n−1n−1bidirectional edges. The ii-th vertex has the value of wiwi. 
In this game, Little Q needs to grab some vertices on the tree. He can select any number of vertices to grab, but he is not allowed to grab both vertices that are adjacent on the tree. That is, if there is an edge between xx and yy, he can't grab both xx and yy. After Q's move, Little T will grab all of the rest vertices. So when the game finishes, every vertex will be occupied by either Q or T. 
The final score of each player is the bitwise XOR sum of his choosen vertices' value. The one who has the higher score will win the game. It is also possible for the game to end in a draw. Assume they all will play optimally, please write a program to predict the result. 

Input

The first line of the input contains an integer T(1≤T≤20)T(1≤T≤20), denoting the number of test cases. 
In each test case, there is one integer n(1≤n≤100000)n(1≤n≤100000) in the first line, denoting the number of vertices. 
In the next line, there are nn integers w1,w2,...,wn(1≤wi≤109)w1,w2,...,wn(1≤wi≤109), denoting the value of each vertex. 
For the next n−1n−1 lines, each line contains two integers uu and vv, denoting a bidirectional edge between vertex uu and vv. 

Output

For each test case, print a single line containing a word, denoting the result. If Q wins, please print Q. If T wins, please print T. And if the game ends in a draw, please print D. 

Sample Input

1
3
2 2 2
1 2
1 3

Sample Output

Q

//看似是一道博弈题,其实跟博弈没一点关系,和树也没有关系,考察异或的性质

a^b=0,所以a==b;

a^b!=0所以a!=b;

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int a[100005];
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int n;
        scanf("%d",&n);
        int sum=0;
        for(int i=0;i<n;i++)
            {
                scanf("%d",&a[i]);
                sum^=a[i];
            }
        int a,b;
        for(int i=0;i<n-1;i++)
            scanf("%d%d",&a,&b);
        if(sum==0)printf("D\n");
        else printf("Q\n");

    }
    return 0;

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值