牛客练习赛30-A/C
链接:https://ac.nowcoder.com/acm/contest/216/A
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
题目描述
众所周知,小K是nowcoder的暴政苟管理,所以小K很擅长踢树,虽然本题与踢树无关
小K喜欢将日期排列成yyyy-mm-dd的形式(位数不足添零补齐)的形式,虽然这与小K只会做回文字符串这道水题无关,但小K觉得日期组成的回文串也是挺可爱的。作为一个凉心出题人,小K决定给你一个可爱的问题:给你两个日期,求这两个日期的闭区间内有多少个回文的日期(输入可能包含多组数据)
输入描述:
第一行包含一个整数T,表示有T组数据
接下来T行,每行两个“yyyy-mm-dd"形式的日期
输出描述:
输出共T行,每行输出当前数据的回文日期的个数
备注:
对于100%的数据,1 ≤ 𝑇 ≤ 10,且日期的形式一定是 YYYY-MM-DD,且输 入日期一定合法,保证答案的年份不会超过4位
一个巧妙地方案是直接枚举 mm-dd所有的格式,然后列出所有合法的日期对应的数字再判断是否在区间之内即可。。相当于把日期映射为一个整数。
我很二逼的打了个表= =
1 #include<bits/stdc++.h>
2 using namespace std;
3 #define LL long long
4 #define ULL unsigned long long
5 int f[10000][13][32];
6 int d[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
7 bool isr(int n){return n%4==0&&n%100!=0||n%400==0;}
8 int cal(int y,int m,int d){
9 char s1[15];
10 sprintf(s1,"%04d%02d%02d",y,m,d);
11 for(int i=0;i<4;++i){
12 if(s1[i]!=s1[7-i]) return 0;
13 }
14 return 1;
15 }
16 int A[]={
17 101,110,111,120,121,130,140,150,160,170,180,190,201,210,211,220,221,230,240,250,260,270,280,290,301,310,311,321,330,340,350,360,370,380,390,1001,1010,1011,1020,1021,1030,1040,1050,1060,1070,1080,1090,1101,1110,1111,1120,1121,1130,1140,1150,1160,1170,1180,1190,1201,1210,1211,1220,1221,1230,1240,1250,1260,1270,1280,1290,1301,1310,1321,1330,1350,1370,1380,2001,2010,2011,2020,2021,2030,2040,2050,2060,2070,2080,2090,2101,2110,2111,2120,2121,2130,2140,2150,2160,2170,2180,2190,2201,2210,2211,2220,2221,2230,2240,2250,2260,2270,2280,2290,3001,3010,3011,3020,3021,3030,3040,3050,3060,3070,3080,3090,3101,3110,3111,3120,3121,3130,3140,3150,3160,3170,3180,3190,3201,3210,3211,3220,3221,3230,3240,3250,3260,3270,3280,3290,4001,4010,4011,4020,4021,4030,4040,4050,4060,4070,4080,4090,4101,4110,4111,4120,4121,4130,4140,4150,4160,4170,4180,4190,4201,4210,4211,4220,4221,4230,4240,4250,4260,4270,4280,4290,5001,5010,5011,5020,5021,5030,5040,5050,5060,5070,5080,5090,5101,5110,5111,5120,5121,5130,5140,5150,5160,5170,5180,5190,5201,5210,5211,5220,5221,5230,5240,5250,5260,5270,5280,5290,6001,6010,6011,6020,6021,6030,6040,6050,6060,6070,6080,6090,6101,6110,6111,6120,6121,6130,6140,6150,6160,6170,6180,6190,6201,6210,6211,6220,6221,6230,6240,6250,6260,6270,6280,6290,7001,7010,7011,7020,7021,7030,7040,7050,7060,7070,7080,7090,7101,7110,7111,7120,7121,7130,7140,7150,7160,7170,7180,7190,7201,7210,7211,7220,7221,7230,7240,7250,7260,7270,7280,7290,8001,8010,8011,8020,8021,8030,8040,8050,8060,8070,8080,8090,8101,8110,8111,8120,8121,8130,8140,8150,8160,8170,8180,8190,8201,8210,8211,8220,8221,8230,8240,8250,8260,8270,8280,8290,9001,9010,9011,9020,9021,9030,9040,9050,9060,9070,9080,9090,9101,9110,9111,9120,9121,9130,9140,9150,9160,9170,9180,9190,9201,9210,9211,9220,9221,9230,9240,9250,9260,9270,9280,9290};
18 int B[]={
19 10,1,11,2,12,3,4,5,6,7,8,9,10,1,11,2,12,3,4,5,6,7,8,9,10,1,11,12,3,4,5,6,7,8,9,10,1,11,2,12,3,4,5,6,7,8,9,10,1,11,2,12,3,4,5,6,7,8,9,10,1,11,2,12,3,4,5,6,7,8,9,10,1,12,3,5,7,8,10,1,11,2,12,3,4,5,6,7,8,9,10,1,11,2,12,3,4,5,6,7,8,9,10,1,11,2,12,3,4,5,6,7,8,9,10,1,11,2,12,3,4,5,6,7,8,9,10,1,11,2,12,3,4,5,6,7,8,9,10,1,11,2,12,3,4,5,6,7,8,9,10,1,11,2,12,3,4,5,6,7,8,9,10,1,11,2,12,3,4,5,6,7,8,9,10,1,11,2,12,3,4,5,6,7,8,9,10,1,11,2,12,3,4,5,6,7,8,9,10,1,11,2,12,3,4,5,6,7,8,9,10,1,11,2,12,3,4,5,6,7,8,9,10,1,11,2,12,3,4,5,6,7,8,9,10,1,11,2,12,3,4,5,6,7,8,9,10,1,11,2,12,3,4,5,6,7,8,9,10,1,11,2,12,3,4,5,6,7,8,9,10,1,11,2,12,3,4,5,6,7,8,9,10,1,11,2,12,3,4,5,6,7,8,9,10,1,11,2,12,3,4,5,6,7,8,9,10,1,11,2,12,3,4,5,6,7,8,9,10,1,11,2,12,3,4,5,6,7,8,9,10,1,11,2,12,3,4,5,6,7,8,9,10,1,11,2,12,3,4,5,6,7,8,9,10,1,11,2,12,3,4,5,6,7,8,9
20 };
21 int C[]={
22 10,10,10,10,10,10,10,10,10,10,10,10,20,20,20,20,20,20,20,20,20,20,20,20,30,30,30,30,30,30,30,30,30,30,30,1,1,1,1,1,1,1,1,1,1,1,1,11,11,11,11,11,11,11,11,11,11,11,11,21,21,21,21,21,21,21,21,21,21,21,21,31,31,31,31,31,31,31,2,2,2,2,2,2,2,2,2,2,2,2,12,12,12,12,12,12,12,12,12,12,12,12,22,22,22,22,22,22,22,22,22,22,22,22,3,3,3,3,3,3,3,3,3,3,3,3,13,13,13,13,13,13,13,13,13,13,13,13,23,23,23,23,23,23,23,23,23,23,23,23,4,4,4,4,4,4,4,4,4,4,4,4,14,14,14,14,14,14,14,14,14,14,14,14,24,24,24,24,24,24,24,24,24,24,24,24,5,5,5,5,5,5,5,5,5,5,5,5,15,15,15,15,15,15,15,15,15,15,15,15,25,25,25,25,25,25,25,25,25,25,25,25,6,6,6,6,6,6,6,6,6,6,6,6,16,16,16,16,16,16,16,16,16,16,16,16,26,26,26,26,26,26,26,26,26,26,26,26,7,7,7,7,7,7,7,7,7,7,7,7,17,17,17,17,17,17,17,17,17,17,17,17,27,27,27,27,27,27,27,27,27,27,27,27,8,8,8,8,8,8,8,8,8,8,8,8,18,18,18,18,18,18,18,18,18,18,18,18,28,28,28,28,28,28,28,28,28,28,28,28,9,9,9,9,9,9,9,9,9,9,9,9,19,19,19,19,19,19,19,19,19,19,19,19,29,29,29,29,29,29,29,29,29,29,29,29
23 };
24 int N=366;
25 int cmp(int o,int a,int b,int c){
26 if(A[o]<a) return -1;
27 if(A[o]>a) return 1;
28 if(B[o]<b) return -1;
29 if(B[o]>b) return 1;
30 if(C[o]<c) return -1;
31 if(C[o]>c) return 1;
32 return 0;
33 }
34 int main()
35 {
36 int t,i,j,k;
37 while(scanf("%d",&t)!=EOF){
38 while(t--){
39 int a1,a2,b1,b2,c1,c2;
40 scanf("%d-%d-%d",&a1,&b1,&c1);
41 scanf("%d-%d-%d",&a2,&b2,&c2);
42 int ans=0;
43 for(int i=0;i<N;++i){
44 int t1=cmp(i,a1,b1,c1),t2=cmp(i,a2,b2,c2);
45 if((t1==0||t1==1)&&(t2==0||t2==-1)) ans++;
46 }
47 printf("%d\n",ans);
48 }
49 }
50 return 0;
51 }
链接:https://ac.nowcoder.com/acm/contest/216/C
来源:牛客网
题目描述
众所周知,小K是一只连NOIP2018初赛都没有过的蒟蒻,所以小K很擅长dfs序+分块树,但是本题与dfs序+分块树无关。
小K现在心态爆炸了,因为小K被一道简单的数据结构题给卡住了,希望请你来解决它,但是小K又不想太麻烦你,于是将题面进行了简化(其实是出题人懒得写题面了233333):
Bob有𝑁个点的树,每条边的长度有一个边权,现在定义𝑑𝑖𝑠(𝑖,𝑗)代表第𝑖个点到第𝑗个点的距离模2之后的结果。问有多少(𝑖,𝑗,𝑘)满足,𝑑𝑖𝑠(𝑖,𝑗) = 𝑑𝑖𝑠(𝑗,𝑘) = 𝑑𝑖𝑠(𝑖,𝑘)。
输入描述:
第一行一个整数𝑁代表点的数量。
接下来𝑁 − 1行每行三个数𝑠,𝑒,𝑑代表有一条在𝑠,𝑒之间长度为𝑑的边。
输出描述:
一行一个整数代表有多少对(𝑖,𝑗,𝑘)满足条件。
备注:
对于100%的数据,1 ≤ 𝑁 ≤ 10000,0 ≤ 𝑑 ≤ 233。
观察后会发现,假设到root距离是奇数的点有x个,为奇数的点有y个,x+y=n ,那么对于同一类的那些点他们彼此之间的距离也是这一种类型,dfs求一下奇偶数量,然后答案就是x^3+y^3,因为数可以重复。
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define LL long long 4 #define pii pair<int,int> 5 #define mp make_pair 6 #define _0 first 7 #define _1 second 8 const int maxn=10010; 9 LL f[maxn][2]; 10 vector<pii>g[maxn]; 11 void dfs(int u,int fa){ 12 f[u][0]=1; 13 for(int i=0;i<g[u].size();++i){ 14 int v=g[u][i]._0,w=g[u][i]._1; 15 if(v==fa)continue; 16 dfs(v,u); 17 if(w%2==1)f[u][0]+=f[v][1],f[u][1]+=f[v][0]; 18 else f[u][0]+=f[v][0],f[u][1]+=f[v][1]; 19 } 20 } 21 int main(){ 22 int n,u,v,w,i; 23 scanf("%d",&n); 24 for(i=1;i<n;++i){ 25 scanf("%d%d%d",&u,&v,&w); 26 g[u].push_back(mp(v,w)); 27 g[v].push_back(mp(u,w)); 28 } 29 dfs(1,0); 30 cout<<f[1][0]*f[1][0]*f[1][0]+f[1][1]*f[1][1]*f[1][1]<<endl; 31 return 0; 32 }