Codeforces Round #305 (Div. 2)

A:检查是否回文。模拟一边。

 1
 2 #include<stdio.h>
 3 #include<math.h>
 4 #include<algorithm>
 5 #include<string.h>
 6 #include<iostream>
 7 #include<set>
 8 #include<time.h>
 9 #include<vector>
10
11 using  namespace std;
12 typedef long long ll;
13 #define N 1234567
14 #define inf 0x3f3f3f3f
15  string s;
16
17  int pan(int x,int y)
18  {
19       y--;
20       while (x<y)
21       {
22       if (s[x]!=s[y]) return 0;
23       x++;y--;
24       }
25      return 1;
26  }
27
28 int main()
29 {
30
31   int k;
32   cin>>s>>k;
33   int n=s.size();
34   if (n%k)
35   {
36       cout<<"NO";
37       return 0;
38   }
39   int tmp=n/k;
40   for (int i=0;i<n;i+=tmp)
41   {
42       int j=i+tmp;
43       if (!pan(i,j))
44       {
45         //  cout<<i<<" "<<j<<endl;
46           cout<<"NO";
47           return 0;
48       }
49   }
50     cout<<"YES";
51     return 0;
52 }

B:用m*n*m 模拟一边;

 1 #include<stdio.h>
 2 #include<math.h>
 3 #include<algorithm>
 4 #include<string.h>
 5 #include<iostream>
 6 #include<set>
 7 #include<time.h>
 8 #include<vector>
 9
10 using  namespace std;
11 typedef long long ll;
12 #define N 1234567
13 #define inf 0x3f3f3f3f
14
15 int mp[555][555];
16 int a[555];
17
18 int main()
19 {
20
21     int n,m,q;
22     cin>>n>>m>>q;
23
24     for (int i=1;i<=n;i++)
25     for (int j=1;j<=m;j++)
26     {
27         cin>>mp[i][j];
28     }
29
30     for (int i=1;i<=n;i++)
31     {
32         int tmp=0;
33         for (int j=1;j<=m;j++){
34         if (mp[i][j]) tmp++;
35         else tmp=0;
36         a[i]=max(a[i],tmp);
37         }
38     }
39
40     while (q--)
41     {
42         int x,y;
43         cin>>x>>y;
44         if (mp[x][y])
45         {
46             mp[x][y]=0;
47         }
48         else
49         {
50             mp[x][y]=1;
51         }
52         a[x]=0;
53             int tmp=0;
54             for (int j=1;j<=m;j++){
55             if (mp[x][j]) tmp++;
56             else tmp=0;
57             a[x]=max(a[x],tmp);
58             }
59
60             int ans=0;
61             for (int i=1;i<=n;i++) ans=max(ans,a[i]);
62             cout<<ans<<endl;
63     }
64
65     return 0;
66 }

C:g挂了不少。。

x=(x*h+y)%m ;如果x已经出现,那么就有循环节。

ps:有没有循环节的可能,(被这个坑了

加入a时间是第一次到A1,并且求得周期为t1,

同理:加入b时间第一次到A2,并且求得周期为t2;

所以等式于:a+t1*x=b+t2*y;

本来这个等式可以用extendgcd求,

但我枚举周期,求得。。也比较投机了

  1 #include<stdio.h>
  2 #include<math.h>
  3 #include<algorithm>
  4 #include<string.h>
  5 #include<iostream>
  6 #include<set>
  7 #include<time.h>
  8 #include<vector>
  9 #include<map>
 10
 11 using  namespace std;
 12 typedef long long ll;
 13 #define N 5223456
 14 #define inf 0x3f3f3f3f
 15 map<ll,int>mp;
 16 map<ll,int>mpp;
 17
 18 int gcd(int a,int b)
 19 {
 20     if (b==0) return a;
 21     return gcd(b,a%b);
 22 }
 23
 24 int main()
 25 {
 26
 27     int M,H1,A1,X1,Y1,H2,A2,X2,Y2;
 28     cin>>M>>H1>>A1>>X1>>Y1>>H2>>A2>>X2>>Y2;
 29     ll tmp=H1;
 30     ll num1=0;
 31     int flag=0;
 32
 33     for (int i=1;i<=N;i++)//N 是随便假设的,值要稍微大点
 34     {
 35         tmp=tmp*X1+Y1;
 36         tmp=tmp%M;
 37         num1++;
 38         if (tmp==A1&&!mp[tmp]) mp[tmp]=num1;
 39         else if (tmp==A1&&mp[tmp])
 40         {
 41             flag++;
 42             break;
 43         }
 44     }
 45
 46     tmp=H2;
 47     ll num2=0;
 48     for (int i=1;i<=N;i++)
 49     {
 50         tmp=tmp*X2+Y2;
 51         tmp=tmp%M;
 52         num2++;
 53         if (tmp==A2&&!mpp[tmp]) mpp[tmp]=num2;
 54         else if (tmp==A2&&mpp[tmp])
 55         {
 56             flag++;
 57             break;
 58         }
 59     }
 60
 61     ll tmp1=H1,tmp2=H2;
 62     int idx=0;
 63
 64     for (int i=0;i<N;i++)
 65     {
 66           tmp1=tmp1*X1+Y1;
 67           tmp1=tmp1%M;
 68           tmp2=tmp2*X2+Y2;
 69           tmp2=tmp2%M;
 70           idx++;
 71         if (tmp1==A1&&tmp2==A2)
 72         {
 73             cout<<idx;
 74             return 0;
 75         }
 76     }
 77
 78     if (flag<2)
 79     {
 80         cout<<-1;
 81         return 0;
 82     }
 83
 84
 85
 86     ll a=mp[A1];
 87     ll b=mpp[A2];//a,b-->都是开始的时间
 88     ll c=num1-a;//c,d都是周期
 89     ll d=num2-b;
 90
 91     for (int i=0;i<5234566;i++)//枚举周期
 92     {
 93         ll tmp=a+c*i-b;
 94         if (tmp<0) continue;
 95         if (tmp==0)
 96         {
 97             cout<<tmp+b;
 98             return 0;
 99         }
100         if (b&&tmp%d==0)
101         {
102             cout<<tmp+b;
103             return 0;
104         }
105     }
106     cout<<-1;
107
108     return 0;
109 }

D: DSU

题意简单:对于每个i (1<=i<=n) 求长度 为i 的最小值的最大。

解析:对于每个i,我们求出i 左边最大x ,使a[x]<a[i];记作 L[i];

同理我们求出右边最小x,      使a[y]<a[i],记作 R[i];那么a[r[i]-l[i]-1]=max(a[r[i]-l[i]-1],a[i]);-->推演一下

并且 a[x]=max(a[x+1],a[x]),继续推一下,

时间复杂度O(n);

求每个L[i],R[i]用stack,单调--;

 1 #include<stdio.h>
 2 #include<math.h>
 3 #include<algorithm>
 4 #include<string.h>
 5 #include<iostream>
 6 #include<set>
 7 #include<time.h>
 8 #include<vector>
 9
10 using  namespace std;
11 typedef long long ll;
12 #define N 1234567
13 #define inf 0x3f3f3f3f
14
15 int mp[555][555];
16 int a[555];
17
18 int main()
19 {
20
21     int n,m,q;
22     cin>>n>>m>>q;
23
24     for (int i=1;i<=n;i++)
25     for (int j=1;j<=m;j++)
26     {
27         cin>>mp[i][j];
28     }
29
30     for (int i=1;i<=n;i++)
31     {
32         int tmp=0;
33         for (int j=1;j<=m;j++){
34         if (mp[i][j]) tmp++;
35         else tmp=0;
36         a[i]=max(a[i],tmp);
37         }
38     }
39
40     while (q--)
41     {
42         int x,y;
43         cin>>x>>y;
44         if (mp[x][y])
45         {
46             mp[x][y]=0;
47         }
48         else
49         {
50             mp[x][y]=1;
51         }
52         a[x]=0;
53             int tmp=0;
54             for (int j=1;j<=m;j++){
55             if (mp[x][j]) tmp++;
56             else tmp=0;
57             a[x]=max(a[x],tmp);
58             }
59
60             int ans=0;
61             for (int i=1;i<=n;i++) ans=max(ans,a[i]);
62             cout<<ans<<endl;
63     }
64
65     return 0;
66 }
时间: 2024-07-31 14:23:37

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

暴力 Codeforces Round #305 (Div. 2) B. Mike and Fun

题目传送门 1 /* 2 暴力:每次更新该行的num[],然后暴力找出最优解就可以了:) 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <iostream> 7 #include <algorithm> 8 #include <string> 9 using namespace std; 10 11 const int MAXN = 5e2 + 10; 12 const int

set+线段树 Codeforces Round #305 (Div. 2) D. Mike and Feet

题目传送门 1 /* 2 题意:对于长度为x的子序列,每个序列存放为最小值,输出长度为x的子序列的最大值 3 set+线段树:线段树每个结点存放长度为rt的最大值,更新:先升序排序,逐个添加到set中 4 查找左右相邻的位置,更新长度为r - l - 1的最大值,感觉线段树结构体封装不错! 5 详细解释:http://blog.csdn.net/u010660276/article/details/46045777 6 其实还有其他解法,先掌握这种:) 7 */ 8 #include <cstd

数论/暴力 Codeforces Round #305 (Div. 2) C. Mike and Frog

题目传送门 1 /* 2 数论/暴力:找出第一次到a1,a2的次数,再找到完整周期p1,p2,然后以2*m为范围 3 t1,t2为各自起点开始“赛跑”,谁落后谁加一个周期,等到t1 == t2结束 4 详细解释:http://blog.csdn.net/u014357885/article/details/46044287 5 */ 6 #include <cstdio> 7 #include <algorithm> 8 #include <cstring> 9 #in

字符串处理 Codeforces Round #305 (Div. 2) A. Mike and Fax

题目传送门 1 /* 2 字符串处理:回文串是串联的,一个一个判断 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <iostream> 7 #include <algorithm> 8 #include <string> 9 using namespace std; 10 11 const int MAXN = 1e3 + 10; 12 const int INF = 0x3f3

Codeforces Round #305 (Div. 2) B

Description Mike and some bears are playing a game just for fun. Mike is the judge. All bears except Mike are standing in an n × m grid, there's exactly one bear in each cell. We denote the bear standing in column number j of row number i by (i, j).

Codeforces Round #305 (Div. 2)C---Mike and Frog(扩欧+乱搞)

Mike has a frog and a flower. His frog is named Xaniar and his flower is named Abol. Initially(at time 0), height of Xaniar is h1 and height of Abol is h2. Each second, Mike waters Abol and Xaniar. So, if height of Xaniar is h1 and height of Abol is

Codeforces Round #305 (Div. 2)D---Mike and Feet(单调栈)

Mike is the president of country What-The-Fatherland. There are n bears living in this country besides Mike. All of them are standing in a line and they are numbered from 1 to n from left to right. i-th bear is exactly ai feet high. A group of bears

Codeforces Round #305 (Div. 1) B. Mike and Feet

Mike is the president of country What-The-Fatherland. There are n bears living in this country besides Mike. All of them are standing in a line and they are numbered from 1 to n from left to right. i-th bear is exactly ai feet high. A group of bears

Codeforces Round #305 (Div. 1)

547A - Mike and Frog Solution:  先求出两种变化的第一次和第二次变化到目标的时间. 对这四个时间的具体情况需要一些特判 . 然后直接从1到2*N枚举其中一个时间的倍数,然后输出第一个满足要求的答案.  或者求出循环节后用拓展欧几里得求出最小解. 547B - Mike and Feet 题意: 给定一个长度为n(<=2*10^5)的序列,分别求出长度为(1~n)的区间的最小数的最大值. Solution: 可以先预处理以每个数为答案的最长区间. 即从每个数分别从左和

Codeforces Round #305 (Div. 2), problem: (A) Mike and Fax

#include<iostream> #include<cstdio> #include<cstring> using namespace std; char a[1000+100]; bool judge(int m,int n) { for(int i=m;i<=(m+n)/2;i++) if(a[i]!=a[m+n-i]) return 0; return 1; } int main() { int k; while(~scanf("%s"