Codeforces Round #366 (Div. 2) ABC

Codeforces Round #366 (Div. 2)

A I hate that I love that I hate it水题

1 #I hate that I love that I hate it
2 n = int(raw_input())
3 s = ""
4 a = ["I hate that ","I love that ", "I hate it","I love it"]
5 for i in range(n-1):
6     s+=a[i%2]
7 s += a[(n-1)%2 +2]
8 print s
View Code

B 有若干堆物体,每堆物体由若干个物体组成,一次操作可以把一堆物体分成两堆。两个人轮流操作,看谁赢。现在一开始没有物体,每次增加一个含有a[i]物体的堆,每次都要问现在这种情况下谁赢。

解:一堆含有x个东西的物体肯定要被切x-1刀,直接算出总共要被切断刀数然后模2就好了。

1 n = int(raw_input())
2 a = map(int, raw_input().split())
3 can_be_cut = 0
4 for i, x in zip(range(n), a):
5     can_be_cut += x - 1
6     print ((can_be_cut % 2)^1) + 1
View Code

C 30W个应用,30W条事件。事件有三种。

第一种,x应用发了一个推送。

第二种,查看x应用发的所有推送。

第三种,查看最老的t个推送。(不管有没有读过,都算在t里面,并不是只读最老的t个未读)

每条事件,要输出未读消息数。

 

解:

就是想办法让它不会到O(n^2),想办法让它每个推送我们只扫一次,不多扫。

首先推送我们全部按顺序记到数组里,各自有读没读过标记read[i]。

我用一个before[i]记录这个位置的推送所属的应用的上一个推送在哪个位置。再用一个last[x]记录每个应用最后一个推送到位置,再用一个already[x]记录每个应用上次全看一遍的时候最后推送到位置。这样,每次第二种操作,我就只用从last[x]一路 before[i]扫到already[x],每个推送最多只扫到一次,总复杂度O(n)。

然后考虑第三种操作,简单的一比,记一个变量firstT用来记之前的第三种操作最多包括到第几个推送。每次只从firstT开始扫,也是每个推送最多扫一遍。和上面的合起来,O(n+n) 还是 O(n)。

(我看错题,以为first t 是指最上面的t个推送,可恶,居然是最老的t个,我的锅)

代码:

  1 //#pragma comment(linker, "/STACK:102400000,102400000")
  2 /**Header!**/ //{
  3 #include<cstdio>
  4 #include<cmath>
  5 #include<iostream>
  6 #include<cstring>
  7 #include<algorithm>
  8 #include<cmath>
  9 #include<map>
 10 #include<set>
 11 #include<stack>
 12 #include<queue>
 13 using namespace std;
 14 
 15 #define MZ(array) memset(array, 0, sizeof(array))
 16 #define MF1(array) memset(array, -1, sizeof(array))
 17 #define MINF(array) memset(array, 0x3f, sizeof(array))
 18 #define REP(i,n) for(i=0;i<(n);i++)
 19 #define FOR(i,x,n) for(i=(x);i<=(n);i++)
 20 #define ROF(i,x,y) for(i=(x);i>=(y);i--)
 21 #define RD(x) scanf("%d",&x)
 22 #define RD2(x,y) scanf("%d%d",&x,&y)
 23 #define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
 24 #define RD4(x,y,z,w) scanf("%d%d%d%d",&x,&y,&z,&w)
 25 #define WN(x) printf("%d\n",x);
 26 #define RE  freopen("D.in","r",stdin)
 27 #define WE  freopen("huzhi.txt","w",stdout)
 28 #define MP make_pair
 29 #define PB push_back
 30 #define PF push_front
 31 #define PPF pop_front
 32 #define PPB pop_back
 33 #define lowbit(x) ((x)&(-x))
 34 #define cindiao ios_base::sync_with_stdio(0)
 35 #define fcout(x,y) cout << fixed << setprecision(x) << (y) << endl
 36 typedef long long LL;
 37 typedef unsigned long long ULL;
 38 typedef pair<int,int> PII;
 39 template<class T>inline void OA(const T &a,const int &st,const int &ed) {
 40     if(ed>=st)cout<<(a[st]);
 41     int i;
 42     FOR(i,st+1,ed)cout<<' '<<(a[i]);
 43     puts("");
 44 }
 45 inline void RDQ(int &x){
 46     char c;
 47     while(!isdigit(c=getchar()));
 48     x=c - '0';
 49     while(isdigit(c=getchar())) x = x*10 +c-'0';
 50 }
 51 inline void WIQ(const int &x) {
 52     if(x>=10)WIQ(x/10);
 53     putchar(x%10 + '0');
 54 }
 55 
 56 template <class T> inline T quickPow(T p,T e,const T &M) {
 57     LL ret = 1;
 58     for(; e > 0; e >>= 1) {
 59         if(e & 1) ret = (ret * p) % M;
 60         p = (p * p) % M;
 61     }
 62     return (T)ret;
 63 }
 64 template <class T> inline T gcd(const T &a,const T &b) {
 65     return (b==0) ? a : gcd(b,a%b);
 66 }
 67 template <class T> inline T niyuan(const T &a, const T &M) {
 68     return quickPow(a,M-2,M);
 69 }
 70 template <class T> inline T exgcd(const T &a,const T &b,T &x,T &y) {
 71     if (!b) {
 72         x=1,y=0;
 73         return a;
 74     }
 75     T ret=exgcd(b,a%b,x,y), t;
 76     t=x,x=y,y=t-a/b*y;
 77     return ret;
 78 }
 79 template <class T> inline T niyuanex(const T &a, const T &M) {
 80     T x,y;
 81     exgcd(a,M,x,y);
 82     return (x+M)%M;
 83 }
 84 inline LL calC(const int &n,int m,const LL &MOD) {
 85     m=(n-m>m)?m:(n-m);
 86     LL up=1,down=1;
 87     int i;
 88     for(i=1; i<=m; i++) {
 89         down*=i;
 90         down%=MOD;
 91         up*=(n-i+1);
 92         up%=MOD;
 93     }
 94     return (up*niyuanex(down, MOD))%MOD;
 95 }
 96 inline LL Lucas(const int &n,const int &m, const int &MOD) {
 97     if(m==0)return 1;
 98     return (1LL * Lucas(n/MOD, m/MOD, MOD)*calC(n%MOD, m%MOD, MOD))%MOD;
 99 }
100 const int gx[4] = {-1,0,1,0};
101 const int gy[4] = {0,1,0,-1};
102 const double PI=acos(-1.0);
103 //}
104 const double EPS=1e-10;
105 inline int sgn(double &x) {
106     if(fabs(x) < EPS)return 0;
107     if(x < 0)return -1;
108     else return 1;
109 }
110 const int INF=0x3f3f3f3f;
111 const int NINF=0x80000001;
112 const int MAXN=300011;
113 const int MAXM=100011;
114 const int MOD = 1<<30;
115 
116 
117 int n,q;
118 int before[MAXN],last[MAXN],already[MAXN];
119 bool read[MAXN];
120 int unread;
121 int cnt;
122 int firstT;
123 inline int farm(const int &no, const int &type, const int &xx) {
124     int k;
125     if(type==1) {
126         before[cnt]=last[xx];
127         last[xx]=cnt;
128         unread++;
129         cnt++;
130     } else if(type==2) {
131         already[xx] = max(already[xx], firstT-1);
132         for(int i=last[xx]; i>already[xx]; i=before[i]) {
133             if(!read[i]) {
134                 unread --;
135                 read[i]=true;
136             }
137         }
138         already[xx] = max(already[xx], last[xx]);
139     } else if(type==3) {
140         int i;
141         int ed = min(cnt-1, xx-1);
142         FOR(i,firstT,ed) {
143             if(!read[i]) {
144                 unread--;
145                 read[i]=true;
146             }
147         }
148         firstT = max(firstT, ed+1);
149     }
150     return unread;
151 }
152 
153 inline void init() {
154     MF1(last);
155     MF1(already);
156     before[0] = -1;
157     firstT=0;
158     cnt = 0;
159     unread=0;
160 }
161 
162 int main() {
163     int i,t,x;
164     init();
165     RD2(n,q);
166     REP(i,q) {
167         RDQ(t);
168         RDQ(x);
169         WIQ(farm(i,t,x));
170         puts("");
171     }
172     return 0;
173 }
View Code

 

posted @ 2016-08-08 12:35  带鱼Yuiffy  阅读(299)  评论(0编辑  收藏  举报