TCO Beijing 热身(Tian Ji's Horse Racing)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/nike0good/article/details/80461582

链接:https://www.nowcoder.com/acm/contest/126/A
来源:牛客网

齐国的大将田忌很喜欢赛马,有一回他被齐威王请来赛 k 天马。经过精心的准备,现在田忌有 n 匹速度分别为 a1, a2, …, an 的马,齐威王则有 n 匹速度分别为 b1, b2, …, bn 的马。每天要进行 n 场比赛,每场比赛田忌和齐威王分别会派出一匹马,每匹马每天只能上场一次,每场比赛的精彩程度是上场的两匹马的速度之和。为了让比赛更精彩,任意不同的两天里进行的比赛不能完全相同,也就是说,对于任意不同的两天,存在至少一匹马在这两天比赛的对手不同。你需要计算出这 k 天总计 k×n 场比赛中精彩程度最小值的最大可能值。

二分答案,对每个答案用乘法原理算出解,注意无解。

#include<bits/stdc++.h> 
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
                        For(j,m-1) cout<<a[i][j]<<' ';\
                        cout<<a[i][m]<<endl; \
                        } 
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
    int x=0,f=1; char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
    while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
    return x*f;
} 
#define MAXN (591)
int n,k;
ll a[500],b[500];
bool check(ll m) {
    int j=n+1;
    __int128 ans=1;
    For(i,n) {
        while(j>1 && a[i]+b[j-1] >=m) --j;
        int t=n+1-j;
        if(!t) return 0;
        if(t<i) return 0;
    }   
    j=n+1;  
    For(i,n) {
        while(j>1 && a[i]+b[j-1] >=m) --j;
        int t=n+1-j;
        ans*=t-i+1;
        if(ans>=k) return 1;
    }
    return 0;
}

int main()
{
//  freopen("C.in","r",stdin);
//  freopen(".out","w",stdout);
    cin>>n>>k;
    For(i,n) a[i]=read();
    For(i,n) b[i]=read();
    sort(a+1,a+1+n);
    sort(b+1,b+1+n);
    ll l=1,r=1e9*50LL,ans=l;

    while(l<=r) {
        ll m=(l+r)/2;
        if(check(m))ans=m,l=m+1;
        else r=m-1;
    }
    cout<<ans<<endl;
    return 0;
}
阅读更多

Tian Ji -- The Horse Racing

08-02

DescriptionnnHere is a famous story in Chinese history. nThat was about 2300 years ago. General Tian Ji was a high official in the country Qi. He likes to play horse racing with the king and others. nnBoth of Tian and the king have three horses in different classes, namely, regular, plus, and super. The rule is to have three rounds in a match; each of the horses must be used in one round. The winner of a single round takes two hundred silver dollars from the loser. nnBeing the most powerful man in the country, the king has so nice horses that in each class his horse is better than Tian's. As a result, each time the king takes six hundred silver dollars from Tian. nnTian Ji was not happy about that, until he met Sun Bin, one of the most famous generals in Chinese history. Using a little trick due to Sun, Tian Ji brought home two hundred silver dollars and such a grace in the next match. nnIt was a rather simple trick. Using his regular class horse race against the super class from the king, they will certainly lose that round. But then his plus beat the king's regular, and his super beat the king's plus. What a simple trick. And how do you think of Tian Ji, the high ranked official in China? nnnWere Tian Ji lives in nowadays, he will certainly laugh at himself. Even more, were he sitting in the ACM contest right now, he may discover that the horse racing problem can be simply viewed as finding the maximum matching in a bipartite graph. Draw Tian's horses on one side, and the king's horses on the other. Whenever one of Tian's horses can beat one from the king, we draw an edge between them, meaning we wish to establish this pair. Then, the problem of winning as many rounds as possible is just to find the maximum matching in this graph. If there are ties, the problem becomes more complicated, he needs to assign weights 0, 1, or -1 to all the possible edges, and find a maximum weighted perfect matching... nnHowever, the horse racing problem is a very special case of bipartite matching. The graph is decided by the speed of the horses -- a vertex of higher speed always beat a vertex of lower speed. In this case, the weighted bipartite matching algorithm is a too advanced tool to deal with the problem. nnIn this problem, you are asked to write a program to solve this special case of matching problem.nInputnnThe input consists of up to 50 test cases. Each case starts with a positive integer n ( n<=1000) on the first line, which is the number of horses on each side. The next n integers on the second line are the speeds of Tian's horses. Then the next n integers on the third line are the speeds of the king's horses. The input ends with a line that has a single `0' after the last test case.nOutputnnFor each input case, output a line containing a single number, which is the maximum money Tian Ji will get, in silver dollars.nSample Inputnn3n92 83 71n95 87 74n2n20 20n20 20n2n20 19n22 18n0nSample Outputnn200n0n0

没有更多推荐了,返回首页