http://codeforces.com/contest/701
水
1 // #pragma comment(linker, "/STACK:102c000000,102c000000") 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <sstream> 6 #include <string> 7 #include <algorithm> 8 #include <list> 9 #include <map> 10 #include <vector> 11 #include <queue> 12 #include <stack> 13 #include <cmath> 14 #include <cstdlib> 15 // #include <conio.h> 16 using namespace std; 17 #define clc(a,b) memset(a,b,sizeof(a)) 18 #define inf 0x3f3f3f3f 19 #define lson l,mid,rt<<1 20 #define rson mid+1,r,rt<<1|1 21 const int N = 50010; 22 const int M = 1e6+10; 23 const int MOD = 1e9+7; 24 #define LL long long 25 #define LB long double 26 #define mi() (l+r)>>1 27 double const pi = acos(-1); 28 const double eps = 1e-8; 29 void fre() { 30 freopen("in.txt","r",stdin); 31 } 32 // inline int r() { 33 // int x=0,f=1;char ch=getchar(); 34 // while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();} 35 // while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}return x*f; 36 // } 37 struct node{ 38 int x,y; 39 node(int x_=0,int y_=0):x(x_),y(y_){} 40 bool operator < (const node & a) const{ 41 return x==a.x?y<a.y:x<a.x; 42 } 43 }p[110]; 44 int n; 45 int main(){ 46 scanf("%d",&n); 47 for(int i=1;i<=n;i++){ 48 scanf("%d",&p[i].x); 49 p[i].y=i; 50 } 51 sort(p+1,p+n+1); 52 for(int i=1;i<=n/2;i++){ 53 printf("%d %d ",p[i].y,p[n-i+1].y); 54 } 55 return 0; 56 }
题意:起始n*n的矩阵都填满,每次询问,询问的点消除当前点所在行和列的所有物品,输出每次询问后剩下的物品数
思路:如果当前行没有删除过,则现在删除n-(已经删除过的列数)
同理当前列:n-(已经删除过的行数)
如果都没有删除过:两种情况相加+1
1 // #pragma comment(linker, "/STACK:102c000000,102c000000") 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <sstream> 6 #include <string> 7 #include <algorithm> 8 #include <list> 9 #include <map> 10 #include <vector> 11 #include <queue> 12 #include <stack> 13 #include <cmath> 14 #include <cstdlib> 15 // #include <conio.h> 16 using namespace std; 17 #define clc(a,b) memset(a,b,sizeof(a)) 18 #define inf 0x3f3f3f3f 19 #define lson l,mid,rt<<1 20 #define rson mid+1,r,rt<<1|1 21 const int N = 100010; 22 const int M = 1e6+10; 23 const int MOD = 1e9+7; 24 #define LL long long 25 #define LB long double 26 #define mi() (l+r)>>1 27 double const pi = acos(-1); 28 const double eps = 1e-8; 29 void fre() { 30 freopen("in.txt","r",stdin); 31 } 32 // inline int r() { 33 // int x=0,f=1;char ch=getchar(); 34 // while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();} 35 // while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}return x*f; 36 // } 37 38 int l[N],r[N]; 39 int main(){ 40 int n,m; 41 scanf("%d%d",&n,&m); 42 int cntl=0,cntr=0; 43 LL ans; 44 ans=(LL)n*n; 45 // cout<<ans<<endl; 46 while(m--){ 47 int a,b; 48 scanf("%d%d",&a,&b); 49 if(r[a]==0&&l[b]==0){ 50 ans-=(n-cntl+n-cntr-1); 51 cntr++; 52 cntl++; 53 r[a]++; 54 l[b]++; 55 printf("%I64d ",ans); 56 continue; 57 } 58 if(r[a]==0){ 59 ans-=(n-cntl); 60 cntr++; 61 r[a]++; 62 } 63 if(l[b]==0){ 64 ans-=(n-cntr); 65 cntl++; 66 l[b]++; 67 } 68 printf("%I64d ",ans); 69 } 70 return 0; 71 }
题意:计算包含母串中所有不同字母的最小子串长度
思路:设置一个左端点,右端点,判断一下合不合适就可以了
1 // #pragma comment(linker, "/STACK:102c000000,102c000000") 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <sstream> 6 #include <string> 7 #include <algorithm> 8 #include <list> 9 #include <map> 10 #include <vector> 11 #include <queue> 12 #include <stack> 13 #include <cmath> 14 #include <cstdlib> 15 // #include <conio.h> 16 using namespace std; 17 #define clc(a,b) memset(a,b,sizeof(a)) 18 #define inf 0x3f3f3f3f 19 #define lson l,mid,rt<<1 20 #define rson mid+1,r,rt<<1|1 21 const int N = 100010; 22 const int M = 1e6+10; 23 const int MOD = 1e9+7; 24 #define LL long long 25 #define LB long double 26 #define mi() (l+r)>>1 27 double const pi = acos(-1); 28 const double eps = 1e-8; 29 void fre() { 30 freopen("in.txt","r",stdin); 31 } 32 // inline int r() { 33 // int x=0,f=1;char ch=getchar(); 34 // while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();} 35 // while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}return x*f; 36 // } 37 int m[110]; 38 int main(){ 39 int n; 40 string s; 41 scanf("%d",&n); 42 cin>>s; 43 int l=0,r=0; 44 int leng=(int)s.size(); 45 for(int i=0;i<leng;i++){ 46 if(m[s[i]]==0){ 47 r=i; 48 } 49 m[s[i]]++; 50 } 51 int ans=inf; 52 for(int i=r+1;i<leng;i++) 53 m[s[i]]--; 54 // cout<<"sdf"<<endl; 55 while(r<leng){ 56 while(m[s[l]]>1){ 57 m[s[l]]--; 58 l++; 59 } 60 ans=min(ans,r-l+1); 61 r++; 62 // cout<<r<<endl; 63 m[s[r]]++; 64 } 65 printf("%d ",ans); 66 return 0; 67 }
题意:n个人去距离为l的地方,人的速度为v1 车的速度v2 车一次最多载k个人,求最短时间
思路:一开始想错了,最短时间应该是:车空余的时间越少,不同的人坐车时间和走路时间都一样,且每个人同时到达终点。
推下公式就可以了
难点是最少时间搞不清楚
1 // #pragma comment(linker, "/STACK:102c000000,102c000000") 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <sstream> 6 #include <string> 7 #include <algorithm> 8 #include <list> 9 #include <map> 10 #include <vector> 11 #include <queue> 12 #include <stack> 13 #include <cmath> 14 #include <cstdlib> 15 // #include <conio.h> 16 using namespace std; 17 #define clc(a,b) memset(a,b,sizeof(a)) 18 #define inf 0x3f3f3f3f 19 #define lson l,mid,rt<<1 20 #define rson mid+1,r,rt<<1|1 21 const int N = 100010; 22 const int M = 1e6+10; 23 const int MOD = 1e9+7; 24 #define LL long long 25 #define LB long double 26 #define mi() (l+r)>>1 27 double const pi = acos(-1); 28 const double eps = 1e-8; 29 void fre() { 30 freopen("in.txt","r",stdin); 31 } 32 // inline int r() { 33 // int x=0,f=1;char ch=getchar(); 34 // while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();} 35 // while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}return x*f; 36 // } 37 38 int main(){ 39 double l,v1,v2; 40 int k,n; 41 scanf("%d%lf%lf%lf%d",&n,&l,&v1,&v2,&k); 42 double ans=0; 43 int p=n/k; 44 if(n%k) p++; 45 double t=l/(p*v2-(v2-v1)/(v2+v1)*v2*(p-1)); 46 ans=(l-v2*t)/v1+t; 47 printf("%.10f ",ans); 48 return 0; 49 }
题意:n点节点,n-1条边的树,规定了树上2*k的节点,使这2*k的节点两两配对,求最长长度,每条边长度为1;
思路:
1.以这2*k个节点来看,求树的重心,再求每个点到重心的距离和
2.求每条边的贡献:两个端点所涵盖规定节点数目的最小值 也就是min(a[v],2*k-a[v])
1 // #pragma comment(linker, "/STACK:102c000000,102c000000") 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <sstream> 6 #include <string> 7 #include <algorithm> 8 #include <list> 9 #include <map> 10 #include <vector> 11 #include <queue> 12 #include <stack> 13 #include <cmath> 14 #include <cstdlib> 15 // #include <conio.h> 16 using namespace std; 17 #define clc(a,b) memset(a,b,sizeof(a)) 18 #define inf 0x3f3f3f3f 19 #define lson l,mid,rt<<1 20 #define rson mid+1,r,rt<<1|1 21 const int N = 200010; 22 const int M = 1e6+10; 23 const int MOD = 1e9+7; 24 #define LL long long 25 #define LB long double 26 #define mi() (l+r)>>1 27 #define pb push_back 28 double const pi = acos(-1); 29 const double eps = 1e-8; 30 void fre() { 31 freopen("in.txt","r",stdin); 32 } 33 // inline int r() { 34 // int x=0,f=1;char ch=getchar(); 35 // while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();} 36 // while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}return x*f; 37 // } 38 int a[N]; 39 vector<int>s[N]; 40 int n,k; 41 LL ans; 42 void dfs(int u,int f){ 43 for(int i=0;i<(int)s[u].size();i++){ 44 int v=s[u][i]; 45 if(v==f) continue; 46 dfs(v,u); 47 a[u]+=a[v]; 48 ans+=min(a[v],2*k-a[v]); 49 } 50 return; 51 } 52 int main(){ 53 scanf("%d%d",&n,&k); 54 for(int i=0;i<2*k;i++){ 55 int x; 56 scanf("%d",&x); 57 a[x]=1; 58 } 59 for(int i=0;i<=n;i++) s[i].clear(); 60 for(int i=0;i<n-1;i++){ 61 int x,y; 62 scanf("%d%d",&x,&y); 63 s[x].pb(y);s[y].pb(x); 64 } 65 ans=0; 66 dfs(1,-1); 67 printf("%I64d ",ans); 68 return 0; 69 }