two point

100 篇文章 0 订阅

名字是zhw起的。
有这样一个问题:
这里写图片描述
解法:
利用两个指针,其中一个从前往后扫a数组,另一个从后往前扫b数组,先固定其中一个,另一个来扫
用可能成为最大值的数来更新答案。
具体看代码吧:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
int n,p,ans;
int a[100009],b[100009];
int main()
{
    scanf("%d%d",&n,&p);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=1;i<=n;i++) scanf("%d",&b[i]);
    int t1=1,t2=n;
    while(t1<=n&&t2>=1)
    {
        while(a[t1]+b[t2]>p&&t2>=1) t2--;
        ans=max(ans,a[t1]+b[t2]);
        t1++;
    }
    printf("%d",ans);
    return 0;
} 

这里写图片描述
解法:对a,b数组用two point,同时求c,d的最大前缀和(即前面的最大的那个数),不断更新答案。
代码:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#define M 100009
using namespace std;
int n,p,ans;
int a[M],b[M],c[M],d[M];
int main()
{
    scanf("%d%d",&n,&p);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=1;i<=n;i++) scanf("%d",&b[i]);
    for(int i=1;i<=n;i++) scanf("%d",&c[i]);
    for(int i=1;i<=n;i++) scanf("%d",&d[i]);
    for(int i=1;i<=n;i++) c[i]=max(c[i],c[i-1]),d[i]=max(d[i],d[i-1]);//前缀最大值    
    int t1=1,t2=n;
    while(t1<=n&&t2>=1)
    {
        while(a[t1]+b[t2]>p&&t2>=1) t2--;
        ans=max(ans,c[t1]+d[t2]);
        t1++;
    } 
    printf("%d",ans);
    return 0;   
}
/*
5 9
1 2 3 4 5
1 3 5 7 9
5 6 7 9 8
1 2 50 100 10
*/
static void check_efuse(void) { #if CONFIG_IDF_TARGET_ESP32 //Check if TP is burned into eFuse if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_TP) == ESP_OK) { printf("eFuse Two Point: Supported\n"); } else { printf("eFuse Two Point: NOT supported\n"); } //Check Vref is burned into eFuse if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_VREF) == ESP_OK) { printf("eFuse Vref: Supported\n"); } else { printf("eFuse Vref: NOT supported\n"); } #elif CONFIG_IDF_TARGET_ESP32S2 if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_TP) == ESP_OK) { printf("eFuse Two Point: Supported\n"); } else { printf("Cannot retrieve eFuse Two Point calibration values. Default calibration values will be used.\n"); } #else #error "This example is configured for ESP32/ESP32S2." #endif } static void print_char_val_type(esp_adc_cal_value_t val_type) { if (val_type == ESP_ADC_CAL_VAL_EFUSE_TP) { printf("Characterized using Two Point Value\n"); } else if (val_type == ESP_ADC_CAL_VAL_EFUSE_VREF) { printf("Characterized using eFuse Vref\n"); } else { printf("Characterized using Default Vref\n"); } } void app_main(void) { //Check if Two Point or Vref are burned into eFuse check_efuse(); //Configure ADC if (unit == ADC_UNIT_1) { adc1_config_width(width); adc1_config_channel_atten(channel, atten); } else { adc2_config_channel_atten((adc2_channel_t)channel, atten); } //Characterize ADC adc_chars = calloc(1, sizeof(esp_adc_cal_characteristics_t)); esp_adc_cal_value_t val_type = esp_adc_cal_characterize(unit, atten, width, DEFAULT_VREF, adc_chars); print_char_val_type(val_type); //Continuously sample ADC1 while (1) { uint32_t adc_reading = 0; //Multisampling for (int i = 0; i < NO_OF_SAMPLES; i++) { if (unit == ADC_UNIT_1) { adc_reading += adc1_get_raw((adc1_channel_t)channel); } else { int raw; adc2_get_raw((adc2_channel_t)channel, width, &raw); adc_reading += raw; } } adc_reading /= NO_OF_SAMPLES; //Convert adc_reading to voltage in mV uint32_t voltage = esp_adc_cal_raw_to_voltage(adc_reading, adc_chars); printf("Raw: %d\tVoltage: %dmV\n", adc_reading, voltage); vTaskDelay(pdMS_TO_TICKS(1000)); } }
06-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值