2021.3.2打卡

田忌赛马

大约2300年前,齐国大将田忌喜欢和国王赛马,并且约定:每赢一场,对方就要付200元。

假设已知田忌和国王的各自马匹的速度都不相同,请计算田忌最好的结果是什么。


Input

输入包含多组测试样例。
每组样例的第一行是一个整数n(n <= 1000),表示田忌和国王各自参赛的马匹数量。
接下来一行的n个整数表示田忌的马的速度,再接下来一行的n个整数表示国王的马的速度。
n为0时,表示输入数据的结束。


Output

每组数据输出一行,表示田忌最多能够赢得的金额。


Sample Input

3
92 83 71
95 87 74
2
20 19
22 18
0

Sample Output

200
0

从大到小排序,从最大的开始,每次找到能打败的记一次胜。

#include<bits/stdc++.h>
using namespace std;
#define MAX_N 1009
int main(){
    int ts[MAX_N], ks[MAX_N], n, w;
    while(scanf("%d", &n)){
        if( n == 0) break;
        for(int i = 1; i <= n; i++) scanf("%d", ts + i);
        for(int i = 1; i <= n; i++) scanf("%d", ks + i);
        sort(ts+1, ts+n+1, greater<int>()), sort(ks+1, ks+n+1, greater<int>());
        w = 0, ts[0] = 1, ks[0] = 1;
        for(int i = 1; i <= n; i++){
            if(ts[ts[0]] > ks[ks[0]]) w++, ts[0]++, ks[0]++;
            else ks[0]++;
        }
        printf("%d\n", (2*w-n)*200);
    }
    return 0;
}


时间管理大师

作为球迷,一定想看尽量多的完整的比赛,当然,作为新时代的好青年,你一定还会看一些其它的节目,假设你已经知道了所有你喜欢看的电视节目的转播时间表,你会合理安排吗?(目标是能看尽量多的完整节目)


Input

输入数据包含多个测试实例,每个测试实例的第一行只有一个整数n(n<=100),表示你喜欢看的节目的总数,然后是n行数据,每行包括两个数据Ti_s,Ti_e (1<=i<=n),分别表示第i个节目的开始和结束时间,为了简化问题,每个时间都用一个正整数表示。n=0表示输入结束,不做处理。


Output

对于每个测试实例,输出能完整看到的电视节目的个数,每个测试实例的输出占一行。


Sample Input

12
1 3
3 4
0 7
3 8
15 19
15 20
10 15
8 18
6 12
5 10
4 14
2 9
0

Sample Output

5

结束得越早,剩余的时间越多,能做的事情也越多。优先选择结束得晚的更合理。

#include <iostream>
#include <algorithm>
using namespace std;

struct TV{
    int Ti_s;
    int Ti_e;
}tv[1000];

bool cmp(struct TV a,struct TV b){
    return a.Ti_e<b.Ti_e;
}

int main(void)
{
    int n=0;
    while (cin>>n){
        if (n==0) break;

        int i=0;
        for(i=0;i<n;i++) cin>>tv[i].Ti_s>>tv[i].Ti_e;
        sort(tv,tv+n,cmp);

        int temp=tv[0].Ti_e;
        int cnt=1;
        for (i=1;i<n;i++)
        if (tv[i].Ti_s>=temp){
            cnt++;
            temp=tv[i].Ti_e;
        }
        cout<<cnt<<endl;
    }
    return 0;
}

Degree Sequence of Graph G

Wang Haiyang is a strong and optimistic Chinese youngster. Although born and brought up in the northern inland city Harbin, he has deep love and yearns for the boundless oceans. After graduation, he came to a coastal city and got a job in a marine transportation company. There, he held a position as a navigator in a freighter and began his new life.

The cargo vessel, Wang Haiyang worked on, sails among 6 ports between which exist 9 routes. At the first sight of his navigation chart, the 6 ports and 9 routes on it reminded him of Graph Theory that he studied in class at university. In the way that Leonhard Euler solved The Seven Bridges of Knoigsberg, Wang Haiyang regarded the navigation chart as a graph of Graph Theory. He considered the 6 ports as 6 nodes and 9 routes as 9 edges of the graph. The graph is illustrated as below.
在这里插入图片描述
According to Graph Theory, the number of edges related to a node is defined as Degree number of this node.

Wang Haiyang looked at the graph and thought, If arranged, the Degree numbers of all nodes of graph G can form such a sequence: 4,4,3,3,2,2, which is called the degree sequence of the graph. Of course, the degree sequence of any simple graph (according to Graph Theory, a graph without any parallel edge or ring is a simple graph) is a non-negative integer sequence?

Wang Haiyang is a thoughtful person and tends to think deeply over any scientific problem that grabs his interest. So as usual, he also gave this problem further thought, As we know, any a simple graph always corresponds with a non-negative integer sequence. But whether a non-negative integer sequence always corresponds with the degree sequence of a simple graph? That is, if given a non-negative integer sequence, are we sure that we can draw a simple graph according to it?

Let’s put forward such a definition: provided that a non-negative integer sequence is the degree sequence of a graph without any parallel edge or ring, that is, a simple graph, the sequence is draw-possible, otherwise, non-draw-possible. Now the problem faced with Wang Haiyang is how to test whether a non-negative integer sequence is draw-possible or not. Since Wang Haiyang hasn’t studied Algorithm Design course, it is difficult for him to solve such a problem. Can you help him?


Input

The first line of input contains an integer T, indicates the number of test cases. In each case, there are n+1 numbers; first is an integer n (n<1000), which indicates there are n integers in the sequence; then follow n integers, which indicate the numbers of the degree sequence.


Output

For each case, the answer should be "yes"or “no” indicating this case is “draw-possible” or “non-draw-possible”


Sample Input

2
6 4 4 3 3 2 2
4 2 1 1 1

Sample Output

yes
no

Havel定理
1. 从小到大排序
2. 最大度数n置为0,其后的n个数均减1
3. 如此反复,如果出现负数,不可以;如果所有度数全为0,可以。

#include<iostream>
#include<algorithm>
using namespace std;
#define N 1010

int a[N];

bool cmp(int a, int b){
    return a > b;
}

int main(){
    int T;
    cin >> T;
    while(T--){
        int n;
        cin >> n;
        for(int i = 0; i < n; i ++) cin >> a[i];
        sort(a, a + n, cmp);

        int flag = 0;
        for(int i = 0; i < n - 1; i ++){
            flag = 0;
            for(int j = 0; j < a[i]; j ++){
                a[i + j + 1] -= 1;
                if(a[i + j + 1] < 0){
                    cout << "no" << endl;
                    flag = 1;
                    break;
                }
            }
            if(flag) break;
            sort(a + i + 1, a + n, cmp);
        }
        if(a[n - 1] == 0 && flag == 0) cout << "yes" <<endl;
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值