看到別人的影片講解 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
二維線段樹