• Baskets of Gold Coins_暴力


    Problem Description
    You are given N baskets of gold coins. The baskets are numbered from 1 to N. In all except one of the baskets, each gold coin weighs w grams. In the one exceptional basket, each gold coin weighs w-d grams. A wizard appears on the scene and takes 1 coin from Basket 1, 2 coins from Basket 2, and so on, up to and including N-1 coins from Basket N-1. He does not take any coins from Basket N. He weighs the selected coins and concludes which of the N baskets contains the lighter coins. Your mission is to emulate the wizard's computation. 

     
    Input
    The input file will consist of one or more lines; each line will contain data for one instance of the problem. More specifically, each line will contain four positive integers, separated by one blank space. The first three integers are, respectively, the numbers N, w, and d, as described above. The fourth integer is the result of weighing the selected coins. 

    N will be at least 2 and not more than 8000. The value of w will be at most 30. The value of d will be less than w. 


     
    Output
    For each instance of the problem, your program will produce one line of output, consisting of one positive integer: the number of the basket that contains lighter coins than the other baskets. 

     
    Sample Input
    10 25 8 1109 10 25 8 1045 8000 30 12 959879400
     
    Sample Output
    2 10 50
     

    【题意】给出n个数的序列,求出给定区间的逆序数

    【思路】N的范围比较小,先暴力搜出sum[1][1~n],

    再就是sum[i][j]和sum[i-1][j]差得是a[i-1]对i~j的影响

    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    using namespace std;
    const int N=1000+10;
    int sum[N][N],a[N];
    int main()
    {
        int n,q;
        while(~scanf("%d%d",&n,&q))
        {
            memset(sum,0,sizeof(sum));
            for(int i=1; i<=n; i++)
                scanf("%d",&a[i]);
            for(int i=1; i<=n; i++)
            {
                for(int j=1; j<i; j++)
                {
                    if(a[j]>a[i])
                        sum[1][i]++;
                }
                sum[1][i]+=sum[1][i-1];
            }
            for(int i=2; i<=n; i++)
            {
                int cnt=0;
                for(int j=i; j<=n; j++)
                {
                    if(a[i-1]>a[j]) cnt--;
                    sum[i][j]=sum[i-1][j]+cnt;
                }
            }
            for(int i=1; i<=q; i++)
            {
                int l,r;
                scanf("%d%d",&l,&r);
                printf("%d
    ",sum[l][r]);
            }
        }
        return 0;
    }
  • 相关阅读:
    运行期优化
    虚拟机字节码执行引擎
    虚拟机类加载机制
    类文件结构
    垃圾收集机制
    浅析Spring MVC工作机制
    TomCat系统架构
    docker安装mongo初体验
    微服务笔记--概念
    使用maven构建多模块项目_记录
  • 原文地址:https://www.cnblogs.com/iwantstrong/p/6111375.html
Copyright © 2020-2023  润新知