2018中国大学生程序设计竞赛 - 网络选拔赛
1001.
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstring> 5 #include <time.h> 6 #include <string> 7 #include <set> 8 #include <map> 9 #include <list> 10 #include <stack> 11 #include <queue> 12 #include <vector> 13 #include <bitset> 14 #include <ext/rope> 15 #include <algorithm> 16 #include <iostream> 17 using namespace std; 18 #define ll long long 19 #define minv 1e-6 20 #define inf 1e9 21 #define pi 3.1415926536 22 #define E 2.7182818284 23 const ll mod=1e9+7;//998244353 24 const int maxn=1e5+10; 25 26 priority_queue<int,vector<int>,greater<int> >s,t; 27 28 int main() 29 { 30 int T,n,i,len,v,c; 31 ll a,sum; 32 scanf("%d",&T); 33 while (T--) 34 { 35 while (!s.empty()) 36 s.pop(); 37 while (!t.empty()) 38 t.pop(); 39 sum=0; 40 41 scanf("%d",&n); 42 for (i=1;i<=n;i++) 43 { 44 scanf("%lld",&a); 45 v=inf; 46 c=0; 47 if (!s.empty() && v>s.top()) 48 { 49 c=1; 50 v=s.top(); 51 } 52 if (!t.empty() && v>t.top()) 53 { 54 c=2; 55 v=t.top(); 56 } 57 if (c==0 || a<=v) 58 t.push(a); 59 else if (c==1) 60 { 61 t.push(s.top()); 62 s.pop(); 63 s.push(a); 64 } 65 else 66 { 67 sum-=t.top(); 68 t.pop(); 69 s.push(a); 70 } 71 } 72 len=s.size(); 73 while (!s.empty()) 74 { 75 sum+=s.top(); 76 s.pop(); 77 } 78 printf("%lld %d\n",sum,len*2); 79 } 80 return 0; 81 } 82 /* 83 100 84 5 85 1 2 3 4 5 86 */
1003.
往定理上靠
观察数据的直觉。。。
#include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <time.h> #include <string> #include <set> #include <map> #include <list> #include <stack> #include <queue> #include <vector> #include <bitset> #include <ext/rope> #include <algorithm> #include <iostream> using namespace std; #define ll long long #define minv 1e-6 #define inf 1e9 #define pi 3.1415926536 #define E 2.7182818284 const ll mod=1e9+7;//998244353 const int maxn=1e5+10; int main() { int t,n,i,j; scanf("%d",&t); while (t--) { scanf("%d",&n); for (i=0;i<n;i++) { for (j=0;j<n;j++) { printf("%d",(i+j)%n); if (j==n-1) printf("\n"); else printf(" "); } } for (i=0;i<n;i++) { for (j=0;j<n;j++) { printf("%d",i*j%n); if (j==n-1) printf("\n"); else printf(" "); } } } return 0; }
1004.
费马大定理:当整数n >2时,关于x, y, z的方程 x^n + y^n = z^n 没有正整数解。
当不知道这公式时,猜,试!
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstring> 5 #include <time.h> 6 #include <string> 7 #include <set> 8 #include <map> 9 #include <list> 10 #include <stack> 11 #include <queue> 12 #include <vector> 13 #include <bitset> 14 #include <ext/rope> 15 #include <algorithm> 16 #include <iostream> 17 using namespace std; 18 #define ll long long 19 #define minv 1e-6 20 #define inf 1e9 21 #define pi 3.1415926536 22 #define E 2.7182818284 23 const ll mod=1e9+7;//998244353 24 const int maxn=1e5+10; 25 26 27 int main() 28 { 29 int t,n,a; 30 scanf("%d",&t); 31 while (t--) 32 { 33 scanf("%d%d",&n,&a); 34 if (n>2 || n==0) 35 printf("-1 -1\n"); 36 else if (n==1) 37 printf("%d %d\n",1,a+1); 38 else 39 { 40 if (a & 1) 41 printf("%d %d\n",(a*a-1)/2,(a*a+1)/2); 42 else 43 { 44 int b=a*a/2; 45 printf("%d %d\n",(b-2)/2,(b+2)/2); 46 } 47 48 } 49 } 50 return 0; 51 }
1007.
检查错误时,不妨再看一下题目
最大连续长度不超过m的子序列和: (单调队列)
study from
https://blog.csdn.net/slongle_amazing/article/details/50116313
设置初始最小/大值时,-5e18 (10^9*10^9)
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstring> 5 #include <time.h> 6 #include <string> 7 #include <set> 8 #include <map> 9 #include <list> 10 #include <stack> 11 #include <queue> 12 #include <vector> 13 #include <bitset> 14 #include <ext/rope> 15 #include <algorithm> 16 #include <iostream> 17 using namespace std; 18 #define ll long long 19 #define minv 1e-6 20 #define inf 1e9 21 #define pi 3.1415926536 22 #define E 2.7182818284 23 const ll mod=1e9+7;//998244353 24 const int maxn=1e4+10; 25 26 27 ll a[maxn],sum[maxn],f[maxn<<1],x[maxn<<1],tot[maxn<<1]; 28 29 int main() 30 { 31 ll TT,T,n,s,m,k; 32 ll _gcd,len,ci,rest; 33 ll i,j,v,head,tail,pos,maxlen,result; 34 cin>>T; 35 for (TT=1;TT<=T;TT++) 36 { 37 cin>>n>>s>>m>>k; 38 result=5e18; 39 40 _gcd=__gcd(n,k); 41 len=n/_gcd; 42 ci=m/len; 43 rest=m%len; 44 45 memset(sum,0,sizeof(sum)); 46 for (i=0;i<n;i++) 47 { 48 scanf("%lld",&a[i]); 49 sum[i%_gcd]+=a[i]; 50 } 51 52 for (i=0;i<_gcd;i++) 53 { 54 if (sum[i]<=0) 55 maxlen=min(m,len); 56 else 57 maxlen=rest; 58 if (maxlen>0) 59 { 60 pos=i; 61 tot[0]=0; 62 for (j=1;j<2*len;j++) 63 { 64 tot[j]=tot[j-1]+a[pos]; 65 pos=(pos+k)%n; 66 } 67 68 v=-5e18; 69 head=1; 70 tail=1; 71 x[1]=0; 72 for (j=1;j<len+maxlen;j++) 73 { 74 while (tail>=head && x[head]<j-maxlen) 75 head++; 76 if (j>=maxlen) 77 v=max(v,tot[j]-tot[ x[head] ]); 78 while (tail>=head && tot[ x[tail] ]>tot[j]) 79 tail--; 80 tail++; 81 x[tail]=j; 82 } 83 84 result=min(result,s-( ci*max(sum[i],0ll) + v )); 85 } 86 87 if (sum[i]>0 && ci>0) 88 { 89 maxlen=len; 90 pos=i; 91 tot[0]=0; 92 for (j=1;j<2*len;j++) 93 { 94 tot[j]=tot[j-1]+a[pos]; 95 pos=(pos+k)%n; 96 } 97 98 v=-5e18; 99 head=1; 100 tail=1; 101 x[1]=0; 102 for (j=1;j<len+maxlen;j++) 103 { 104 while (tail>=head && x[head]<j-maxlen) 105 head++; 106 if (j>=maxlen) 107 v=max(v,tot[j]-tot[ x[head] ]); 108 while (tail>=head && tot[ x[tail] ]>tot[j]) 109 tail--; 110 tail++; 111 x[tail]=j; 112 } 113 result=min(result,s-( (ci-1)*sum[i] + v )); 114 } 115 116 117 } 118 printf("Case #%lld: %lld\n",TT,max(0ll,result)); 119 } 120 return 0; 121 } 122 /* 123 100 124 6 100 5 2 125 1 2 3 4 5 6 126 127 6 100 4 2 128 1 2 3 4 10 6 129 130 6 100 3 5 131 1 2 3 4 5 6 132 133 6 100 3 3 134 1 2 3 5 4 3 135 136 6 100 3 3 137 1 6 6 10 6 6 138 139 6 100 6 1 140 1 2 3 4 5 6 141 142 1 100 10 1 143 -1 144 145 6 100 5 6 146 1 2 3 4 5 -6 147 148 6 100 100 6 149 1 2 3 4 5 -6 150 151 6 100 3 5 152 1 -1 10 -3 -5 6 153 154 6 100 4 5 155 1 -1 10 -3 -5 6 156 157 6 100 3 5 158 -1 -2 -3 -4 -5 -6 159 160 6 100 10 1 161 1 2 3 4 5 -100 162 163 6 100 10 1 164 -100 4 -2 -1 5 -100 165 166 6 100 3 1 167 1 2 3 4 5 -100 168 169 5 1000 1 1 170 40 -20 -20 30 40 171 172 5 1000 2 1 173 40 -20 -20 30 40 174 175 5 1000 3 1 176 40 -20 -20 30 40 177 178 5 1000 4 1 179 40 -20 -20 30 40 180 181 5 1000 5 1 182 40 -20 -20 30 40 183 184 5 1000 6 1 185 40 -20 -20 30 40 186 187 5 1000 7 1 188 40 -20 -20 30 40 189 190 5 1000 8 1 191 40 -20 -20 30 40 192 193 5 1000 9 1 194 40 -20 -20 30 40 195 196 5 1000 10 1 197 40 -20 -20 30 40 198 199 200 5 1000 1 1 201 35 -20 -20 25 35 202 203 5 1000 2 1 204 35 -20 -20 25 35 205 206 5 1000 3 1 207 35 -20 -20 25 35 208 209 5 1000 4 1 210 35 -20 -20 25 35 211 212 5 1000 5 1 213 35 -20 -20 25 35 214 215 5 1000 6 1 216 35 -20 -20 25 35 217 218 5 1000 7 1 219 35 -20 -20 25 35 220 221 5 1000 8 1 222 35 -20 -20 25 35 223 224 5 1000 9 1 225 35 -20 -20 25 35 226 227 5 1000 10 1 228 35 -20 -20 25 35 229 230 231 6 100 11 1 232 1 2 3 4 5 -6 233 234 6 100 12 1 235 1 2 3 4 5 -6 236 237 6 100 13 1 238 1 2 3 4 5 -6 239 240 6 100 14 1 241 1 2 3 4 5 -6 242 243 244 6 100 1 1 245 -100 3 -2 -1 5 -100 246 247 6 100 2 1 248 -100 4 -2 -1 5 -100 249 250 6 100 3 1 251 -100 4 -2 -1 5 -100 252 253 6 100 4 1 254 -100 4 -2 -1 5 -100 255 256 6 100 5 1 257 -100 4 -2 -1 5 -100 258 259 6 100 6 1 260 -100 4 -2 -1 5 -100 261 262 6 100 7 1 263 -100 4 -2 -1 5 -100 264 265 6 100 8 1 266 -100 4 -2 -1 5 -100 267 268 6 100 9 1 269 -100 4 -2 -1 5 -100 270 271 6 100 10 1 272 -100 4 -2 -1 5 -100 273 274 275 6 100 1 2 276 1 -4 7 3 -9 3 277 278 6 100 2 2 279 1 -4 7 3 -9 3 280 281 6 100 3 2 282 1 -4 7 3 -9 3 283 284 6 100 4 2 285 1 -4 7 3 -9 3 286 287 6 100 5 2 288 1 -4 7 3 -9 3 289 290 6 100 6 2 291 1 -4 7 3 -9 3 292 293 6 100 7 2 294 1 -4 7 3 -9 3 295 296 6 100 8 2 297 1 -4 7 3 -9 3 298 299 6 100 9 2 300 1 -4 7 3 -9 3 301 302 6 100 10 2 303 1 -4 7 3 -9 3 304 305 306 5 1000 9 1 307 40 -20 -10 20 40 308 309 5 1000 9 1 310 40 -20 -10 10 -5 311 312 313 6 1000 1 1 314 40 -20 -10 10 30 -60 315 316 6 1000 2 1 317 40 -20 -10 10 30 -60 318 319 6 1000 3 1 320 40 -20 -10 10 30 -60 321 322 6 1000 4 1 323 40 -20 -30 40 10 -60 324 325 6 1000 5 1 326 40 -20 -10 10 40 -30 327 328 6 1000 6 1 329 40 -20 -10 10 30 -60 330 331 6 1000 7 1 332 40 -20 -10 10 30 -60 333 334 6 1000 8 1 335 40 -20 -10 10 30 -60 336 337 6 1000 9 1 338 40 -20 -10 10 30 -60 339 340 6 1000 10 1 341 40 -20 -10 10 30 -60 342 343 6 1000 11 1 344 40 -20 -10 10 30 -60 345 346 6 1000 12 1 347 40 -20 -10 10 30 -60 348 349 */
1009.
检查错误时:
多造几个数据验证
long long!
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstring> 5 #include <time.h> 6 #include <string> 7 #include <set> 8 #include <map> 9 #include <list> 10 #include <stack> 11 #include <queue> 12 #include <vector> 13 #include <bitset> 14 #include <ext/rope> 15 #include <algorithm> 16 #include <iostream> 17 using namespace std; 18 #define ll long long 19 #define minv 1e-6 20 #define inf 1e9 21 #define pi 3.1415926536 22 #define E 2.7182818284 23 const ll mod=1e9+7;//998244353 24 const int maxn=1e5+10; 25 26 struct node 27 { 28 int d; 29 ll len; 30 node* next; 31 }*e[maxn]; 32 33 bool vis[maxn]; 34 ll tot[maxn],n,f[maxn]; 35 ll sum=0,fac[maxn]; 36 37 void dfs(int d) 38 { 39 int dd; 40 node* p=e[d]; 41 tot[d]=1ll; 42 vis[d]=1; 43 while (p) 44 { 45 dd=p->d; 46 if (!vis[dd]) 47 { 48 f[dd]=p->len; 49 dfs(dd); 50 tot[d]+=tot[dd]; 51 } 52 p=p->next; 53 } 54 sum=(sum+tot[d]*(n-tot[d])%mod *f[d]%mod)%mod; 55 } 56 57 int main() 58 { 59 node* p; 60 int i,x,y; 61 ll z; 62 f[1]=0; 63 fac[1]=1; 64 for (i=2;i<=1e5;i++) 65 fac[i]=fac[i-1]*i%mod; 66 while (~scanf("%lld",&n)) 67 { 68 sum=0; 69 for (i=1;i<=n;i++) 70 e[i]=NULL; 71 memset(vis,0,sizeof(vis)); 72 for (i=1;i<n;i++) 73 { 74 scanf("%d%d%lld",&x,&y,&z); 75 p=(node*) malloc (sizeof(node)); 76 p->d=x; 77 p->len=z; 78 p->next=e[y]; 79 e[y]=p; 80 81 p=(node*) malloc (sizeof(node)); 82 p->d=y; 83 p->len=z; 84 p->next=e[x]; 85 e[x]=p; 86 } 87 dfs(1); 88 89 printf("%lld\n",sum*2ll*fac[n-1]%mod); 90 } 91 return 0; 92 } 93 /* 94 4 95 1 2 1 96 1 3 1 97 3 4 1 98 99 4 100 1 2 2 101 1 3 1 102 3 4 1 103 104 7 105 1 2 1 106 1 3 1 107 2 4 1 108 2 5 1 109 3 6 1 110 3 7 1 111 112 5 113 1 2 1 114 2 3 1 115 3 4 1 116 4 5 1 117 118 6 119 1 2 1 120 2 3 1 121 3 4 1 122 4 5 1 123 4 6 1 124 125 6 126 1 2 1000000000 127 2 3 1000000000 128 3 4 1000000000 129 4 5 1000000000 130 4 6 1000000000 131 132 4 133 1 2 1000000000 134 1 3 1000000000 135 3 4 1000000000 136 */
1010.
用树状数组好一点
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstring> 5 #include <time.h> 6 #include <string> 7 #include <set> 8 #include <map> 9 #include <list> 10 #include <stack> 11 #include <queue> 12 #include <vector> 13 #include <bitset> 14 #include <ext/rope> 15 #include <algorithm> 16 #include <iostream> 17 using namespace std; 18 #define ll long long 19 #define minv 1e-6 20 #define inf 1e9 21 #define pi 3.1415926536 22 #define E 2.7182818284 23 const ll mod=1e9+7;//998244353 24 const int maxn=1e5+10; 25 26 struct node 27 { 28 int x,y,v; 29 }p[maxn]; 30 31 struct rec 32 { 33 int d,pos; 34 }d[maxn]; 35 int f[maxn]; 36 37 int cmp(node a,node b) 38 { 39 if (a.x==b.x) 40 return a.y>b.y;///!!! 41 else 42 return a.x<b.x; 43 } 44 45 int cmp1(rec a,rec b) 46 { 47 return a.d<b.d; 48 } 49 50 int main() 51 { 52 int t,n,i,j,value; 53 scanf("%d",&t); 54 while (t--) 55 { 56 memset(f,0,sizeof(f)); 57 scanf("%d",&n); 58 for (i=1;i<=n;i++) 59 scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].v); 60 61 for (i=1;i<=n;i++) 62 { 63 d[i].d=p[i].x; 64 d[i].pos=i; 65 } 66 sort(d+1,d+n+1,cmp1); 67 j=0;d[0].d=-1; 68 for (i=1;i<=n;i++) 69 { 70 if (d[i].d!=d[i-1].d) 71 j++; 72 p[ d[i].pos ].x=j; 73 } 74 75 for (i=1;i<=n;i++) 76 { 77 d[i].d=p[i].y; 78 d[i].pos=i; 79 } 80 sort(d+1,d+n+1,cmp1); 81 j=0;d[0].d=-1; 82 for (i=1;i<=n;i++) 83 { 84 if (d[i].d!=d[i-1].d) 85 j++; 86 p[ d[i].pos ].y=j; 87 } 88 89 sort(p+1,p+n+1,cmp); 90 p[n+1].x=-1; 91 for (i=1;i<=n;i++) 92 { 93 value=0; 94 95 j=p[i].y-1; 96 while (j>0) 97 { 98 value=max(value,f[j]); 99 j-=(j & (-j)); 100 } 101 102 j=p[i].y; 103 value=max(f[j],value+p[i].v); 104 105 while (j<=1e5) 106 { 107 f[j]=max(f[j],value); 108 j+=(j & (-j)); 109 } 110 } 111 112 value=0; 113 j=1e5; 114 while (j>0) 115 { 116 value=max(value,f[j]); 117 j-=(j & (-j)); 118 } 119 printf("%d\n",value); 120 } 121 return 0; 122 }