Codeforces Round #223 (Div. 2)
A. Sereja and Dima
從兩端去最大的,要是相等,看下一個更當前的相比誰大,比如1,5,3,2,1,先取的應取最後一個。
下面是程式碼:
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=1005;
int a[maxn];
int f,e,n;
void getsum(int &sum1)
{
if(a[f]>a[e]) {sum1+=a[f];f++;}
else if(a[e]>a[f]){sum1+=a[e];e--;}
else if(a[e]==a[f])
{
if(f<e-2)
{
if(a[f+1]>a[e]){sum1+=a[e];e--;}
else if(a[e-1]>a[f+1]){sum1+=a[f];f++;}
else {sum1+=a[f];f++;}
}
else {sum1+=a[f];f++;}
}
}
int main()
{
//#ifndef ONLINE_JUDGE
// freopen("in.txt","r",stdin);
//#endif
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
int sum1=0,sum2=0;
f=0,e=n-1;
while(f<=e)
{
getsum(sum1);
if(f>e) break;
getsum(sum2);
}
cout<<sum1<<' '<<sum2<<endl;
return 0;
}
B. Sereja and Stairs
從給出的序列中找到一個最大的序列,滿足a1 < a2 < ... < ai - 1 < ai > ai + 1 > ... > a|a| - 1 > a|a|.
注意最後一個,不能跟相鄰的相等,還有每隔數不能出現三次及以上
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn=100010;
int a[maxn],ans[maxn];
int n;
int main()
{
//#ifndef ONLINE_JUDGE
//freopen("in.txt","r",stdin);
//#endif
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
sort(a+1,a+n+1);
int m=n;
for(int i=m-1;i>=1;i--)
if(a[i]==a[i+1])n--;
else break;
memset(ans,-1,sizeof(ans));
ans[1]=a[1];
int f=1,e=n+1;
ans[e]=-1;
for(int i=2;i<=n;i++)
{
if(a[i]==ans[e]&&a[i]==ans[f])continue;
if(i==n&&(a[i]==ans[e]||a[i]==ans[f]))continue;
if(a[i]>ans[f]) {ans[++f]=a[i];}
else if(a[i]>ans[e]){ans[--e]=a[i];}
}
bool first=true;
int num=0;
for(int i=1;i<=n;i++)
if(ans[i]!=-1)num++;
cout<<num<<endl;
for(int i=1;i<=n;i++)
{
if(ans[i]==-1) continue;
if(first){cout<<ans[i];first=false;}
else cout<<" "<<ans[i];
}
cout<<endl;
return 0;
}
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 100005;
int n;
int a[maxn];
int b[5005];
int res[maxn];
int main(){
while (scanf("%d",&n)!=EOF){
memset(b, 0, sizeof(b));
int va;
for (int i=0; i<n; i++){
scanf("%d",&va);
b[va]++;
}
int tp = 0;
int ma = 0;
for (int i=1; i<=5000; i++){
if (b[i]){
ma = i;
b[i]--;
res[tp++] = i;
}
}
for (int i=ma-1; i>=1; i--){
if (b[i]){
res[tp++] = i;
}
}
printf("%d\n",tp);
for (int i=0; i<tp; i++){
printf("%d%c",res[i],i==tp-1?'\n':' ');
}
}
return 0;
}
C. Sereja and Prefixes
給出m < 100000 次操作,每次操作兩種:
1 xi :表示在序列結尾加上一個值為 xi 的數
2 li ci: 表示把序列的前li個數重複ci次放在序列結尾
給出 n 個數 ai
輸出序列中第 ai 個數的值
由於 li <= 100000 所以可以僅保留序列的前 100000 個數即可,
對於 1 情況看看 len + 1 是否等於 ai,等於就輸出,否則不管,
對於情況 2 第 ai 個數的值為 (ai-len) % li 個位置的數,直接輸出即可,每次 len += ci * li
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 100005;
int m, n;
long long va[maxn];
struct node
{
int ty;
long long li, ci;
long long ai;
} q[maxn];
long long a[maxn];
void solve()
{
int tt = 0;
int tp = 0;
long long len = 0;
for (int i=0; i<m; i++)
{
if (q[i].ty == 1)
{
len += 1;
if (tt < n && len == va[tt])
{
tt++;
printf("%I64d ",q[i].ai);
}
if (tp < 100000)
a[tp++] = q[i].ai;
}
else
{
while (tt < n && len + q[i].ci*q[i].li >= va[tt])
{
long long xu = (va[tt]-len) % q[i].li;
xu = (xu-1 + q[i].li) % q[i].li;
printf("%I64d ", a[xu]);
tt++;
}
len += q[i].ci * q[i].li;
for (long long j=0; j<q[i].ci && tp<100000; j++)
{
for (long long k=0; k<q[i].li && tp<100000; k++)
a[tp++] = a[k];
}
}
}
}
int main()
{
while (scanf("%d",&m)!=EOF)
{
for (int i=0; i<m; i++)
{
scanf("%d",&q[i].ty);
if (q[i].ty == 1)
scanf("%I64d",&q[i].ai);
else
scanf("%I64d %I64d",&q[i].li,&q[i].ci)
}
scanf("%d",&n);
for (int i=0; i<n; i++)
scanf("%I64d",&va[i]);
solve();
}
return 0;
}
E. Sereja and Brackets(思路轉載)題意:給出一個類似 ())(())(())( 的括號序列,長度 m < 1000000,給出 n 個詢問 l, r,輸出[l, r]區間內最多有多少個匹配的括號,n<100000。
分析:樹狀陣列+離線。
先把詢問儲存,然後按照 ( 位置從大到小排序,
從括號序列的尾部向前遍歷,維護一個 ) 位置的棧
如果遇到 i 位置為 ) 則把 i 入棧,
如果遇到 i 位置為 ( ,如果棧不為空,則 add(pos = st[top]), 即把區間[1, st[top]] 都+1
對於當前位置 i, 計算所有詢問左區間 = i 的值為 getsum(r).
下面是程式碼:
#include<iostream>
#include<stack>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1000010;
const int maxm=100010;
struct node
{
int l,r,id;
}qu[maxm];
int tree[maxn];
int n,ans[maxn],m;
string a;
bool cmp(node a,node b)
{
return a.l>b.l;
}
void update(int x)
{
while(x<m)
{
tree[x]+=1;
x+=(x&(-x));
}
}
int sum(int i)
{
int ans=0;
while(i>0)
{
ans+=tree[i];
i-=(i&(-i));
}
return ans;
}
void solve()
{
int j=0;
m=a.size();
stack<int> s;
memset(tree,0,sizeof(tree));
for(int i=m-1;i>=0;i--)
{
if(a[i]==')') s.push(i);
else if(!s.empty()) {update(s.top());s.pop();}
while(j<n&&qu[j].l==i)
{
ans[qu[j].id]=sum(qu[j].r);
j++;
}
}
for(int i=0;i<n;i++)
cout<<ans[i]*2<<endl;
}
int main()
{
cin>>a>>n;
for(int i=0;i<n;i++)
{
int l,r;
cin>>l>>r;
qu[i].l=l-1;
qu[i].r=r-1;
qu[i].id=i;
}
sort(qu,qu+n,cmp);
solve();
return 0;
}
相關文章
- Codeforces Round #639 (Div. 2)
- Codeforces Round #541 (Div. 2)
- Codeforces Round #682 (Div. 2)
- Codeforces Round #678 (Div. 2)
- Codeforces Round #747 (Div. 2)
- Codeforces Round #673 (Div. 2)
- Codeforces Round #672 (Div. 2)
- Codeforces Round #448 (Div. 2) A
- Codeforces Round #217 (Div. 2)
- Codeforces Round #256 (Div. 2)
- Codeforces Round #259 (Div. 2)
- Codeforces Round #257 (Div. 2)
- Codeforces Round #258 (Div. 2)
- Codeforces Round #171 (Div. 2)
- Codeforces Round #173 (Div. 2)
- Codeforces Round 951 (Div. 2)
- Codeforces Round 955 (Div. 2)
- Codeforces Round 953 (Div. 2)
- Codeforces Round 975 (Div. 2)
- Codeforces Round 976 (Div. 2)
- Codeforces Round 972 (Div. 2)
- Codeforces Round 979 (Div. 2)
- Codeforces Round 982 (Div. 2)
- Codeforces Round 932 (Div. 2)
- Codeforces Round 934 (Div. 2)
- Codeforces Round 940 (Div. 2)
- Codeforces Round 973 (Div. 2)
- Codeforces Round 960 (Div. 2)
- Codeforces Round 958 (Div. 2)
- Codeforces Round 961 (Div. 2)
- Codeforces Round 948 (Div. 2)
- Codeforces Round 945 (Div. 2)
- Codeforces Round 873 (Div. 2)
- Codeforces Round 969 (Div. 2)
- Codeforces Round 949 (Div. 2)
- Codeforces Round 965 (Div. 2)
- Codeforces Round 963 (Div. 2)
- Codeforces Round 967 (Div. 2)