zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    ASP.NET MVC学习系列(二)-WebAPI请求
    dynamic介绍
    C#中dynamic的正确用法 以及 typeof(DynamicSample).GetMethod("Add");
    json中jobject
    泛型类型参数及约束
    new 运算符
    内核知识第五讲.驱动框架编写,以及3环和0环通信.
    编写内核驱动加载工具
    内核知识第四讲,简单的认识内核函数.以及调试驱动技巧
    内核第三讲,进入ring0,以及编写第一个内核驱动程序.
  • 原文地址:https://www.cnblogs.com/iwantstrong/p/6111375.html
Copyright © 2011-2022 走看看