codeforces #345(div2)
来源:互联网 发布:深圳网络教育机构 编辑:程序博客网 时间:2024/04/23 15:54
链接:戳这里
Friends are going to play console. They have two joysticks and only one charger for them. Initially first joystick is charged at a1 percent and second one is charged at a2 percent. You can connect charger to a joystick only at the beginning of each minute. In one minute joystick either discharges by 2 percent (if not connected to a charger) or charges by 1 percent (if connected to a charger).
Game continues while both joysticks have a positive charge. Hence, if at the beginning of minute some joystick is charged by 1 percent, it has to be connected to a charger, otherwise the game stops. If some joystick completely discharges (its charge turns to 0), the game also stops.
Determine the maximum number of minutes that game can last. It is prohibited to pause the game, i. e. at each moment both joysticks should be enabled. It is allowed for joystick to be charged by more than 100 percent.
The first line of the input contains two positive integers a1 and a2 (1 ≤ a1, a2 ≤ 100), the initial charge level of first and second joystick respectively.
Output the only integer, the maximum number of minutes that the game can last. Game continues until some joystick is discharged.
3 5
6
4 4
5
In the first sample game lasts for 6 minute by using the following algorithm:
- at the beginning of the first minute connect first joystick to the charger, by the end of this minute first joystick is at 4%, second is at 3%;
- continue the game without changing charger, by the end of the second minute the first joystick is at 5%, second is at 1%;
- at the beginning of the third minute connect second joystick to the charger, after this minute the first joystick is at 3%, the second one is at 2%;
- continue the game without changing charger, by the end of the fourth minute first joystick is at 1%, second one is at 3%;
- at the beginning of the fifth minute connect first joystick to the charger, after this minute the first joystick is at 2%, the second one is at 1%;
- at the beginning of the sixth minute connect second joystick to the charger, after this minute the first joystick is at 0%, the second one is at 2%.
After that the first joystick is completely discharged and the game is stopped.
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<vector>#include <ctime>#include<queue>#include<set>#include<map>#include<stack>#include<cmath>#define mst(ss,b) memset((ss),(b),sizeof(ss))#define maxn 0x3f3f3f3f#define MAX 1000100///#pragma comment(linker, "/STACK:102400000,102400000")typedef long long ll;#define INF (1ll<<60)-1using namespace std;int a1,a2;int main(){ scanf("%d%d",&a1,&a2); int num=0; while(a1 && a2){ if(a1>=a2){ if(a1==1) break; a1-=2; a2++; }else{ if(a2==1) break; a2-=2; a1++; } num++; } printf("%d\n",num); return 0;}
There are n pictures delivered for the new exhibition. The i-th painting has beauty ai. We know that a visitor becomes happy every time he passes from a painting to a more beautiful one.
We are allowed to arranged pictures in any order. What is the maximum possible number of times the visitor may become happy while passing all pictures from first to last? In other words, we are allowed to rearrange elements of a in any order. What is the maximum possible number of indices i (1 ≤ i ≤ n - 1), such that ai + 1 > ai.
The first line of the input contains integer n (1 ≤ n ≤ 1000) — the number of painting.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000), where ai means the beauty of the i-th painting.
Print one integer — the maximum possible number of neighbouring pairs, such that ai + 1 > ai, after the optimal rearrangement.
520 30 10 50 40
4
4200 100 100 200
2
In the first sample, the optimal order is: 10, 20, 30, 40, 50.
In the second sample, the optimal order is: 100, 200, 100, 200.
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<vector>#include <ctime>#include<queue>#include<set>#include<map>#include<stack>#include<cmath>#define mst(ss,b) memset((ss),(b),sizeof(ss))#define maxn 0x3f3f3f3f#define MAX 1000100///#pragma comment(linker, "/STACK:102400000,102400000")typedef long long ll;#define INF (1ll<<60)-1using namespace std;int n;int a[10010],num[100100];int main(){ scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); num[a[i]]++; } int ans=0; while(1){ int flag=0,t=0; for(int i=1;i<=1000;i++){ if(num[i]){ if(flag==0) flag=1; else { flag=1; ans++; } num[i]--; } else t++; } ///for(int i=1;i<=1000;i++){ /// if(num[i]) cout<<i<<" "; ///} ///cout<<ans<<endl; if(t==1000) { cout<<ans<<endl; return 0; } } return 0;}
Watchmen are in a danger and Doctor Manhattan together with his friend Daniel Dreiberg should warn them as soon as possible. There are n watchmen on a plane, the i-th watchman is located at point (xi, yi).
They need to arrange a plan, but there are some difficulties on their way. As you know, Doctor Manhattan considers the distance between watchmen i and j to be |xi - xj| + |yi - yj|. Daniel, as an ordinary person, calculates the distance using the formula .
The success of the operation relies on the number of pairs (i, j) (1 ≤ i < j ≤ n), such that the distance between watchman i and watchmen j calculated by Doctor Manhattan is equal to the distance between them calculated by Daniel. You were asked to compute the number of such pairs.
The first line of the input contains the single integer n (1 ≤ n ≤ 200 000) — the number of watchmen.
Each of the following n lines contains two integers xi and yi (|xi|, |yi| ≤ 109).
Some positions may coincide.
Print the number of pairs of watchmen such that the distance between them calculated by Doctor Manhattan is equal to the distance calculated by Daniel.
31 17 51 5
2
60 00 10 2-1 10 11 1
11
In the first sample, the distance between watchman 1 and watchman 2 is equal to |1 - 7| + |1 - 5| = 10 for Doctor Manhattan and for Daniel. For pairs (1, 1), (1, 5) and (7, 5), (1, 5) Doctor Manhattan and Daniel will calculate the same distances.
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<vector>#include <ctime>#include<queue>#include<set>#include<map>#include<stack>#include<cmath>#define mst(ss,b) memset((ss),(b),sizeof(ss))#define maxn 0x3f3f3f3f#define MAX 1000100///#pragma comment(linker, "/STACK:102400000,102400000")typedef long long ll;#define INF (1ll<<60)-1using namespace std;int n;int x[200100],y[200100];map<int,int> mp1;map<int,int> mp2;struct node{ int x,y;}s[200100];bool cmp(node a,node b){ if(a.x==b.x) return a.y<b.y; return a.x<b.x;}int main(){ scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d%d",&x[i],&y[i]); mp1[x[i]]++; mp2[y[i]]++; s[i].x=x[i]; s[i].y=y[i]; } sort(s+1,s+n+1,cmp); ll ans1=0,ans2=0; int t; for(int i=1;i<n;i++){ t=1; while(s[i].x==s[i+1].x && s[i].y==s[i+1].y && i<n){ t++; i++; } ans1+=(ll)t*(t-1)/2; } int num=0; for(int i=1;i<=n;i++){ num=mp1[x[i]]; mp1[x[i]]=0; ans2+=(ll)num*(num-1)/2; num=mp2[y[i]]; mp2[y[i]]=0; ans2+=(ll)num*(num-1)/2; } printf("%I64d\n",ans2-ans1); return 0;}
Vasya's telephone contains n photos. Photo number 1 is currently opened on the phone. It is allowed to move left and right to the adjacent photo by swiping finger over the screen. If you swipe left from the first photo, you reach photo n. Similarly, by swiping right from the last photo you reach photo 1. It takes a seconds to swipe from photo to adjacent.
For each photo it is known which orientation is intended for it — horizontal or vertical. Phone is in the vertical orientation and can't be rotated. It takes b second to change orientation of the photo.
Vasya has T seconds to watch photos. He want to watch as many photos as possible. If Vasya opens the photo for the first time, he spends 1 second to notice all details in it. If photo is in the wrong orientation, he spends b seconds on rotating it before watching it. If Vasya has already opened the photo, he just skips it (so he doesn't spend any time for watching it or for changing its orientation). It is not allowed to skip unseen photos.
Help Vasya find the maximum number of photos he is able to watch during T seconds.
The first line of the input contains 4 integers n, a, b, T (1 ≤ n ≤ 5·105, 1 ≤ a, b ≤ 1000, 1 ≤ T ≤ 109) — the number of photos, time to move from a photo to adjacent, time to change orientation of a photo and time Vasya can spend for watching photo.
Second line of the input contains a string of length n containing symbols 'w' and 'h'.
If the i-th position of a string contains 'w', then the photo i should be seen in the horizontal orientation.
If the i-th position of a string contains 'h', then the photo i should be seen in vertical orientation.
Output the only integer, the maximum number of photos Vasya is able to watch during those T seconds.
4 2 3 10wwhw
2
5 2 4 13hhwhh
4
5 2 4 1000hhwhh
5
3 1 100 10whw
0
In the first sample test you can rotate the first photo (3 seconds), watch the first photo (1 seconds), move left (2 second), rotate fourth photo (3 seconds), watch fourth photo (1 second). The whole process takes exactly 10 seconds.
Note that in the last sample test the time is not enough even to watch the first photo, also you can't skip it.
题意:给定n张照片,每看一张没看过的照片需要1秒,每左右滑动一次 需要a秒,但是状态为'w'的照片需要翻转,每翻转一次需要b秒,状态为'h'的照片可以直接看。问在给定的t秒内最多看多少张照片。必须从第一张照片开始看,没看过的照片不能跳跃。
暴力枚举左边看多少张,二分右边最多可以看多少张。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<vector>#include <ctime>#include<queue>#include<set>#include<map>#include<stack>#include<cmath>#define mst(ss,b) memset((ss),(b),sizeof(ss))#define maxn 0x3f3f3f3f#define MAX 1000100///#pragma comment(linker, "/STACK:102400000,102400000")typedef long long ll;#define INF (1ll<<60)-1using namespace std;int n,m,A,B,T,TT,ans;char a[1000100],b[1000100];int v[1000100];void init(){ int t=0; for(int i=n-1;i>=1;i--){ if(b[i]=='w') t+=B; t+=A+1; v[i]=t; } t=0; for(int i=n+1;i<=m;i++){ if(b[i]=='w') t+=B; t+=A+1; v[i]=t; }}bool solve(int x){ if(v[x]>T) return false; return true;}bool solve1(int x){ if(v[x]>T) return false; return true;}int main(){ ans=0; scanf("%d%d%d%d",&n,&A,&B,&TT); scanf("%s",a+1); m=2*n-1; for(int i=1;i<=n;i++) b[i+n-1]=a[i]; for(int i=1;i<=n-1;i++) b[i]=a[i+1]; int t=0; init(); for(int i=n;i<=m;i++){ T=TT; if(b[i]=='w') t=t+B; if(i!=n) t=t+A+1; else t++; int num=i-n+1; if(t>T) break; ans=max(ans,num); T=T-t;T=T-(num-1)*A; if(T<0) continue; int l=1,r=n-1,mid,flag=n; while(l<r){ int mid=(l+r)/2; if(solve(mid)) { r=mid; flag=mid; } else l=mid+1; } ans=max(ans,n-flag+num); } t=0; for(int i=n;i>=1;i--){ T=TT; if(b[i]=='w') t+=B; if(i!=n) t+=A+1; else t++; int num=n-i+1; if(t>T) break; ans=max(ans,num); T-=t;T-=(num-1)*A; if(T<0) continue; int l=n+1,r=m,mid,flag=n; while(l<r) { mid=(l+r)/2; if(solve1(mid)) { l=mid+1; flag=mid; } else r=mid; } ans=max(ans,flag-n+num); } ans=min(ans,n); cout<<ans<<endl; return 0;}
Little Petya is now fond of data compression algorithms. He has already studied gz, bz, zip algorithms and many others. Inspired by the new knowledge, Petya is now developing the new compression algorithm which he wants to name dis.
Petya decided to compress tables. He is given a table a consisting of n rows and m columns that is filled with positive integers. He wants to build the table a' consisting of positive integers such that the relative order of the elements in each row and each column remains the same. That is, if in some row i of the initial table ai, j < ai, k, then in the resulting table a'i, j < a'i, k, and if ai, j = ai, k then a'i, j = a'i, k. Similarly, if in some column j of the initial table ai, j < ap, j then in compressed table a'i, j < a'p, j and if ai, j = ap, j then a'i, j = a'p, j.
Because large values require more space to store them, the maximum value in a' should be as small as possible.
Petya is good in theory, however, he needs your help to implement the algorithm.
The first line of the input contains two integers n and m (, the number of rows and the number of columns of the table respectively.
Each of the following n rows contain m integers ai, j (1 ≤ ai, j ≤ 109) that are the values in the table.
Output the compressed table in form of n lines each containing m integers.
If there exist several answers such that the maximum number in the compressed table is minimum possible, you are allowed to output any of them.
2 21 23 4
1 22 3
4 320 10 3050 40 3050 60 7090 80 70
2 1 35 4 35 6 79 8 7
In the first sample test, despite the fact a1, 2 ≠ a21, they are not located in the same row or column so they may become equal after the compression.
题意:给定一个n*m的矩阵,你需要精简成另外一个n*m的矩阵。
这两个矩阵必须要求每一行每一列的大小顺序不变。
题解:显然我们可以先从小到大排序,然后插进矩阵。
但是出现相同的数怎么处理。你必须要矩阵上把所有相同的数都赋值成当前第k小。
找出每一行或每一列的相同的数所在的位置,然后跑并查集将这些相同的位置拧成一团。
然后寻找所有相同位置上的坐标x,y所在的行列的最大值。
找出最大值然后加1就是所有相同的数所在的位置的值。
代码的具体实现是仿照卿神的 链接:戳这里
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<vector>#include <ctime>#include<queue>#include<set>#include<map>#include<stack>#include<cmath>#define mst(ss,b) memset((ss),(b),sizeof(ss))#define maxn 0x3f3f3f3f#define MAX 1000100///#pragma comment(linker, "/STACK:102400000,102400000")typedef long long ll;#define INF (1ll<<60)-1using namespace std;int n,m;int fa[1000100],X[1000100],Y[1000100],anw[1000100],Vx[1000100],Vy[1000100];pair<int,pair<int,int> > A[1000100];int find(int x){ if(x!=fa[x]){ fa[x]=find(fa[x]); } return fa[x];}void Union(int u,int v){ int xx=find(u); int yy=find(v); if(xx!=yy) fa[xx]=yy;}int main(){ scanf("%d%d",&n,&m); for(int i=0;i<n*m;i++){ fa[i]=i; scanf("%d",&A[i].first); A[i].second.first=i/m; A[i].second.second=i%m; } sort(A,A+n*m); int l=-1; for(int j=0;j<n*m;j++){ if(j!=n*m-1 && A[j].first==A[j+1].first) continue; for(int i=l+1;i<=j;i++){ int pos=A[i].second.first*m+A[i].second.second; int x=A[i].second.first*m,y=A[i].second.second; X[x]=pos; Y[y]=pos; } for(int i=l+1;i<=j;i++){ int pos=A[i].second.first*m+A[i].second.second; int x=A[i].second.first*m,y=A[i].second.second; Union(X[x],pos); Union(Y[y],pos); } for(int i=l+1;i<=j;i++){ int pos=A[i].second.first*m+A[i].second.second; int x=A[i].second.first*m,y=A[i].second.second; int p=find(pos); anw[p]=max(anw[p],max(Vx[x],Vy[y])+1); } for(int i=l+1;i<=j;i++){ int pos=A[i].second.first*m+A[i].second.second; int x=A[i].second.first*m,y=A[i].second.second; int p=find(pos); Vx[x]=max(Vx[x],anw[p]); Vy[y]=max(Vy[y],anw[p]); } l=j; } for(int i=0;i<n*m;i++){ int p=find(i); printf("%d ",anw[p]); if(i%m==(m-1)) printf("\n"); } return 0;}
- codeforces #345(div2)
- Codeforces #345 Div2 C Watchmen 容斥
- codeforces #78 div2 C
- codeforces 83div2
- 【codeforces #91 div2】
- codeforces 105 div2 A
- codeforces 105 div2 B
- CodeForces #120 DIV2
- Codeforces---125--div2--总结
- Codeforces-127-div2
- codeforces-div2-128
- codeforces-div2-134
- Codeforces 135 div2
- codeforces 137 div2
- Codeforces #137 div2
- Codeforces #49 div2
- codeforces 145 div2 A
- codeforces #147(div2)
- 写了一个hello.py,运行出错,提示 File "<stdin>" , line 1
- Android WebView页面加载优化
- Arduino IDE的编译执行过程解读
- sscanf用法总结
- NVIDIA Jetson TK1学习与开发(四):一些细节问题
- codeforces #345(div2)
- HDU1710 Binary Tree Traversals
- YTU 1439: 2.4.5 Fractions to Decimals 分数化小数
- Problem F: C语言习题 不等长字符串排序
- opencv颜色识别和操作
- Oracle12c中SQL优化(SQL TUNING)新特性之SQL计划指令
- 【bzoj1305】[CQOI2009]dance跳舞 最大流
- Codeforces Round #345 (Div. 2) (D. Image Preview(二分))
- NVIDIA Jetson TK1学习与开发(五):helloworld的C版本与C++版本