Noip模拟

来源:互联网 发布:淘宝直通车怎么样 编辑:程序博客网 时间:2024/04/29 23:20

反正不是我们学校的题,随便写题解

Passward

你来到了一个庙前,庙牌上有一个仅包含小写字母的字符串 s。

传说打开庙门的密码是这个字符串的一个子串 t,并且 t 既是 s 的前缀又是 s 的后缀并且还在 s 的中间位置出现过一次。

如果存在这样的串,请你输出这个串,如有多个满足条件的串,输出最长的那一个。

如果不存在这样的串,输出"Just a legend"(去掉引号)。

输入格式:

仅一行,字符串 s。

输出格式:

如题所述

样例输入

fixprefixsuffix

样例输出:

fix

数据范围:

对于 60%的数据, s 的长度<=100

对于 100%的数据, s 的长度<=100000;}

亲测暴力可过,暴力最害怕的是aaaaaaaaaaaaaaa,倒着for

//kmp? bruce!#include<algorithm>#include<iostream>#include<cstring>#include<cstdio>#include<queue>#include<set>using namespace std;const int N = 1000000 + 5;char s[N]; int ans=0,n;bool check( int x ){for( int i = x; i >= 1; i-- ){if( s[n-x+i] != s[i] ) return false;}bool flag = 0;for( int i = 2; i <= n-x; i++ ){if( flag ) break;for( int j = 1; j <= x; j++ ){if( s[j] != s[j+i-1] ) break;if( j == x ) { flag = 1; break; }}}return flag;}int main(){freopen("passward.in","r",stdin);freopen("passward.out","w",stdout);scanf("%s", s+1); n = strlen( s+1 );for( int i = n-1; i >= 1; i-- ){if( check(i) ){ ans = i; break; }}if( !ans ) puts( "Just a legend" );else{ for( int i = 1; i <= ans; i++ ) printf("%c", s[i]); }return 0;}/*aaaaaaaaaaaaaaaaa*/

【背景描述】

一排N 个数,i个数是Ai你要找出K个不相邻的数,使得他们的和最大。

请求出这个最大和。

【输入格式】

第一行两个整数 N 和 K。

接下来一行 N 个整数, 第 i 个整数表示 Ai 。

【输出格式】

一行一个整数表示最大和, 请注意答案可能会超过 int 范围

【样例输入】

3 2

4 5 3

【样例输出】

7

【数据范围】

对于 20% 的数据, N, K ≤ 20 。

对于 40% 的数据, N, K ≤ 1000 。

对于 60% 的数据, N, K ≤ 10000 。

对于 100%的数据, N,K ≤ 100000 , 1 ≤ Ai ≤ 1000000000。

#include<algorithm>#include<iostream>#include<cstdio>#include<queue>#include<set>using namespace std;const int N = 100000 + 5;typedef long long ll;ll n,k,a[N],ans; int pre[N],nxt[N];struct data{ll id, v;friend bool operator < ( const data &a, const data &b ){return a.v == b.v ? a.id < b.id : a.v > b.v;}};//priority_queue< data , vector<data>, greater<data> > q; naive priority queueset < data > q;//手写个铲铲 int main(){freopen("so.in","r",stdin);freopen("so.out","w",stdout);cin>>n>>k;for( int i = 1; i <= n; i++ ) cin>>a[i], pre[i] = i-1, nxt[i] = i+1, q.insert((data){i,a[i]});nxt[n] = 0; a[0] = a[n+1] = -1000000000000000LL;for( int i = 1; i <= k; i++ ){data now = *q.begin(); ans += a[now.id]; q.erase( (data){now.id,now.v} );a[now.id] = a[pre[now.id]] + a[nxt[now.id]] - a[now.id];q.insert( (data){ now.id, a[now.id] } );/*My English is worse than The Elder, is the worst in my classroom.It's not ganxingrenzhi!5 numbers: z,a,b,c,d. If I choose b, it mins b is the biggset number of abc.And if I want to choose a single a, but b is the biggest number, so whydo I choose a? Because I want to choose c, it's the only advantage I have taken of( Because if I choose a, I can choose c, while if I choose b, I can choose z )by choose a insead of choosing b. So if I choose the biggest b, a simgle aor a simgle c is impossible,so a[now.id] = a[pre[now.id]] + a[nxt[now.id]] - a[now.id]; is right!*/ q.erase( (data){ pre[now.id], a[pre[now.id]] } ); pre[now.id] = pre[pre[now.id]];q.erase( (data){ nxt[now.id], a[nxt[now.id]] } ); nxt[now.id] = nxt[nxt[now.id]];if(pre[now.id]) nxt[pre[now.id]] = now.id; if(nxt[now.id]) pre[nxt[now.id]] = now.id;}cout<<ans<<endl;return 0;}/*3 24 5 35 25 1 7 9 210 35 5 1 4 7 8 1 6 9 5*/

Hazel有n本书,编号1为n到 ,叠成一堆。当她每次抽出一本书的时候,上方的书会因重力而下落,这本被取出的书则会被放置在书堆顶。

每次有pi的概率抽取编号为i的书。她每次抽书所消耗的体力与这本书在这堆中是第几本成正比。具体地,抽取堆顶的书所耗费体力值为1 ,抽取第二本耗费体力值为2 ,以此类推。

现在 想知道,在很久很久以后(可以认为几乎是无穷的),她每次抽书所耗费的体力的期望值是多少。

最终的答案显然可以表示成a/b的形式,请输出a*(b^-1)模1e9+7的值。

【输入格式】

第一行一个整数n

接下来n行,每行两个整数ai,bi,代表抽取第i本书的概率是ai/bi

保证所有书的概率和等于1

【输出格式】

输出一行一个整数,代表期望值

【输入样例1】

2

227494 333333

105839 333333

【输出样例1】

432679642

【输入样例2】】

10

159073 999999

1493 142857

3422 333333

4945 37037

2227 111111

196276 999999

190882 999999

142721 999999

34858 999999

101914 999999

【输出样例2】

871435606

【数据规模与约定】

对于30%的数据,1<=n<=10。

对于100%的数据,1<=n<=1000,0<=ai<=bi,bi!=0。

它其实是数学题

只用n2算一本书在他上面的概率,分数用逆元处理

//math problem#include<algorithm>#include<iostream>#include<cstdio>using namespace std;typedef long long ll;const ll mod = 1e9 + 7;void exgcd( ll a, ll b, ll &x, ll &y ){if( !b ) x = 1ll, y = 0ll;else exgcd( b, a%b, y, x ), y -= a/b*x;}ll inv( ll a, ll b ){ll x = 0ll, y = 0ll;exgcd( a, b, x, y );x = ( x % b + b ) % b;if(!x) x += b;return x;}ll n,a[1005],b[1005],ans=0;int main(){freopen("book.in","r",stdin);freopen("book.out","w",stdout);cin>>n;for( int i = 1; i <= n; i++ ){cin>>a[i]>>b[i];b[i] = inv( b[i], mod ) % mod;a[i] = ( a[i] * b[i] ) % mod;}for( int i = 1; i <= n; i++ ){ll res = 1ll;for( int j = 1; j <= n; j++ )if( j ^ i ){ll x = a[j], y = inv( a[i]+a[j], mod );x = ( x * y ) % mod;res = ( res + x ) % mod;}res = ( res * a[i] ) % mod;ans = ( ans + res ) % mod;}cout<<ans<<endl;return 0;}




原创粉丝点击