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

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

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 }

时间: 2024-10-03 21:54:18

Codeforces Round #366 (Div. 2) ABC的相关文章

Codeforces Round #247 (Div. 2) ABC

Codeforces Round #247 (Div. 2) http://codeforces.com/contest/431 代码均已投放:https://github.com/illuz/WayToACM/tree/master/CodeForces/431 A - Black Square 题目地址 题意: Jury玩别踩白块,游戏中有四个区域,Jury点每个区域要消耗ai的卡路里,给出踩白块的序列,问要消耗多少卡路里. 分析: 模拟水题.. 代码: /* * Author: illuz

Codeforces Round #451 (Div. 2) ABC

A. Rounding Vasya has a non-negative integer n. He wants to round it to nearest integer, which ends up with 0. If n already ends up with 0, Vasya considers it already rounded. For example, if n = 4722 answer is 4720. If n = 5 Vasya can round it to 0 

Codeforces Round #366 (Div. 2) C. Thor

题意: 手机里有n个应用,有三类操作. 第一类,第x个应用产生一个提醒通知 第二类,阅读了第x个应用产生的所有提醒通知,可能重复阅读已经读过的. 第三类,阅读了从头开始产生的t个提醒通知,可能重复阅读已经读过的. 输出每个操作发生后未读消息的个数. 分析: 对产生的消息进行编号,编号唯一. 未读的消息构成一个上述产生消息的子集s. 每个消息属于特定的应用,每个应用产生的消息构成队列. 这样, 1操作同时维护应用x产生的队列和整个的消息集合. 2操作读取应用x的消息队列,然后删除集合s中的这些消息

Codeforces Round #366 (Div. 2) A

Description Dr. Bruce Banner hates his enemies (like others don't). As we all know, he can barely talk when he turns into the incredible Hulk. That's why he asked you to help him to express his feelings. Hulk likes the Inception so much, and like tha

Codeforces Round #312 (Div. 2) ABC题解

[比赛链接]click here~~ A. Lala Land and Apple Trees: [题意]: AMR住在拉拉土地.拉拉土地是一个非常美丽的国家,位于坐标线.拉拉土地是与著名的苹果树越来越随处可见. 拉拉土地恰好n苹果树.树数i位于位置xi和具有人工智能的苹果就可以了增长.阿姆鲁希望从苹果树收集苹果. AMR目前维持在X =0的位置.在开始的时候,他可以选择是否去左边或右边.他会在他的方向继续下去,直到他遇见一棵苹果树,他之前没有参观.他会采取所有的苹果,然后扭转他的方向,继续走这

Codeforces Round #366 (Div. 2)

Mishka and Game Mishka and trip Thor Ant Man Black Widow

Codeforces Round #366 (Div. 2) B

Description Peter Parker wants to play a game with Dr. Octopus. The game is about cycles. Cycle is a sequence of vertices, such that first one is connected with the second, second is connected with third and so on, while the last one is connected wit

Codeforces Round #429 (Div. 2)ABC

A: 题意:n个东西,k个朋友,全部给朋友,每个朋友不可以拿同样的,问是否可行 1 #include<bits/stdc++.h> 2 using namespace std; 3 4 map<char ,int >ma; 5 int main(){ 6 int n,k; 7 cin>>n>>k; 8 string s; 9 cin>>s; 10 for(int i=0;i<n;i++){ 11 ma[s[i]]++; 12 if(ma[s

Codeforces Round #443 (Div. 2)ABC

A. Borya's Diagnosis It seems that Borya is seriously sick. He is going visit n doctors to find out the exact diagnosis. Each of the doctors needs the information about all previous visits, so Borya has to visit them in the prescribed order (i.e. Bor