Try Again

ACM-ICPC 2016 Qingdao Preliminary Contest

A I Count Two Three

I will show you the most popular board game in the Shanghai Ingress Resistance Team.

It all started several months ago.

We found out the home address of the enlightened agent Icount2three and decided to draw him out.

Millions of missiles were detonated, but some of them failed.

After the event, we analysed the laws of failed attacks.

It's interesting that the iii-th attacks failed if and only if iii can be rewritten as the form of 2a3b5c7d2^a3^b5^c7^d2a3b5c7d which a, b, c, d are non-negative integers.

At recent dinner parties, we call the integers with the form 2^a3^b5^c7^d "I Count Two Three Numbers".

A related board game with a given positive integer nnn from one agent, asks all participants the smallest "I Count Two Three Number" no smaller than nnn.
Input Format

The first line of input contains an integer t(1≤t≤500000), the number of test cases. ttt test cases follow. Each test case provides one integer n(1≤n≤109).
Output Format

For each test case, output one line with only one integer corresponding to the shortest "I Count Two Three Number" no smaller than nnn.
样例输入

10
1
11
13
123
1234
12345
123456
1234567
12345678
123456789

样例输出

1
12
14
125
1250
12348
123480
1234800
12348000
123480000
打表后二分查找。

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#pragma GCC diagnostic error "-std=c++11"
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define esp 1e-9
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 0x3f3f3f3f
typedef long long ll;
ll v[10006],tot=0;
ll a[70],b[70],c[70],d[70];
void init()
{
    a[0]=b[0]=c[0]=d[0]=1;
    for(int i=1;i<=40;i++)
        a[i]=a[i-1]*2;
    for(int i=1;i<=40;i++)
        b[i]=b[i-1]*3;
    for(int i=1;i<=40;i++)
        c[i]=c[i-1]*5;
    for(int i=1;i<=40;i++)
        d[i]=d[i-1]*7;
    for(int i=0;i<=40;i++)
    {
        if(a[i]>1000000000) break;
        for(int j=0;j<=40;j++)
        {
            if(b[j]>1000000000) break;
            for(int k=0;k<=40;k++)
            {
                if(c[k]>1000000000) break;
                for(int z=0;z<=40;z++)
                {
                    if(d[z]>1000000000) break;
                    if(a[i]*b[j]*c[k]*d[z]<=1000000000) v[tot++]=a[i]*b[j]*c[k]*d[z];
                    else break;
                }
            }
        }
    }
    sort(v,v+tot);
}
int main()
{
    ll n,t;
    scanf("%d",&t);
    init();
    while(t--)
    {
        scanf("%lld",&n);
        int k=lower_bound(v,v+tot,n)-v;
        printf("%lld\n",v[k]);
    }
    return 0;
}

B Cure

Given an integer nnn, we only want to know the sum of 1/k2 where k from 1 to n.
Input Format

There are multiple cases.

For each test case, there is a single line, containing a single positive integer nnn.

The input file is at most 1M.
Output Format

The required sum, rounded to the fifth digits after the decimal point.
样例输入

1
2
4
8
15

样例输出

1.00000
1.25000
1.42361
1.52742
1.58044

保留5位小数,当k值足够大时,恒等于0

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#pragma GCC diagnostic error "-std=c++11"
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define esp 1e-9
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 0x3f3f3f3f
typedef long long ll;
ll n;
double ans[1000006];
string s;
int main()
{
    ans[0]=0.0;
    for(ll i=1;i<=1000000;i++)
        ans[i]=ans[i-1]+1.0/(i*i);
    while(cin>>s)
    {
        if(s.size()>7) printf("%.5lf\n",ans[1000000]);
        else
        {
            int n=0;
            for(int i=0;i<s.size();i++)
                n=n*10+(s[i]-'0');
            if(n>1000000) printf("%.5lf\n",ans[1000000]);
            else printf("%.5lf\n",ans[n]);
        }
    }
    return 0;
}

D Tea

Tea is good.

Tea is life.

Tea is everything.

The balance of tea is a journey of pursuing balance of the universe.

Alice knows that.

Alice wants to teach you the art of pouring tea.

Alice has a pot of tea.

The exact volume of tea is not important.

The exact volume of tea is at least LLL.

The exact volume of tea is at most RRR.

Alice put two empty cups between you and her.

Alice wants the two cups filled by almost equal volume of tea.

Yours cannot be 1 unit more than hers.

Hers cannot be 1 unit more than yours.

Alice wants you to pour the tea.

Alice wants you to pour until the pot is almost empty.

Alice wants no more than 1 unit volume of tea remaining in the pot.

You cannot read the residue volume of tea remaining in the pot.

You can only know the tea status in the pot, empty or not.

Alice does not want you to pour the tea too many times.

You better pour as few times as possible.
Input Format

There are multiple cases.

For each case, there is one line of two integers L and R, separated by single space.

Here are some analyses about sample cases.

For the first case, pouring 1 unit into one cup will satisfy Alice.

For the second case, it is clearly that you cannot only pour once to reach the desired balance, but she can achieve it by pouring twice.

First you pour 1.5 units into one cup, then you attempt to pour another 1.5 units into the other cup.

Since the lower bound is 2, at least 0.5 unit remains in the pot after the first pouring.

If the initial volume is in range [2, 3], the second cup will have volume in range [0.5, 1.5] which is balanced with 1.51.51.5 unit in the first cup, and at most 111 unit remain after these two attempts.

About 1000 test cases, and 0≤L≤R≤1016.
Output Format

For each case, there should be a single integer in a single line, the least number of pouring attempts.
样例输入

2 2
2 4

样例输出

1
2

题目很长读了很久,但就是使得分配完后,两个杯子内茶水的体积相差不超过一,茶壶内剩余不超过一,问最少倒几次

首先确定下界,这是茶壶里一定存在的可以平分,下界到上界不确定的,只能按部就班分

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#pragma GCC diagnostic error "-std=c++11"
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define esp 1e-9
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 0x3f3f3f3f
typedef long long ll;
ll l,r;
int main()
{
    while(~scanf("%lld%lld",&l,&r))
    {
        if(r<=1) printf("0\n");
        else if(r<=2) printf("1\n");
        else if(l==r || l==r-1) printf("2\n");
        else if(r>l+1)
        {
            if(l==0) l=1;
            printf("%lld\n",(r-l)/2+1);
        }
    }
    return 0;
}

E Balanced Game

Rock-paper-scissors is a zero-sum hand game usually played between two people, in which each player simultaneously forms one of three shapes with an outstretched hand. These shapes are "rock", "paper", and "scissors". The game has only three possible outcomes other than a tie: a player who decides to play rock will beat another player who has chosen scissors ("rock crushes scissors") but will lose to one who has played paper ("paper covers rock"); a play of paper will lose to a play of scissors ("scissors cut paper"). If both players choose the same shape, the game is tied and is usually immediately replayed to break the tie.

Recently, there is a upgraded edition of this game: rock-paper-scissors-Spock-lizard, in which there are totally five shapes. The rule is simple: scissors cuts paper; paper covers rock; rock crushes lizard; lizard poisons Spock; Spock smashes scissors; scissors decapitates lizard; lizard eats paper; paper disproves Spock; Spock vaporizes rock; and as it always has, rock crushes scissors.

Both rock-paper-scissors and rock-paper-scissors-Spock-lizard are balanced games. Because there does not exist a strategy which is better than another. In other words, if one chooses shapes randomly, the possibility he or she wins is exactly 50%50\%50% no matter how the other one plays (if there is a tie, repeat this game until someone wins). Given an integer NNN, representing the count of shapes in a game. You need to find out if there exist a rule to make this game balanced.
Input Format

The first line of input contains an integer ttt, the number of test cases. ttt test cases follow.

For each test case, there is only one line with an integer N(2≤N≤1000), as described above.

Here is the sample explanation.

In the first case, donate two shapes as A and B. There are only two kind of rules: A defeats B, or B defeats A. Obviously, in both situation, one shapes is better than another. Consequently, this game is not balanced.

In the second case, donate two shapes as A, B and C. If A defeats B, B defeats C, and C defeats A, this game is balanced. This is also the same as rock-paper-scissors.

In the third case, it is easy to set a rule according to that of rock-paper-scissors-Spock-lizard.
Output Format

For each test cases, output "Balanced" if there exist a rule to make the game balanced, otherwise output "Bad".
样例输入

3
2
3
5

样例输出

Bad
Balanced
Balanced
题目很长,刚开始猜小于等于是balanced,否则是bad。wa了。

仔细一分析,只要是偶数我只取可以互相抵消的一定可以打平,分奇偶。

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#pragma GCC diagnostic error "-std=c++11"
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define esp 1e-9
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 0x3f3f3f3f
typedef long long ll;
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        puts(n&1?"Balanced":"Bad");
    }
    return 0;
}

F The Best Path

Alice is planning her travel route in a beautiful valley. In this valley, there are NNN lakes, and MMM rivers linking these lakes. Alice wants to start her trip from one lake, and enjoys the landscape by boat. That means she need to set up a path which go through every river exactly once. In addition, Alice has a specific number (a1​, a2​, ..., an​) for each lake. If the path she finds is P0→P1→...→Pt, the lucky number of this trip would be aP0xor aP1 xor ... xor aPt. She want to make this number as large as possible. Can you help her?
Input Format

The first line of input contains an integer ttt, the number of test cases. ttt test cases follow.

For each test case, in the first line there are two positive integers N(N≤100000) and M(M≤500000), as described above. The iii-th line of the next NNN lines contains an integer ai(∀i, 0≤ai≤10000) representing the number of the i-th lake.

The i-th line of the next M lines contains two integers ui and vi representing the iii-th river between the uith lake and vith lake. It is possible that ui=vi.
Output Format

For each test cases, output the largest lucky number. If it dose not have any path, output "Impossible".
样例输入

2
3 2
3
4
5
1 2
2 3
4 3
1
2
3
4
1 2
2 3
2 4

样例输出

2
Impossible
考欧拉路径的性质,自己写的max函数不能用,无限wa,还得用库函数

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#pragma GCC diagnostic error "-std=c++11"
#define lowbit(x) (x&(-x))
//#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define esp 1e-9
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 0x3f3f3f3f
typedef long long ll;
const int maxn=100005;
int a[maxn],n,m,t,de[maxn];
int solve()
{
    int pos=0,ans=0;
    for(int i=1;i<=n;i++)
    {
        if(de[i]&1)
            pos++;
    }
    if(pos!=0 && pos!=2)
        return -1;
    for(int i=1;i<=n;i++)
    {
        de[i]=(de[i]+1)/2;
        if(de[i]&1)
            ans^=a[i];
    }
    if(pos==0)
    {
        for(int i=1;i<=n;i++)
            ans=max(ans,ans^a[i]);
    }
    return ans;
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        memset(de,0,sizeof(de));
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        for(int i=1;i<=m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            de[x]++;de[y]++;
        }
        int ans=solve();
        if(ans== -1) printf("Impossible\n");
        else printf("%d\n",ans);
    }
    return 0;
}

G Sort

Recently, Bob has just learnt a naive sorting algorithm: merge sort. Now, Bob receives a task from Alice.

Alice will give Bob NNN sorted sequences, and the iii-th sequence includes ai elements. Bob need to merge all of these sequences. He can write a program, which can merge no more than kkk sequences in one time. The cost of a merging operation is the sum of the length of these sequences. Unfortunately, Alice allows this program to use no more than T cost. So Bob wants to know the smallest kkk to make the program complete in time.
Input Format

The first line of input contains an integer t0​, the number of test cases. t0t_0t0​ test cases follow.

For each test case, the first line consists two integers N(2≤N≤100000) and T(∑i=1Nai<T<231).

In the next line there are NNN integers a1, a2, a3, ..., aN(∀i, 0≤ai≤1000).
Output Format

For each test cases, output the smallest kkk.
样例输入

1
5 25
1 2 3 4 5

样例输出

3

二分+优先队列

想到了二分,也想到了最后不够k个序列的单独处理,但就是没想到不够k个的应该最先处理,将小的累加,结果会更小

wa了好几次。

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#pragma GCC diagnostic error "-std=c++11"
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define esp 1e-9
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 0x3f3f3f3f
typedef long long ll;
int t,n,k,a[100005],ans[100005];
int solve(int m)
{
    priority_queue<int,vector<int>,greater<int> >q;
    int mid=(n-1)%(m-1),sum=0,cnt=0,pos=0;
    if(mid){
        mid++;
        cnt+=ans[mid];
        q.push(cnt);
    }
    for(int i=mid+1;i<=n;i++)
        q.push(a[i]);
    while(1)
    {
        sum+=q.top();
        q.pop();
        pos++;
        if(pos==m){
            cnt+=sum;
            if(cnt>k) return 1;
                if(q.size()==0){
                break;
            }
            q.push(sum);
            sum=0;
            pos=0;
        }
    }
    if(cnt>k) return 1;
    return 0;
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&k);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        sort(a+1,a+1+n);
        for(int i=1;i<=n;i++)
            ans[i]=ans[i-1]+a[i];
        int l=2,r=n;
        while(l<r)
        {
            int mid=l+r>>1;
            if(solve(mid)) l=mid+1;
            else r=mid;
        }
        printf("%d\n",r);
    }
    return 0;
}

 

posted @ 2018-08-14 19:19  十年换你一句好久不见  阅读(274)  评论(0编辑  收藏  举报