B

题目链接:https://cn.vjudge.net/contest/283924#problem/B

题目大意:给你n个人的信息,让你找出一个时间,要求让你选择一天,使得这一天的前一个生日距离它最远,如果有多个一样的,有点选择10月27之后的。

AC代码:

  1 #include<iostream>
  2 #include<stack>
  3 #include<cstring>
  4 #include<iomanip>
  5 #include<stdio.h>
  6 #include<algorithm>
  7 #include<cmath>
  8 using namespace std;
  9 # define ll long long
 10 # define inf 0x3f3f3f3f
 11 # define lson l,m,rt<<1
 12 # define rson m+1,r,rt<<1|1
 13 const int maxn = 2e5+100;
 14 struct node
 15 {
 16     int m;
 17     int d;
 18     int day;
 19     bool friend operator < (node t1,node t2)
 20     {
 21         return t1.day<t2.day;
 22     }
 23 } q[maxn];
 24 char u;
 25 char str[maxn];
 26 int sto[maxn];
 27 int a[maxn];
 28 int f[14]= {31,28,31,30,31,30,31,31,30,31,30,31};
 29 int cal(int t1,int t2)
 30 {
 31     int ans=0;
 32     for(int i=0; i<t1-1; i++)
 33     {
 34         ans+=f[i];
 35     }
 36     ans+=t2;
 37     return ans;
 38 }
 39 int main()
 40 {
 41    // freopen("data1.in","r",stdin);
 42     int n,m,d;
 43     scanf("%d",&n);
 44     if(n==1)
 45     {
 46         cin>>str;
 47         cin>>q[1].m>>u>>q[1].d;
 48         int  i,tot=cal(q[1].m,q[1].d)-1;
 49         for( i=0; i<12; i++)
 50         {
 51             if(tot-f[i]>=0)
 52                 tot-=f[i];
 53             else
 54                 break;
 55         }
 56         if(tot==0)
 57         {
 58             i--;
 59             if(i==-1)
 60                 i=11;
 61             tot=f[i];
 62         }
 63         printf("%02d-%02d
",i+1,tot);
 64         return 0;
 65     }
 66     for(int i=1; i<=n; i++)
 67     {
 68         cin>>str;
 69         cin>>q[i].m>>u>>q[i].d;
 70         q[i].day=cal(q[i].m,q[i].d);
 71     }
 72     sort(q+1,q+n+1);
 73     sto[1]=365-q[n].day+q[1].day;
 74     for(int i=2; i<=n; i++)
 75     {
 76         sto[i]=q[i].day-q[i-1].day;
 77     }
 78     int maxx=0;
 79     for(int i=1; i<=n; i++)
 80     {
 81         maxx=max(maxx,sto[i]);
 82     }
 83     int num=0,id;
 84     for(int i=1; i<=n; i++)
 85     {
 86         if(maxx==sto[i])
 87         {
 88             a[++num]=i;
 89         }
 90     }
 91    // cout<<num<<endl;
 92     if(num==1)
 93     {
 94         int i,tot=sto[a[1]]+q[a[1]-1].day-1;
 95         if(a[1]==1)tot=q[1].day-1;
 96         tot=(tot+365)%365;
 97         for(i=0; i<12; i++)
 98         {
 99             if(tot-f[i]>=0)
100                 tot-=f[i];
101             else
102                 break;
103         }
104         if(tot==0)
105         {
106             i--;
107             if(i==-1)
108                 i=11;
109             tot=f[i];
110         }
111         printf("%02d-%02d
",i+1,tot);
112     }
113     else
114     {
115         int flag=0;
116         int minn1=inf;
117         int minn2=inf;
118         int pos;
119         for(int i=1; i<=num; i++)
120         {
121             int tmp=sto[a[i]]+q[a[i]-1].day-1;
122             if(tmp>365)
123                 tmp-=365;
124             if(tmp>cal(10,27))
125             {
126                 if(tmp-cal(10,27)<minn1)
127                 {
128                     minn1=tmp-cal(10,27);
129                     flag=i;
130                 }
131             }
132             else
133             {
134                 if((tmp-cal(10,27))<minn2)
135                 {
136                     minn2=tmp-cal(10,27);
137                     pos=i;
138                 }
139             }
140         }
141       //  cout<<minn1<<endl;
142         int i,tot;
143         if(minn1!=inf)
144         {
145             i,tot=sto[a[flag]]+q[a[flag]-1].day-1;
146             tot=(tot+365)%365;
147             for( i=0; i<12; i++)
148             {
149                 if(tot-f[i]>=0)
150                     tot-=f[i];
151                 else
152                     break;
153             }
154         }
155         else
156         {
157             tot=q[pos].day-1;
158             tot=(tot+365)%365;
159             for( i=0; i<12; i++)
160             {
161                 if(tot-f[i]>=0)
162                     tot-=f[i];
163                 else
164                     break;
165             }
166         }
167         if(tot==0)
168         {
169             i--;
170             if(i==-1)
171                 i=11;
172             tot=f[i];
173         }
174         printf("%02d-%02d
",i+1,tot);
175     }
176     return 0;
177 }
原文地址:https://www.cnblogs.com/letlifestop/p/10419351.html