首页 > 其他分享 >atcoder beginner 346 题解

atcoder beginner 346 题解

时间:2024-03-29 19:33:05浏览次数:18  
标签:atcoder const 题解 ll pos long 346 maxn include

 

 

看到别人的视频讲解 AtCoder Beginner Contest 346 A 至 G 題讲解 by dreamoon

 

C

如果用sort写,那么再从小到大遍历也需要写几行

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cstdbool>
#include <string>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <ctime>
#include <stack>
#include <vector>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define ll long long
#define ull unsigned long long

const ll mod_1=1e9+7;
const ll mod_2=998244353;

const double eps_1=1e-5;
const double eps_2=1e-10;

const int maxn=2e5+10;

int a[maxn];




int main()
{
    int n,i,k;
    ll sum=0;
    scanf("%d%d",&n,&k);
    for (i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    sort(a+1,a+n+1);
    sum=1ll*k*(k+1)/2;
    a[0]=0;
    for (i=1;i<=n;i++)
        if (a[i]<=k && a[i]!=a[i-1])
            sum-=a[i];
    printf("%lld",sum);
    return 0;
}

 

map也是同样的情况,是否出现过,map_1.find()!=map_1.end()

 

set的写法

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <cstdbool>
 6 #include <string>
 7 #include <algorithm>
 8 #include <iostream>
 9 #include <sstream>
10 #include <ctime>
11 #include <stack>
12 #include <vector>
13 #include <queue>
14 #include <set>
15 #include <map>
16 using namespace std;
17 #define ll long long
18 #define ull unsigned long long
19 
20 const ll mod_1=1e9+7;
21 const ll mod_2=998244353;
22 
23 const double eps_1=1e-5;
24 const double eps_2=1e-10;
25 
26 const int maxn=2e5+10;
27 
28 set<int> set_1;
29 
30 int main()
31 {
32     int n,k,i,a;
33     ll sum;
34     scanf("%d%d",&n,&k);
35     sum=k*(k+1ll)/2;
36     for (i=1;i<=n;i++)
37     {
38         scanf("%d",&a);
39         if (a<=k)
40             sum -= set_1.insert(a).second * a;
41     }
42     printf("%lld",sum);
43 
44 
45     return 0;
46 }

 

F

一个比较明显的,从这个字符a开始,后面k个字符b后的的位置(dp) + 二分。

预估场上的时间,还是有些细节容易写错,当最后剩余时间不多的时候容易慌。

这样 1 前面A-E尽快做完,这样F容易安全渡过  2 F在剩余时间少的时候,大心脏的完成。都要培养

  1 #include <cstdio>
  2 #include <cstdlib>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <cstdbool>
  6 #include <string>
  7 #include <algorithm>
  8 #include <iostream>
  9 #include <sstream>
 10 #include <ctime>
 11 #include <stack>
 12 #include <vector>
 13 #include <queue>
 14 #include <set>
 15 #include <map>
 16 using namespace std;
 17 #define ll long long
 18 #define ull unsigned long long
 19 
 20 const ll mod_1=1e9+7;
 21 const ll mod_2=998244353;
 22 
 23 const double eps_1=1e-5;
 24 const double eps_2=1e-10;
 25 
 26 const int maxn=1e5+10;
 27 
 28 
 29 
 30 char str1[maxn], str2[maxn];
 31 ll f[26][maxn], cnt[26], next_fit_pos[26][maxn], next_fit_index[26][maxn];
 32 ll s2[maxn];
 33 
 34 int main()
 35 {
 36     ll n,len1,len2,ch,i,j,k,o,pos_in_loop,cur_pos_index,xx,yy,max_length;
 37     ll l,r,mid,cur_pos;
 38     bool vis;
 39     cin>>n;
 40     scanf("%s%s",str1,str2);
 41     len1=strlen(str1);
 42     len2=strlen(str2);
 43     max_length=len1*n;
 44     memset(cnt,0,sizeof(cnt));
 45     //先加上试试
 46     memset(next_fit_pos,0,sizeof(next_fit_pos));
 47     memset(next_fit_index,0,sizeof(next_fit_index));
 48     memset(f,0,sizeof(f));
 49     memset(s2,0,sizeof(s2));
 50 
 51     for (i=0;i<len1;i++)
 52     {
 53         ch = str1[i]-'a';
 54         f[ch][ cnt[ch]++ ]=i;
 55     }
 56 
 57     for (i=0;i<len2;i++)
 58         s2[i] = str2[i]-'a';
 59 
 60     for (i=0;i<26;i++)
 61         if (cnt[i]>0)
 62         {
 63             k=0;
 64             for (j=0;j<len1;j++)
 65             {
 66                 if (j==f[i][k])
 67                 {
 68                     k++;
 69                     if (cnt[i]==k)
 70                     {
 71                         for (o=j;o<len1;o++)
 72                         {
 73                             next_fit_pos[i][o]=len1+f[i][0]-o;
 74                             next_fit_index[i][o]=0;
 75                         }
 76                         break;
 77                     }
 78                 }
 79                 next_fit_pos[i][j]=f[i][k]-j;
 80                 next_fit_index[i][j]=k;
 81             }
 82         }
 83 
 84     //binary search
 85 
 86     l=1,r=n*len1/len2;
 87 
 88     while (l<=r)
 89     {
 90         mid = (l+r)/2;
 91 
 92         //if ok
 93 
 94         vis=1;
 95 
 96         cur_pos=-1;
 97         for (i=0;i<len2;i++)
 98         {
 99             ch=s2[i];
100 
101             if (cnt[ch]==0)
102             {
103                 cout<<0;
104                 return 0;
105             }
106 
107             if (cur_pos==-1)
108             {
109                 cur_pos=f[ch][0];
110                 cur_pos_index=0;
111             }
112             else
113             {
114                 pos_in_loop=cur_pos%len1;
115                 cur_pos += next_fit_pos[ch][pos_in_loop];
116                 cur_pos_index = next_fit_index[ch][pos_in_loop];
117             }
118 
119             xx=(mid-1)/cnt[ch];
120             yy=(mid-1)%cnt[ch];
121 
122             yy+=cur_pos_index;
123             if (yy>=cnt[ch])
124             {
125                 xx++;
126                 yy-=cnt[ch];
127             }
128 
129             cur_pos = cur_pos - f[ch][cur_pos_index] + xx * len1 + f[ch][yy];
130 
131             if (cur_pos>=max_length)
132             {
133                 vis=0;
134                 break;
135             }
136         }
137 
138         if (!vis)
139             r=mid-1;
140         else
141             l=mid+1;
142     }
143 
144     cout<<r;
145 
146     return 0;
147 }
148 /*
149 3
150 aaa
151 aaa
152 
153 3
154 aaa
155 a
156 
157 3
158 aaaaa
159 aaaa
160 
161 
162 =============
163 
164 asd容易乱打
165 
166 3
167 sadasdasdsdasdaasdas
168 asd
169 
170 output 7
171 sadasdasdsdasdaasdassadasdasdsdasdaasdassadasdasdsdasdaasdas
172 
173 =============
174 
175 
176 
177 
178 
179 
180 */

 

 

 

G

二维线段树

 

标签:atcoder,const,题解,ll,pos,long,346,maxn,include
From: https://www.cnblogs.com/cmyg/p/18104479

相关文章

  • P1484 种树 题解
    P1484种树有\(n\)个坑。第\(i\)个坑种树的价值是\(c_i\),相邻坑不能同时种。可以种\(k\)颗树,求最大价值。模拟费用流,建图类似这样:中间两层结点之间有\(7\)条边,表示\(n=7\)的情况。相邻两条边,例如\(1,2\)总流入量为\(1\),\(2,3\)总流出量为\(1\),也不可能出现相......
  • Luogu P6834 梦原 题解
    原题传送门首先考虑如果树的形态确定了之后的情况:如果当前点比爸爸的值大,那么显然爸爸变成\(0\)之后,这个点需要自己被额外操作删除,贡献就是\(a[v]-a[u]\)。类似,如果比爸爸的值小,那么这个点肯定会跟爸爸一起被删除,所以贡献就是\(0\)。综上所述,一个点的贡献是\(max{a[v]-......
  • ICPC2023 杭州 题解
    M-V-DiagramSolution很显然,连续的子序列的一段肯定是包括最左边或最右边的其中一个点Code#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;constllinf=1ll<<60;intmain(){intt;cin>>t;while(t--){ intn;cin>>n;......
  • [正常题解]Acwing.5308 公路
    ​首先需要理解一个证明:​ 假设我们有三个点,前两个点价格为\(a_1,\a_2\),距离为\(v_1,\v_2\)那么就有式子:\(\frac{a_1\timesv_1}{d}+\frac{a_2\timesv_2}{d}\式①\),和式子\(\frac{a_1\timesv_1}{d}+\frac{a_1\timesv_2}{d}\式子②\)$\rightarrow\frac{1}{d}(......
  • CF1184E1题解
    CF11841E1&blog尽然想让第一条边最大且这条边在最小生成树中,那么这条边就需要尽量晚。但是假如加上一条边\(i\)可以使\(u_1\)和\(v_1\)联通并且第\(w_i\lew_1\)那么我们就会舍弃原本第一条边,使用第\(i\)条边。所以第一条边的比安全一定小于等于所有么满足上述条......
  • 2023年全国青少年信息素养大赛 第9届Python编程挑战赛北京赛区(小学组)复赛试题解析
    2023年全国青少年信息素养大赛第9届Python编程挑战赛北京赛区(小学组)复赛试题解析T1.求余数题目描述:输入一个正整数,输出这个整数除以5的余数。输入描述:输入一行一个正整数输出描述:输出这个整数除以5的余数样例1:输入:12输出:2#示例代码n=int(input())print(n%5)......
  • 【洛谷 P8738】[蓝桥杯 2020 国 C] 天干地支 题解(字符串+数学+模运算)
    [蓝桥杯2020国C]天干地支题目描述古代中国使用天干地支来记录当前的年份。天干一共有十个,分别为:甲(jiǎ)、乙(yǐ)、丙(bǐng)、丁(dīng)、戊(wù)、己(jǐ)、庚(gēng)、辛(xīn)、壬(rén)、癸(guǐ)。地支一共有十二个,分别为:子(zǐ)、丑(chǒu)、寅(yín)、卯(mǎo)、辰(chén)、巳(sì)、午(wǔ......
  • 【洛谷 P8654】[蓝桥杯 2017 国 C] 合根植物 题解(并查集)
    [蓝桥杯2017国C]合根植物题目描述w星球的一个种植园,被分成m×nm\timesnm×n个小格子(东西方向......
  • 启动应用程序出现FirewallAPI.dll找不到问题解决
    其实很多用户玩单机游戏或者安装软件的时候就出现过这种问题,如果是新手第一时间会认为是软件或游戏出错了,其实并不是这样,其主要原因就是你电脑系统的该dll文件丢失了或没有安装一些系统软件平台所需要的动态链接库,这时你可以下载这个FirewallAPI.dll文件(挑选合适的版本文件)把......
  • 启动应用程序出现fthsvc.dll找不到问题解决
    其实很多用户玩单机游戏或者安装软件的时候就出现过这种问题,如果是新手第一时间会认为是软件或游戏出错了,其实并不是这样,其主要原因就是你电脑系统的该dll文件丢失了或没有安装一些系统软件平台所需要的动态链接库,这时你可以下载这个fthsvc.dll文件(挑选合适的版本文件)把它放......