zoukankan      html  css  js  c++  java
  • HDU_1050 && POJ_1083 Move Tables(贪心)

    很典型的贪心算法,不过要注意奇偶,比如:

    S: 4 6

    E: 5 7

    这个例子答案是多少?答案是20。

    5和6重合,所以要分两次搬完。

    从上边可以看出当E为奇数时,如果下一个的S = E+1,则要多搬一次;

    同理当E为偶数,如果下一个的S = E-1, 也要多搬一次;

    代码:

    #include <stdio.h>
    #include
    <stdlib.h>
    #define N 207
    struct node
    {
    int e;
    int s;
    }num[N];

    int cmp(const void * a, const void *b)
    {
    return (*(struct node*)a).e - (*(struct node *)b).e;
    }

    int main()
    {
    //freopen("data.in", "r", stdin);
    int t, n, i, j;
    scanf(
    "%d", &t);
    while(t--)
    {
    scanf(
    "%d", &n);
    for(i = 0; i < n; i++)
    {
    scanf(
    "%d%d", &num[i].s, &num[i].e);
    if(num[i].s > num[i].e) //chang
    {
    num[i].s
    = num[i].s + num[i].e;
    num[i].e
    = num[i].s - num[i].e;
    num[i].s
    = num[i].s - num[i].e;
    }
    }
    qsort(num, n,
    sizeof(num[0]), cmp);
    int sum = 0;
    for(i = 0; i < n; i++)
    {
    int x = 0;
    for(j = i; j < n; j++)
    {
    if(num[i].e >= num[j].s) x++;
    else if(num[i].e % 2 == 0) //"e" is even
    {
    if(num[j].s == num[i].e - 1)  
    x
    ++;
    }
    else if(num[i].e % 2) //"e" is odd
    {
    if(num[j].s == num[i].e + 1)
    x
    ++;
    }
    else break;
    }
    if(x > sum) sum = x;
    }
    printf(
    "%d\n", sum*10);
    }
    return 0;
    }
  • 相关阅读:
    js array 排序
    element 右键菜单
    Element ui tree 搜索
    threejs CameraHelper 查看照相机的观察范围
    android 错误解决
    数据结构学习--数组
    js 遍历树的层级关系的实现
    一篇文章理清WebGL绘制流程
    最基础的CSS面试题
    display:flex;多行多列布局学习
  • 原文地址:https://www.cnblogs.com/vongang/p/2144050.html
Copyright © 2011-2022 走看看