【HDU4968】一道水題的4種艹法

Improving the GPA

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 259    Accepted Submission(s): 212


Problem Description
Xueba: Using the 4-Point Scale, my GPA is 4.0.

In fact, the AVERAGE SCORE of Xueba is calculated by the following formula:
AVERAGE SCORE = ∑(Wi * SCOREi) / ∑(Wi) 1<=i<=N

where SCOREi represents the scores of the ith course and Wi represents the credit of the corresponding course.

To simplify the problem, we assume that the credit of each course is 1. In this way, the AVERAGE SCORE is ∑(SCOREi) / N. In addition, SCOREi are all integers between 60 and 100, and we guarantee that ∑(SCOREi) can be divided by N.

In SYSU, the university usually uses the AVERAGE SCORE as the standard to represent the students’ level. However, when the students want to study further in foreign countries, other universities will use the 4-Point Scale to represent the students’ level. There are 2 ways of transforming each score to 4-Point Scale. Here is one of them. 


The student’s average GPA in the 4-Point Scale is calculated as follows:
GPA = ∑(GPAi) / N

So given one student’s AVERAGE SCORE and the number of the courses, there are many different possible values in the 4-Point Scale. Please calculate the minimum and maximum value of the GPA in the 4-Point Scale. 
 

Input
The input begins with a line containing an integer T (1 < T < 500), which denotes the number of test cases. The next T lines each contain two integers AVGSCORE, N (60 <= AVGSCORE <= 100, 1 <= N <= 10).
 

Output
For each test case, you should display the minimum and maximum value of the GPA in the 4-Point Scale in one line, accurate up to 4 decimal places. There is a space between two values.
 

Sample Input
4 75 1 75 2 75 3 75 10
 

Sample Output
3.0000 3.0000 2.7500 3.0000 2.6667 3.1667 2.4000 3.2000
Hint
In the third case, there are many possible ways to calculate the minimum value of the GPA in the 4-Point Scale. For example, Scores 78 74 73 GPA = (3.0 + 2.5 + 2.5) / 3 = 2.6667 Scores 79 78 68 GPA = (3.0 + 3.0 + 2.0) / 3 = 2.6667 Scores 84 74 67 GPA = (3.5 + 2.5 + 2.0) / 3 = 2.6667 Scores 100 64 61 GPA = (4.0 + 2.0 + 2.0) / 3 = 2.6667
 

Author
SYSU

下面代碼有來自其他博客,向博主們的聰明才智表示敬佩,以下列舉這個題的花式艹法~

艹法一:

4層循環枚舉情況數,若平均值在最大最小區間內則可行,求最優值
#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <list>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 0x3f3f3f3f
#define EPS 1e-6
#define TRUE true
#define FALSE false
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
    return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
#define N 100005
int tot;
bool check(int a, int b, int c, int d, int e)
{
    int minn = a * 85 + b * 80 + c * 75 + d * 70 + e * 60;
    int maxx = a * 100 + b * 84 + c * 79 + d * 74 + e * 69;
    if (tot >= minn && tot <= maxx)
        return true;
    else
        return false;
}
double get(int a, int b, int c, int d, int e)
{
    return a * 4.0 + b * 3.5 + c * 3.0 + d * 2.5 + e * 2.0;
}
int main()
{
#ifdef DeBUGs
    freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);
#endif
    int T;
    scanf("%d", &T);
    while (T--)
    {
        int fen, n;
        scanf("%d%d", &fen, &n);
        tot = fen * n;
        double ans1 = -INF;
        double ans2 = INF;
        for (int i = 0; i <= n; i++)
        {
            for (int j = 0; i + j <= n; j++)
            {
                for (int k = 0; i + j + k <= n; k++)
                {
                    for (int l = 0; i + j + k + l <= n; l++)
                    {
                        int d = n - i - j - k - l;
                        if (d < 0)
                            continue;
                        if (check(i, j, k, l, d))
                        {
                            ans1 = max(ans1, get(i, j, k, l, d));
                            ans2 = min(ans2, get(i, j, k, l, d));
                        }
                    }
                }
            }
        }
        printf("%.4lf %.4lf\n", ans2 / n, ans1 / n);
    }

    return 0;
}

艹法二:
DFS與上面類似,把循環寫成了dfs而已,注意退出條件k深度爲5而且num全部用完
#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <list>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 0x3f3f3f3f
#define EPS 1e-6
#define TRUE true
#define FALSE false
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
    return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
#define N 100005
double ansmi = INF;
double ansmx = -INF;
int lsco;
int mxsco[] = {100, 84, 79, 74, 69};
int misco[] = {85, 80, 75, 70, 60};
double sc[] = {4.0, 3.5, 3.0, 2.5, 2.0};
void dfs(int k, int num, int mi, int mx, double get)
{
    if (k == 5)
    {
        if(num!=0)
            return;
        if (lsco >= mi && lsco <= mx)
        {
            ansmi = min(ansmi, get);
            ansmx = max(ansmx, get);
        }
        return;
    }
    for (int i = 0; i <= num; i++)
    {
        dfs(k + 1, num - i, mi + misco[k]*i, mx + mxsco[k]*i, get + sc[k]*i);
    }
}
int main()
{
#ifdef DeBUGs
    freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);
#endif
    int T;
    scanf("%d", &T);
    while (T--)
    {
        int sco, n;
        scanf("%d%d", &sco, &n);
        lsco = sco * n;
        ansmi = INF;
        ansmx = -INF;
        dfs(0, n, 0, 0, 0.0);
        printf("%.4lf %.4lf\n", ansmi / n, ansmx / n);
    }

    return 0;
}

艹法三:
直接貪心,速度最快,這種方法只可意會不可言傳
#include<stdio.h>
int main()
{
    int t;
    while (~scanf("%d", &t))
    {
        while (t--)
        {
            double mi = 0, ma = 0; 
            int x, y;
            scanf("%d%d", &x, &y);
            int tmp = (x - 85) * y; x = (x - 69) * y;
            if (x <= 0) mi = 2.0 * y;
            else
            {
                mi = 2.0 * y;
                for (; x > 0;)
                {
                    if (x > 15)
                    {
                        mi += 2.0;
                        x -= 31;
                    }
                    else if (x > 10)
                    {
                        mi += 1.5;
                        x -= 15;
                    }
                    else if (x > 7)
                    {
                        mi += 1.0;
                        x -= 10;
                    }
                    else if (x > 0)
                    {
                        mi += 0.5;
                        x -= 5;
                    }
                }
            }
            if (tmp >= 0) ma = 4.0 * y;
            else
            {
                ma = 4.0 * y;
                for (; tmp < 0;)
                {
                    if (tmp < -15)
                    {
                        ma -= 2.0;
                        tmp += 25;
                    }
                    else if (tmp < -10)
                    {
                        ma -= 1.5;
                        tmp += 15;
                    }
                    else if (tmp < -5)
                    {
                        ma -= 1.0;
                        tmp += 10;
                    }
                    else if (tmp < 0)
                    {
                        ma -= 0.5;
                        tmp += 5;
                    }
                }
            }
            printf("%.4lf %.4lf\n", mi / y, ma / y);
        }
    }
    return 0;
}

艹法四:
把課程想象成有價值和體積的東西,最多10個,狀態10*100,遍歷取最大
DP~~
#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <list>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 0x3f3f3f3f
#define EPS 1e-6
#define TRUE true
#define FALSE false
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
    return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
#define N 105
double dp[15][1010], tab[105];
double lp[15][1010];
//dp[i][j]前i個課總分j的最大績點和
//lp[i][j]前i個課總分j的最小績點和
int main()
{
#ifdef DeBUGs
    freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);
#endif
    for (int i = 60; i <= 69; i++)
        tab[i] = 2.0;
    for (int i = 70; i <= 74; i++)
        tab[i] = 2.5;
    for (int i = 75; i <= 79; i++)
        tab[i] = 3.0;
    for (int i = 80; i <= 84; i++)
        tab[i] = 3.5;
    for (int i = 85; i <= 100; i++)
        tab[i] = 4.0;

    memset(dp, 0, sizeof(dp));
    for (int i = 60; i <= 100; i++)
        dp[1][i] = tab[i];
    for (int i = 2; i <= 10; i++)
    {
        for (int j = 60; j <= 100; j++)
        {
            for (int k = j; k <= 1000; k++)
                if (dp[i - 1][k - j] != 0)
                    dp[i][k] = max(dp[i][k], dp[i - 1][k - j] + tab[j]);
        }
    }
    for (int i = 0; i <= 10; i++)
        for (int j = 0; j <= 1000; j++)
            lp[i][j] = INF;
    for (int i = 60; i <= 100; i++)
        lp[1][i] = tab[i];
    for (int i = 2; i <= 10; i++)
        for (int j = 60; j <= 100; j++)
            for (int k = j; k <= 1000; k++)
                if (lp[i - 1][k - j] != 0)
                    lp[i][k] = min(lp[i][k], lp[i - 1][k - j] + tab[j]);
    int v, n;
    int T;
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d%d", &v, &n);
        printf("%.4lf %.4lf\n", lp[n][n * v] / n, dp[n][n * v] / n);
    }

    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章