2022-04-28每日刷题打卡

2022-04-28每日刷题打卡

代码源——每日一题

国家铁路 - 题目 - Daimayuan Online Judge
题目描述

dls的算竞王国可以被表示为一个有 H行和 W列的网格,我们让 (i,j)表示从北边第 i行和从西边第 j列的网格。最近此王国的公民希望国王能够修建一条铁路。

铁路的修建分为两个阶段:

1:从所有网格中挑选2个不同的网格,在这两个网格上分别修建一个火车站。在一个网络上修建一个火车站的代价是 Ai,j;

2:在这两个网格间修建一条铁轨,假设我们选择的网格是 (x1,y1)和(x2,y2),其代价是 C∗(|x1−x2|+|y1−y2|)

dls的愿望是希望用最少的花费去修建一条铁路造福公民们。现在请你求出这个最小花费。

题目输入

第一行输入三个整数分别代表H,W,C

接下来H行,每行W个整数,代表Ai,j

题目输出

输出一个整数代表最小花费

样例输入1
3 4 2
1 7 7 9
9 6 3 7
7 8 6 4
样例输出1
10
数据范围

2≤H,W≤1000

1≤C≤1e9

1≤Ai,j≤1e9

(今天滑铁卢了,鸽了,待我后面好好看看学会了再补 orz)

(呜呜呜呜呜呜)

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<math.h>
#include<set>
#include<numeric>
#include<string>
#include<string.h>
#include<iterator>
#include<map>
#include<unordered_map>
#include<stack>
#include<list>
#include<queue>
#include<iomanip>

#define endl '\n';
typedef long long ll;
typedef pair<ll, ll>PII;
const int N = 1e3 + 10;
int n, m, c;
ll f[N], g[N];

int main()
{
    ll ans = 1e18, n, m, c;
    cin >> n >> m >> c;
    for (int i = 0; i < n; i++)
    {
        memcpy(f, g, sizeof g);
        for (int j = m - 2; j >= 0; j--)
            f[j] = min(f[j + 1] + c, f[j]);
        for (int j = 0; j < m; j++)
        {
            cin >> g[j];
            ll tmp = g[j] + c;
            if (i > 0)
            {
                ans = min(ans, f[j] + g[j]);
                tmp = min(tmp, f[j] + c);
            }
            if (j > 0)
            {
                ans = min(ans, g[j - 1] + g[j]);
                tmp = min(tmp, g[j - 1] + c);
            }
            g[j] = tmp;
        }
    }
    cout << ans << endl;
    return 0;
}

力扣

421. 数组中两个数的最大异或值

给你一个整数数组 nums ,返回 nums[i] XOR nums[j] 的最大运算结果,其中 0 ≤ i ≤ j < n 。

进阶:你可以在 O(n) 的时间解决这个问题吗?

示例 1:

输入:nums = [3,10,5,25,2,8]
输出:28
解释:最大运算结果是 5 XOR 25 = 28.

提示:

  • 1 <= nums.length <= 2 * 104
  • 0 <= nums[i] <= 231 - 1

(虽然用上了字典树,但效率低的我想死)

01字典树经典题,先把nums的元素转成2进制的样子,比如1 2 3 4 5,转化成二进制就是0001,0010,0011,0100,0101。用它们构建完字典树后就是:
在这里插入图片描述

然后我们每次用一个元素遍历字典树,由于是异或运算,所以我们尽量要找不一样的地方,比如当前如果我们是0,那我们应该往1的方向走,如果我们是1,就该往0的方向走。要是没有不同的路给我们走,那二进制下这一位就只能是0了。最后根据得到的二进制转成10进制,同时维护一下最大值即可。一共n个元素,每个元素异或运算的复杂度为long(num[i]),所以是n*logC,相较暴力的n^2做法快了很多(但是由于我这里计算二进制过于笨拙,所以beats不是太理想)。

int f[1000000][2],cnt[1000000],idx=1;
class Solution {
public:
    void insert(string s)
    {
        int p=1;
        for(auto i:s)
        {
            int j=i-'0';
            if(f[p][j]==0)f[p][j]=++idx;
            p=f[p][j];
        }
        cnt[p]=1;
    }
    string seratch(string s)
    {
        string str;
        int p=1;
        for(auto i:s)
        {
            int j=i-'0';
            if(j==1&&f[p][0]!=0)
            {
                p=f[p][0];
                str+='1';
            }
            else if(j==0&&f[p][1]!=0)
            {
                p=f[p][1];
                str+='1';
            }
            else 
            {
                p=f[p][j];
                str+='0';
            }
        }
        return str;
    }
    int findMaximumXOR(vector<int>& nums) {
        memset(f,0,sizeof f);
        memset(cnt,0,sizeof cnt);
        idx=1;
        int n=nums.size();
        vector<string>v;
        for(int i=0;i<n;i++)
        {
            string s;
            for(int j=31;j>=0;j--)
            {
                if((nums[i]&(1<<j)))s+='1';
                else s+='0';
            }
            insert(s);
            v.push_back(s);
        }
        long long res=0;
        for(int i=0;i<n;i++)
        {
            string s=seratch(v[i]);
            long long num=0,len=s.size();
            for(long long k=len-1,j=1;k>=0;k--,j*=2)
            {
                num+=(s[k]-'0')*(j);
            }
            res=max(res,num);
        }
        return res;
    }
};
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
打开leetcode很卡 Leetcode | 算法刷题 算法学习: 由于Leetcode题量大,内容多,复习难度大,学习掌握算法的情况也不是很好。 从8.28开始转刷剑指offer,一共67道题目,其中简单题7道,中等难度题31道,较难题26道,困难题3道 1: 简单题和中等难度题(共38道题目) 2: 最优解,做笔记 3: 每天2道,隔天复习(即每天4道题) 4: Python 每日打卡: 8月: 28:1⃣剑指offer刷题 29:1⃣剑指offer刷题 30:1⃣剑指offer刷题2⃣博客网站布局搭建完毕 8月 28 29 30 31 . . . 1h练字/阅读 :check_box_with_check: :check_box_with_check: :check_box_with_check: :check_box_with_check: :cross_mark: :cross_mark: :cross_mark: 2+2算法题 :check_box_with_check: :check_box_with_check: :check_box_with_check: :check_box_with_check: :cross_mark: :cross_mark: :cross_mark: 博客网站 :check_box_with_check: :check_box_with_check: :check_box_with_check: :check_box_with_check: :cross_mark: :cross_mark: :cross_mark: 毕业课题 :cross_mark: :cross_mark: :check_box_with_check: :check_box_with_check: :cross_mark: :cross_mark: :cross_mark: 认真投入学习 9月第一周 1 2 3 4 5 6 7 1h练字/阅读 :check_box_with_check: :check_box_with_check: :cross_mark: :check_box_with_check: - - - 2+2算法题 :check_box_with_check: :check_box_with_check: :cross_mark: :check_box_with_check: - - - 博客网站 :check_box_with_check: :check_box_with_check: :check_box_with_check: :check_box_with_check: :cross_mark: :cross_mark: :cross_mark: 毕业课题 :cross_mark: :cross_mark: :cross_mark: :cross_mark: :cross_mark: :cross_mark: :cross_mark: 1号:
你可以使用 Apache POI 库来读取本地的 xlsx 文件,并实现计算每个工号每天的最晚打卡时间减去最早打卡时间的功能。下面是一个示例代码: ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileInputStream; import java.io.IOException; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.format.DateTimeFormatter; import java.util.HashMap; import java.util.Map; public class ReadXlsxFile { public static void main(String[] args) { String filePath = "path/to/xlsx/file.xlsx"; // 替换为你的xlsx文件路径 try (FileInputStream fis = new FileInputStream(filePath); Workbook workbook = new XSSFWorkbook(fis)) { Sheet sheet = workbook.getSheetAt(0); // 假设数据在第一个工作表中 Map<String, LocalDateTime[]> employeeTimes = new HashMap<>(); for (Row row : sheet) { Cell employeeIdCell = row.getCell(0); Cell timestampCell = row.getCell(1); String employeeId = employeeIdCell.getStringCellValue(); LocalDateTime timestamp = LocalDateTime.parse(timestampCell.getStringCellValue(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); LocalDate date = timestamp.toLocalDate(); LocalTime time = timestamp.toLocalTime(); LocalDateTime dateTime = LocalDateTime.of(date, time); if (employeeTimes.containsKey(employeeId)) { LocalDateTime[] times = employeeTimes.get(employeeId); times[0] = times[0].isBefore(dateTime) ? times[0] : dateTime; // 更新最晚打卡时间 times[1] = times[1].isAfter(dateTime) ? times[1] : dateTime; // 更新最早打卡时间 } else { employeeTimes.put(employeeId, new LocalDateTime[]{dateTime, dateTime}); } } for (Map.Entry<String, LocalDateTime[]> entry : employeeTimes.entrySet()) { String employeeId = entry.getKey(); LocalDateTime[] times = entry.getValue(); LocalDateTime earliestTime = times[1]; LocalDateTime latestTime = times[0]; System.out.println("Employee ID: " + employeeId); System.out.println("Earliest Time: " + earliestTime); System.out.println("Latest Time: " + latestTime); System.out.println("Time Difference: " + earliestTime.until(latestTime)); System.out.println(); } } catch (IOException e) { e.printStackTrace(); } } } ``` 请将代码中的 `path/to/xlsx/file.xlsx` 替换为你的 xlsx 文件的实际路径。此代码会使用 Apache POI 库读取 xlsx 文件的每一,将工号和对应的打卡时间提取出来,并计算每个工号每天的最晚打卡时间减去最早打卡时间,并输出到控制台。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值