#
归并排序+线段树+二分.
二分写的我比较恶心..
#include <iostream>
#include <stdio.h>
using namespace std;
struct node
{
int l,r,t;
}tree[400001];
const int MAXN=100001;
int n,qn;
int num[19][MAXN];
int top=0;
void mergesort(int l,int r,int t,int p)
{
if (l==r)
{
num[t][l]=num[0][l];
tree[p].t=t;
tree[p].l=l;
tree[p].r=r;
return;
}
int mid=(l+r)/2;
mergesort(l,mid,t+1,p*2);
mergesort(mid+1,r,t+1,p*2+1);
int l1=l,r1=mid+1,v=l;
while(v<=r)
{
if ((num[t+1][l1]<num[t+1][r1]||r1>r)&&l1<=mid) {num[t][v++]=num[t+1][l1++];continue;}
if ((num[t+1][r1]<num[t+1][l1]||l1>mid)&&r1<=r) {num[t][v++]=num[t+1][r1++];}
}
tree[p].l=l;
tree[p].r=r;
tree[p].t=t;
}
int find(int l,int r,int value,int p)
{
int left_=tree[p].l;
int right_=tree[p].r;
int mid=(left_+right_)/2;
int count=0;
if (left_>=l&&right_<=r)
{
int ll=left_,rr=right_,t=tree[p].t;
int mm;
while(ll<rr)
{
mm=(ll+rr+1)/2;
if (num[t][mm]<value) ll=mm; else rr=mm-1;
}
count=ll-left_;
if (value>num[t][ll]) count++;
return count;
}
else
{
if (l<=mid) count+=find(l,r,value,p*2);
if (r>=mid+1) count+=find(l,r,value,p*2+1);
}
return count;
}
int main()
{
cin>>n>>qn;
for (int i=1;i<=n;i++)
{
cin>>num[0][i];
}
top++;
mergesort(1,n,top,1);
for (int i=1;i<=qn;i++)
{
int l,r,k;
cin>>l>>r>>k;
k--;
int lp=1,rp=n;
int mid;
while(lp<rp)
{
mid=(lp+rp+1)/2;
int kk=find(l,r,num[1][mid],1);
if (kk<=k) lp=mid;
else rp=mid-1;
}
cout<<num[1][lp]<<endl;
}
system("pause");
return 0;
}
摘要: km..第一次写km..
#include <iostream>#include <fstream>#include <string>#include <math.h>using namespace std;ifstream fin("cupid.in");ofstream&nb...
阅读全文
/**//*
ID:ccl03261
LANG:C++
TASK:lgame
*/
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream fin("lgame.in");
ifstream dfin("lgame.dict");
ofstream fout("lgame.out");
vector<string> v;
string input_str;
string str[40001];
int n=0;
int hash[26],phash[26];
int t[40001][26];
int score[26]={2,5,4,4,1,6,5,5,1,7,6,3,5,2,3,5,7,2,1,2,4,6,6,7,5,7};
int main()
{
for (int i=0;i<26;i++)
hash[i]=0;
fin>>input_str;
for (int i=0;i<input_str.length();i++)
hash[input_str[i]-'a']++;
for (int i=0;i<40001;i++)
for (int j=0;j<26;j++)
t[i][j]=0;
while(1)
{
string s;
dfin>>s;
if (s==".") break;
for (int i=0;i<26;i++) phash[i]=0;
for (int i=0;i<s.length();i++)
phash[s[i]-'a']++;
bool ok=true;
for (int i=0;i<26;i++)
if (phash[i]>hash[i]) {ok=false;break;}
if (ok) {n++;str[n]=s;for (int i=0;i<26;i++){t[n][i]=phash[i];}}
}
int max_=0;
str[0]="";
for (int i=0;i<=n;i++)
{
for (int j=i+1;j<=n;j++)
{
for (int l=0;l<26;l++) phash[l]=0;
for (int k=0;k<26;k++)
phash[k]=t[i][k]+t[j][k];
bool ok=true;
for (int k=0;k<26;k++)
{
if (phash[k]>hash[k]) {ok=false;break;}
}
if (ok)
{
int sum=0;
for (int k=0;k<26;k++)
sum+=phash[k]*score[k];
if (max_<sum)
{
max_=sum;
v.clear();
if (i==0) v.push_back(str[j]);
else
v.push_back(str[i]+" "+str[j]);
}
else if (max_==sum)
{
if (i==0) v.push_back(str[j]);
else
v.push_back(str[i]+" "+str[j]);
}
}
}
}
fout<<max_<<endl;
sort(v.begin(),v.end());
for (int i=0;i<v.size();i++)
fout<<v[i]<<endl;
// system("pause");
return 0;
}
老老老老老题..能继续剪枝的..但是我懒..
就不剪了..
1y.我喜欢木陷阱的题...
#include <iostream>
#include <vector>
using namespace std;
int c[10][10];
int ca;
struct node
{
int x,y,value;
};
vector<node> v;
bool h[10][10],l[10][10];
bool mat[10][10];
bool ok;
int getPos(int x,int y)
{
return (x-1)/3*3+(y-1)/3+1;
}
void dfs(int pos)
{
if (pos==v.size())
{
for (int i=1;i<=9;i++)
{
for (int j=1;j<=9;j++)
{
cout<<c[i][j];
}
cout<<endl;
}
ok=true;
return;
}
node p=v[pos];
int x=p.x;
int y=p.y;
for (int i=1;i<=9&&(!ok);i++)
{
if (h[x][i]) continue;
if (l[i][y]) continue;
if (mat[getPos(x,y)][i]) continue;
h[x][i]=true;
l[i][y]=true;
mat[getPos(x,y)][i]=true;
c[x][y]=i;
dfs(pos+1);
h[x][i]=false;
l[i][y]=false;
mat[getPos(x,y)][i]=false;
}
}
int main()
{
cin>>ca;
while(ca--)
{
v.clear();
memset(h,false,sizeof(h));
memset(l,false,sizeof(l));
memset(mat,false,sizeof(mat));
ok=false;
char ch;
for (int i=1;i<=9;i++)
for (int j=1;j<=9;j++)
{
cin>>ch;
c[i][j]=ch-'0';
if (c[i][j]==0)
{
node p;
p.x=i;
p.y=j;
p.value=0;
v.push_back(p);
}
else
{
h[i][c[i][j]]=true;
l[c[i][j]][j]=true;
mat[getPos(i,j)][c[i][j]]=true;
}
}
dfs(0);
// solve();
}
//system("pause");
return 0;
}
求强连通分量,用邻接表储存,然后缩点,统计出度的点.话说我很勇敢的使用了邻接矩阵..然后就mle了
orz的是求强连通分量我还只会kosajura..
#include <iostream>
#include <stdio.h>
using namespace std;
int n,m;
int t=0;
const int MAXN=10001;
const int MAXM=50001;
bool used[MAXN];
int p[MAXN];
int pos[MAXN];
int len;
int d[MAXN];
int b[MAXN],bb[MAXN];
int x_[MAXM],y_[MAXM];
struct node
{
int v;
int next;
}ts[MAXM],tss[MAXM];
void dfs(int x)
{
used[x]=true;
int p_=b[x];
while(p_>0)
{
int i=ts[p_].v;
if (used[i]) {p_=ts[p_].next;continue;}
dfs(i);
p_=ts[p_].next;
}
t++;
p[t]=x;
}
void dfs1(int x)
{
used[x]=true;
int p=bb[x];
while(p>0)
{
int i=tss[p].v;
if (used[i]) {p=tss[p].next;continue;}
dfs1(i);
p=tss[p].next;
}
pos[x]=len;
}
void insert(int x,int y,int i)
{
ts[i].v=y;
ts[i].next=b[x];
b[x]=i;
tss[i].v=x;
tss[i].next=bb[y];
bb[y]=i;
}
int main()
{
memset(b,0,sizeof(b));
memset(bb,0,sizeof(bb));
scanf("%d %d",&n,&m);
for (int i=1;i<=m;i++)
{
int x,y;
scanf("%d %d",&x,&y);
x_[i]=x;
y_[i]=y;
insert(x,y,i);
}
memset(used,false,sizeof(used));
for (int i=1;i<=n;i++)
{
if (!used[i])
{
dfs(i);
}
}
len=0;
memset(used,false,sizeof(used));
for (int i=t;i>=1;i--)
{
int k=p[i];
if (!used[k])
{
len++;
dfs1(k);
}
}
memset(d,0,sizeof(d));
for (int i=1;i<=m;i++)
{
int x=pos[x_[i]];
int y=pos[y_[i]];
if (x==y) continue;
d[x]++;
}
int result=0;
int max_=0;
int co=0;
for (int i=1;i<=len;i++)
{
if (d[i]==0) co++;
}
if (co!=1) cout<<0<<endl;
else
{
for (int i=1;i<=len;i++)
if (d[i]==0)
{
for (int j=1;j<=n;j++)
if (pos[j]==i) result++;
}
cout<<result<<endl;
}
system("pause");
return 0;
}