AGC:D - ABS(博弈)

本文介绍了一个涉及策略选择的游戏问题,两名玩家通过轮流抽取卡片并替换手中的牌来比拼得分,目标是最小化或最大化两者的分数差值。文章提供了解决此问题的一种思路及代码实现。

D - ABS


Time limit : 2sec / Memory limit : 256MB

Score : 500 points

Problem Statement

We have a deck consisting of N cards. Each card has an integer written on it. The integer on the i-th card from the top is ai.

Two people X and Y will play a game using this deck. Initially, X has a card with Z written on it in his hand, and Y has a card with W written on it in his hand. Then, starting from X, they will alternately perform the following action:

  • Draw some number of cards from the top of the deck. Then, discard the card in his hand and keep the last drawn card instead. Here, at least one card must be drawn.

The game ends when there is no more card in the deck. The score of the game is the absolute difference of the integers written on the cards in the two players' hand.

X will play the game so that the score will be maximized, and Y will play the game so that the score will be minimized. What will be the score of the game?

Constraints

  • All input values are integers.
  • 1N2000
  • 1Z,W,ai109

Input

Input is given from Standard Input in the following format:

N Z W
a1 a2  aN

Output

Print the score.


Sample Input 1

Copy
3 100 100
10 1000 100

Sample Output 1

Copy
900

If X draws two cards first, Y will draw the last card, and the score will be |1000100|=900.


Sample Input 2

Copy
3 100 1000
10 100 100

Sample Output 2

Copy
900

If X draws all the cards first, the score will be |1000100|=900.


Sample Input 3

Copy
5 1 1
1 1 1 1 1

Sample Output 3

Copy
0

Sample Input 4

Copy
1 1 1
1000000000

Sample Output 4

Copy
999999999
题意:两个人初始手上分别有Z,W两个牌,一副N个牌在桌面上,每轮取任意>0数目的牌,将手上的牌替换成取得牌最下面那张,然后剩余的扔掉,双方轮流操作直至无牌。结果是两人手上牌的差值K,先手令K尽量大,后手令K尽量小,问结果是什么。

思路:答案是max(a[n]-a[n-1], a[n]-w),这种题的思路我一直都比较模糊,记下我目前的想法,因为先手要最大化差值,开局他可以取a[n-1]剩下a[n],或者取a[n]直接结束游戏,这两种情况是先手100%能达到的。如果先手不这样做,他取得牌点数必须>=a[n-1],否则后手会直接拿掉a[n],这样先手就违背他的最大化差值的使命了。然后同理后手就取<=a[n-1]的牌,否则将违背他的使命,这样最终肯定是分别拿到a[n]和a[n-1]的。

# include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL a[2003];
int main()
{
    int n;
    LL z, w;
    scanf("%d%lld%lld",&n,&z,&w);
    for(int i=1; i<=n; ++i) scanf("%d",&a[i]);
    if(n == 1) printf("%lld\n",llabs(a[1]-w));
    else
        printf("%lld\n",max(llabs(a[n-1]-a[n]), llabs(a[n]-w)));
    return 0;
}



<think>嗯,用户想用Python处理日志文件,提取eqid并去重,然后合并相关信息到Excel。首先得确定日志的格式,比如每行是否有特定的分隔符,或者eqid的位置在哪里。假设eqid是类似“eqid=1234”这样的字段,我需要用正则表达式来提取。然后,去重的问题。可能需要把提取到的eqid存到一个集合里,避免重复。同时,关联的日志信息要怎么合并呢?比如同一个eqid对应的不同日志行,可能需要把这些行的信息合并到Excel的一行里,或者分列显示。接下来是Python处理的部分。得用open打开日志文件,逐行读取,用正则匹配eqid。可能需要一个字典,键是eqid,值是一个列表,保存相关的日志信息。这样在去重的时候,每个eqid只保留一次,但相关的信息可以合并。写入Excel的话,用pandas库应该比较方便。可以用DataFrame来存储数据,然后导出到Excel。或者用openpyxl库直接操作Excel文件。不过pandas更简单,尤其是处理表格数据。 另外,用户提到要处理特定格式的日志数据,可能需要考虑日志文件的大小。如果文件很大,逐行读取会更高效,而不是一次性读入内存。不过如果文件不大,直接读取全部内容也没问题。 还要注意编码问题,日志文件可能是UTF-8或者其他编码,需要正确指定。处理完数据后,去重可以通过检查eqid是否已经在字典中来实现,如果不存在就添加,存在的话则合并或忽略,具体看用户需求。最后,生成的Excel表格需要包含哪些列?可能需要eqid作为一列,其他相关信息作为其他列。比如时间、日志级别、消息内容等,具体要根据日志的格式来定。可能需要用户提供日志的样例,或者假设日志的格式。比如每行可能有时间戳、eqid、其他信息。正则表达式可以写成r'eqid=(\d+)'来提取数字部分。然后,将每个eqid对应的其他信息提取出来,比如时间戳和消息内容,合并到Excel的不同列中。总结步骤:读取日志,逐行提取eqid和相关数据,存储到字典去重,最后用pandas写入Excel。需要注意异常处理,比如文件不存在,或者格式不符合预期的情况。</think>### 解决方案 以下是通过Python解析日志文件、提取eqid字段并生成Excel表格的完整流程: --- ### 1. 解析日志文件 假设日志格式如下: ```log 2023-08-20 10:00:00 [INFO] eqid=1234, message=connected, user=john 2023-08-20 10:00:05 [ERROR] eqid=5678, message=timeout, user=alice 2023-08-20 10:00:10 [INFO] eqid=1234, message=disconnected, user=john ``` #### 步骤: 1. **正则表达式提取eqid**:使用正则表达式`r'eqid=(\d+)'`提取字段。 2. **关联数据存储**:将每个eqid对应的日志行存入字典,实现去重和合并。 ```python import re from collections import defaultdict def parse_log(file_path): eqid_dict = defaultdict(list) pattern = re.compile(r'eqid=(\d+)') with open(file_path, 'r', encoding='utf-8') as f: for line in f: match = pattern.search(line) if match: eqid = match.group(1) eqid_dict[eqid].append(line.strip()) # 存储原始日志行或拆分后的字段 return eqid_dict ``` --- ### 2. 数据去重与合并 若需合并同一eqid的多条日志信息(例如保留时间范围或错误统计): ```python def process_data(eqid_dict): processed = [] for eqid, logs in eqid_dict.items(): first_time = logs[0].split()[0] # 提取第一条日志时间 last_time = logs[-1].split()[0] # 提取最后一条日志时间 error_count = sum("[ERROR]" in log for log in logs) processed.append({ 'eqid': eqid, 'first_occurrence': first_time, 'last_occurrence': last_time, 'error_count': error_count, 'related_logs': '\n'.join(logs) # 合并日志到单元格 }) return processed ``` --- ### 3. 写入Excel表格 使用`pandas`库生成结构化Excel文件: ```python import pandas as pd def write_to_excel(data, output_file): df = pd.DataFrame(data) df.to_excel(output_file, index=False, columns=['eqid', 'first_occurrence', 'last_occurrence', 'error_count', 'related_logs']) ``` --- ### 完整调用示例 ```python if __name__ == "__main__": log_data = parse_log("system.log") processed_data = process_data(log_data) write_to_excel(processed_data, "output.xlsx") ``` --- ### 关键点说明 1. **正则表达式优化**:若日志字段格式复杂,可改进正则表达式(如`r'eqid=([^,]+)'`)。 2. **性能优化**:大文件建议逐行读取,避免内存溢出[^1]。 3. **Excel格式调整**:使用`openpyxl`可定制单元格宽度、颜色等格式。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值