1855_Give you a Matrix

本文介绍了一种算法,用于从多个数组中选取元素,形成组合,并计算组合的和,特别关注找到和最小的k个组合。通过排序和优先队列,该算法在时间和空间效率上实现了优化。

Give you a Matrix

  • Description

    给你k个数组,每个数组有k个元素,那么如果从每个数组里面取一个数出来,将有 k^k 种组合方式,我们计算每种方式中从每个数组中取出来的数的和(即取出来的k个数的和),现在我们需要知道这k^k个组合方式中的和中最小的k个和。

  • Input

    有多个测试用例。每个测试用例的第一行包含一个整数K(2 <= K<=750)。以下的K行都包含K个正整数。这些整数,每个都不超过1,000,000。输入文件到文件尾结束。输入文件的大小不超过5MB。

  • Output

    对于每个测试用例,以非降序的方式输出这k个最小值。

  • Sample Input

3
1 8 5
9 2 5
10 7 6
2
1 1
1 2

  • Sample Output

9 10 12
2 2

  • Hint

    对于测试用例2:有四种组合方式,和分别为2,2,3,3,其中最小的2个和为2,2.

 #include <iostream>
#include <algorithm>
#include <queue>
#include <cstdio>
#include <cstring>

using namespace std;
#define N 800
int a[N][N],n,tp[N];
struct cmp
{
    bool operator()(const int&a,const int&b)
    {
        return a<b;
    }
};
typedef priority_queue<int,vector<int>,cmp> pq;
pq q;

void input()
{
    while(!q.empty())   q.pop();
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        scanf("%d",&a[i][j]);
    }
}

void solve()
{
    for(int i=0;i<n;i++)    sort(a[i],a[i]+n);
    for(int i=0;i<n;i++)    tp[i]=a[0][n-i-1];

    for(int i=1;i<n;i++)
    {
        for(int j=0;j<n;j++)    q.push(a[i][0]+tp[j]);

        for(int j=1;j<n;j++)
        {
            if(a[i][j]+tp[n-1]>=q.top()) break;

            q.push(a[i][j]+tp[n-1]);
            int t=n/(j+1)+1;
            for(int k=n-2;n-k<=t;k--)
            {
                if(a[i][j]+tp[k]>=q.top())   break;
                q.push(a[i][j]+tp[k]);
            }
        }
        while(q.size()!=n)  q.pop();
        for(int j=0;j<n;j++)
        {
            tp[j]=q.top();
            q.pop();
        }
    }
    printf("%d",tp[n-1]);
    for(int i=n-2;i>=0;i--)
    {
        printf(" %d",tp[i]);
    }
    printf("\n");
}

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        input();
        solve();
    }
    return 0;
}
#include "RGBMatrix_device.h" HUB75_port RGB_Matrix; /** * Initialization routine. * You might need to enable access to DWT registers on Cortex-M7 * DWT->LAR = 0xC5ACCE55 */ void DWT_Init(void) { if (!(CoreDebug->DEMCR & CoreDebug_DEMCR_TRCENA_Msk)) { CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; DWT->CYCCNT = 0; DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk; } } /** * Delay routine itself. * Time is in microseconds (1/1000000th of a second), not to be * confused with millisecond (1/1000th). * * No need to check an overflow. Let it just tick :) * * @param uint32_t us Number of microseconds to delay for */ void DWT_Delay(uint32_t us) // microseconds { uint32_t startTick = DWT->CYCCNT, delayTicks = us * (SystemCoreClock/1000000); while (DWT->CYCCNT - startTick < delayTicks); } void HUB75E_DelayUs(int us) { DWT_Delay(us); } /** * HUB75 Initialization. * Give the buffer address to the GUI * */ uint32_t _PM_timerSave; uint32_t freq = 0; void HUB75_show() { HAL_TIM_Base_Start_IT(&htim1); _PM_timerSave = __HAL_TIM_GET_COUNTER(&htim1); } void HUB75_Init(uint8_t width,uint8_t address_size,uint8_t bitDepth) { RGB_Matrix.tile = 1; RGB_Matrix.address_size = address_size; RGB_Matrix.column_select = 1 << RGB_Matrix.address_size - 1; RGB_Matrix.width = width; RGB_Matrix.height = (2 << RGB_Matrix.address_size) * abs(RGB_Matrix.tile); RGB_Matrix.min_Period = HUB75_MIN_PERIOD; RGB_Matrix.timer_Period = RGB_Matrix.min_Period; RGB_Matrix.bitDepth=bitDepth; RGB_Matrix.plane = RGB_Matrix.bitDepth; // Take a four-bit color. Due to processor speed issues, if it is too high, flickering will occur. } uint16_t initialRedBit = 0x0800 << 1, initialGreenBit = 0x0040 << 1, initialBlueBit = 0x0001 << 1; //Take the four higher color RGB 11110 111100 11110 void RGBMatrixWrite_565Data(uint8_t row , uint8_t plane){ if(plane == 0){ initialRedBit = 0x0800 << 1 ; initialGreenBit = 0x0040 << 1 ; initialBlueBit = 0x0001 << 1 ; } uint16_t *upperSrc, *lowerSrc; // Canvas scanline pointers int16_t srcIdx; int8_t srcInc; for (int8_t tile = abs(RGB_Matrix.tile) - 1; tile >= 0; tile--) { if(RGB_Matrix.tile < 0) { lowerSrc = RGB_Matrix.BlackImage + (tile * RGB_Matrix.width * (RGB_Matrix.height / 2)) + RGB_Matrix.width * ((RGB_Matrix.height / 2) - 1 - row); upperSrc = lowerSrc + RGB_Matrix.width * (RGB_Matrix.height / 2); srcIdx = RGB_Matrix.width - 1; srcInc = -1; } else { upperSrc = RGB_Matrix.BlackImage + (tile * RGB_Matrix.width * (RGB_Matrix.height / 2)) + (RGB_Matrix.width * row); lowerSrc = upperSrc + RGB_Matrix.width * (RGB_Matrix.height / 2); srcIdx = 0; srcInc = 1; } for(uint16_t x = 0; x < RGB_Matrix.width; x++, srcIdx += srcInc) { if(upperSrc[srcIdx] & initialRedBit) R1_GPIO_Port->BSRR = R1_Pin; else R1_GPIO_Port->BSRR = (uint32_t)R1_Pin << 16u; if(upperSrc[srcIdx] & initialGreenBit) G1_GPIO_Port->BSRR = G1_Pin; else G1_GPIO_Port->BSRR = (uint32_t)G1_Pin << 16u; if(upperSrc[srcIdx] & initialBlueBit) B1_GPIO_Port->BSRR = B1_Pin; else B1_GPIO_Port->BSRR = (uint32_t)B1_Pin << 16u; if(lowerSrc[srcIdx] & initialRedBit) R2_GPIO_Port->BSRR = R2_Pin; else R2_GPIO_Port->BSRR = (uint32_t)R2_Pin << 16u; if(lowerSrc[srcIdx] & initialGreenBit) G2_GPIO_Port->BSRR = G2_Pin; else G2_GPIO_Port->BSRR = (uint32_t)G2_Pin << 16u; if(lowerSrc[srcIdx] & initialBlueBit) B2_GPIO_Port->BSRR = B2_Pin; else B2_GPIO_Port->BSRR = (uint32_t)B2_Pin << 16u; CLK_GPIO_Port->BSRR = (uint32_t)CLK_Pin << 16u ; CLK_GPIO_Port->BSRR = CLK_Pin; } //end x }// end tile initialRedBit <<= 1; initialGreenBit <<= 1; initialBlueBit <<= 1; } //timer interrupt callback void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { if(htim->Instance == htim1.Instance) { OE_GPIO_Port->BSRR = OE_Pin; LAT_GPIO_Port->BSRR = (uint32_t)LAT_Pin << 16u; LAT_GPIO_Port->BSRR = LAT_Pin; HAL_TIM_Base_Stop_IT(&htim1); //uint8_t prevPlane = plane; //printf("prevPlane:%d\n",prevPlane); LAT_GPIO_Port->BSRR = (uint32_t)LAT_Pin << 16u; if(RGB_Matrix.plane == 0) { if(RGB_Matrix.column_select & 0x1) A_GPIO_Port->BSRR = A_Pin; else A_GPIO_Port->BSRR = (uint32_t)A_Pin << 16u; if(RGB_Matrix.column_select & 0x2) B_GPIO_Port->BSRR = B_Pin; else B_GPIO_Port->BSRR = (uint32_t)B_Pin << 16u; if(RGB_Matrix.column_select & 0x4) C_GPIO_Port->BSRR = C_Pin; else C_GPIO_Port->BSRR = (uint32_t)C_Pin << 16u; if(RGB_Matrix.column_select & 0x8) D_GPIO_Port->BSRR = D_Pin; else D_GPIO_Port->BSRR = (uint32_t)D_Pin << 16u; if(RGB_Matrix.column_select & 0x10) E_GPIO_Port->BSRR = E_Pin; else E_GPIO_Port->BSRR = (uint32_t)E_Pin << 16u; } if(++RGB_Matrix.plane >= RGB_Matrix.bitDepth) { RGB_Matrix.plane = 0; if(++RGB_Matrix.column_select >= (1 << RGB_Matrix.address_size)) { RGB_Matrix.column_select = 0; } } __HAL_TIM_SET_COUNTER(&htim1,0); htim1.Instance->ARR = RGB_Matrix.timer_Period << RGB_Matrix.plane; HAL_TIM_Base_Start_IT(&htim1); OE_GPIO_Port->BSRR = (uint32_t)OE_Pin << 16u;; RGBMatrixWrite_565Data(RGB_Matrix.column_select,RGB_Matrix.plane); if(RGB_Matrix.plane == 0) { uint32_t elapsed = __HAL_TIM_GET_COUNTER(&htim1) - _PM_timerSave; RGB_Matrix.timer_Period = ((RGB_Matrix.timer_Period * 7) + elapsed) / 8; if (RGB_Matrix.timer_Period < RGB_Matrix.min_Period) RGB_Matrix.timer_Period = RGB_Matrix.min_Period; } } } uint16_t Wheel(uint8_t WheelPos) { if(WheelPos < 16) { return ((32 - WheelPos) << 11) |(WheelPos << 5) | 0; } else if(WheelPos < 32) { WheelPos -= 16; return 0 | ((32 - WheelPos) << 5) | WheelPos; } else { WheelPos -= 32; return (WheelPos <<11 )| 0 | (32 - WheelPos); } } 解释一下代码
05-24
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值