Merge pull request #8 from Kiritow/master

Catch up with you~
This commit is contained in:
KiritoTRw 2016-05-22 22:56:01 +08:00
commit 6ed29af4f8
52 changed files with 2598 additions and 0 deletions

View File

@ -0,0 +1,55 @@
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
inline void bzero(void* ptr,size_t size)
{
memset(ptr,0,size);
}
char msg[102400];
char ans[102400];
int cnt=0;
int main()
{
int key;
int turn;
scanf("%d%*c",&turn);
for(int tt=0;tt<turn;tt++)
{
bzero(msg,102400);
bzero(ans,102400);
cnt=0;
gets(msg);
scanf("%d%*c",&key);
int len=strlen(msg);
int LL=len%key;
int L=len/key;
for(int i=0;i<L;i++)
{
for(int j=0;j<key;j++)
{
int v;
if(j<LL)
{
v=j*(L+1)+i;
}
else
{
v=(L+1)*LL+(j-LL)*L+i;
}
ans[cnt]=msg[v];
cnt++;
}
}
/// "L" th Line. (i==L)
for(int j=0;j<LL;j++)
{
int v=(j+1)*(L+1)-1;
ans[cnt]=msg[v];
cnt++;
}
printf("Case #%d:\n%s\n",tt+1,ans);
}
return 0;
}

View File

@ -0,0 +1,61 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
using namespace std;
/// std::cin and std::cout is so slow.
struct ippack
{
int a,b,c,d;
};
int main()
{
int t;
scanf("%d",&t);
for(int tt=1;tt<=t;tt++)
{
printf("Case #%d:\n",tt);
vector<ippack> ip;
int n,m;
scanf("%d %d",&n,&m);
for(int i=0;i<n;i++)
{
ippack s;
scanf("%d.%d.%d.%d",&s.a,&s.b,&s.c,&s.d);
ip.push_back(s);
}
for(int i=0;i<m;i++)
{
vector<unsigned long> ans;
ippack s;
scanf("%d.%d.%d.%d",&s.a,&s.b,&s.c,&s.d);
for(int j=0;j<n;j++)
{
ippack k=ip.at(j);
k.a&=s.a;
k.b&=s.b;
k.c&=s.c;
k.d&=s.d;
unsigned long tmp=(k.a<<24)|(k.b<<16)|(k.c<<8)|(k.d);
int sz=ans.size();
bool flag_isfound=false;
for(int k=0;k<sz;k++)
{
if(ans.at(k)==tmp)
{
flag_isfound=true;
break;
}
}
if(!flag_isfound)
{
ans.push_back(tmp);
}
}
printf("%d\n",ans.size());
}
}
return 0;
}

View File

@ -0,0 +1,65 @@
#include"iostream"
#include"cstdio"
#include"cstring"
#include"algorithm"
#include"queue"
#include"vector"
using namespace std;
const int ms=30010;
vector<int> v[ms];
struct cmp
{
bool operator()(const int &a,const int &b)
{//从大到小
return a<b;//注意在结构体中以第二元素为基准
}
};
int n,m,in[ms],ans[ms],cnt;
void topo()
{
priority_queue<int,vector<int>,cmp> q;
for(int i=1;i<=n;i++)
if(in[i]==0)
q.push(i);
int x,i;
while(!q.empty())
{
x=q.top();
q.pop();
ans[++cnt]=x;
for(i=0;i<v[x].size();i++)
{
in[v[x][i]]--;
if(in[v[x][i]]==0)
q.push(v[x][i]);
}
}
return ;
}
int main()
{
int t,a,b,i;
cin>>t;
while(t--)
{
cin>>n>>m;
for(i=0;i<=n;i++)
v[i].clear();
memset(in,0,sizeof(in));
while(m--)
{
scanf("%d%d",&a,&b);
v[b].push_back(a);
in[a]++;
}
cnt=0;
topo();
printf("%d",ans[cnt]);
for(i=cnt-1;i>0;i--)
{
printf(" %d",ans[i]);
}
puts("");
}
return 0;
}

55
HDOJ/1075_cnblogs_B.cpp Normal file
View File

@ -0,0 +1,55 @@
#include <bits/stdc++.h>
using namespace std;
int main()
{
char getChar[11];
gets(getChar);
char str1[33], str2[33];
map <string, string> mp;
map <string, string> :: iterator it;
while (scanf("%s%s", str1, str2), str1[0] != 'E')
{
mp[str2] = str1;//可以传入char[]的地址
}
getchar();
char ansStr[3333];
char saveStr[33];
while (gets(ansStr), ansStr[0] != 'E')
{
int i;
int k = 0;
for (i = 0; i < strlen(ansStr); i++)
{
if (ansStr[i] < 'a' || ansStr[i] > 'z')
{
if (ansStr[i - 1] >= 'a' && ansStr[i - 1] <= 'z')
{
saveStr[k] = '\0';
k = 0;
it = mp.find(saveStr);
if (it == mp.end())
{
printf("%s", saveStr);//字典中不存在,则输出原单词
}
else
{
cout << it->second;//存在,输出翻译
}
}
printf("%c", ansStr[i]);
}
else
{
saveStr[k++] = ansStr[i];
}
}
puts("");
}
return 0;
}

View File

@ -0,0 +1,65 @@
#include"iostream"
#include"cstdio"
#include"cstring"
#include"algorithm"
#include"queue"
#include"vector"
using namespace std;
const int ms=30010;
vector<int> v[ms];
struct cmp
{
bool operator()(const int &a,const int &b)
{//从大到小
return a<b;//注意在结构体中以第二元素为基准
}
};
int n,m,in[ms],ans[ms],cnt;
void topo()
{
priority_queue<int,vector<int>,cmp> q;
for(int i=1;i<=n;i++)
if(in[i]==0)
q.push(i);
int x,i;
while(!q.empty())
{
x=q.top();
q.pop();
ans[++cnt]=x;
for(i=0;i<v[x].size();i++)
{
in[v[x][i]]--;
if(in[v[x][i]]==0)
q.push(v[x][i]);
}
}
return ;
}
int main()
{
int t,a,b,i;
cin>>t;
while(t--)
{
cin>>n>>m;
for(i=0;i<=n;i++)
v[i].clear();
memset(in,0,sizeof(in));
while(m--)
{
scanf("%d%d",&a,&b);
v[b].push_back(a);
in[a]++;
}
cnt=0;
topo();
printf("%d",ans[cnt]);
for(i=cnt-1;i>0;i--)
{
printf(" %d",ans[i]);
}
puts("");
}
return 0;
}

31
HDOJ/4931.cpp Normal file
View File

@ -0,0 +1,31 @@
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
vector<int> vec;
int s;
for(int i=0; i<6; i++)
{
scanf("%d",&s);
vec.push_back(s);
}
sort(vec.begin(),vec.end());
int sum1=vec.at(5)+vec.at(4);
int sum2=vec.at(1)+vec.at(2)+vec.at(3);
if(sum1>sum2)
{
printf("Grandpa Shawn is the Winner!\n");
}
else
{
printf("What a sad story!\n");
}
}
return 0;
}

22
HDOJ/4931_zgxue.cpp Normal file
View File

@ -0,0 +1,22 @@
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
int t, a[6];
int main() {
scanf("%d", &t);
while (t--) {
for (int i = 0; i < 6; i++)
scanf("%d", &a[i]);
sort(a, a + 6);
int aa = a[5] + a[4];
int bb = a[3] + a[2] + a[1];
if (aa > bb) printf("Grandpa Shawn is the Winner!\n");
else printf("What a sad story!\n");
}
return 0;
}

57
HDOJ/4932_lyhvoyage.cpp Normal file
View File

@ -0,0 +1,57 @@
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
double b[120], c[60];
int flag[60];
int n, i, j, T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i = 0; i < n; i++)
scanf("%lf",&c[i]);
sort(c, c+n);
int m = 0;
for(i = 1; i < n; i++)
{
b[m++] = c[i] - c[i-1];
b[m++] = (c[i] - c[i-1]) / 2;
}
sort(b, b+m);
double ans;
for(i = m - 1; i >= 0; i--)
{
memset(flag, 0, sizeof(flag));
flag[0] = 1;
double tmp = b[i];
for(j = 1; j < n - 1; j++)
{
if(c[j] - tmp < c[j-1] && c[j] + tmp > c[j+1]) //往左往右都不行
break;
if(c[j] - tmp >= c[j-1])
{
if(flag[j-1] == 2) // 前一个往右
{
if(c[j] - c[j-1] == tmp) flag[j] = 1; //两个点作为线段的两个端点
else if(c[j] - c[j-1] >= 2 * tmp) flag[j] = 1; //一个往左,一个往右
else if(c[j] + tmp <= c[j+1]) flag[j] = 2; //只能往右
else break;
}
else flag[j] = 1;
}
else if(c[j] + tmp <= c[j+1])
flag[j] = 2;
}
if(j == n - 1)
{
ans = tmp;
break;
}
}
printf("%.3lf\n", double(ans));
}
return 0;
}

78
POJ/1125.cpp Normal file
View File

@ -0,0 +1,78 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
#define MAXSTOCK 101
int m[MAXSTOCK][MAXSTOCK];
int main()
{
int n;
while(scanf("%d",&n)==1&&n!=0)
{
memset(m,0,sizeof(int)*MAXSTOCK*MAXSTOCK);
for(int i=1;i<=n;i++)/// From i to...
{
int s;
scanf("%d",&s);
for(int j=0;j<s;j++)
{
int t,v;
scanf("%d %d",&t,&v);
/// From i to t, value=v
m[i][t]=v;
}
}
for(int k=1;k<=n;k++)
{
for(int f=1;f<=n;f++)
{
for(int t=1;t<=n;t++)
{
if(f==t||f==k||t==k) continue;
if(m[f][k]!=0&&m[k][t]!=0)
{
int total=m[f][k]+m[k][t];
if(total<m[f][t]||m[f][t]==0)
{
m[f][t]=total;
}
}
}
}
}
/// Judge if is disjoint
bool found=false;
int FromPos=-1;
int FromPosMaxTime=-1;
for(int i=1;i<=n;i++)
{
int cnt=0;
int Max=0;
for(int j=1;j<=n;j++)
{
Max=Max>m[i][j]?Max:m[i][j];
if(m[i][j]) ++cnt;
}
if(cnt==n-1)
{
found=true;
if(FromPosMaxTime<0||FromPosMaxTime>Max)
{
FromPos=i;
FromPosMaxTime=Max;
}
}
}
if(!found)
{
printf("disjoint\n");
}
else
{
printf("%d %d\n",FromPos,FromPosMaxTime);
}
}
return 0;
}

68
POJ/1170_2cto.cpp Normal file
View File

@ -0,0 +1,68 @@
#include <stdio.h>
#include <string.h>
#define MAX 110000
#define INF (1<<29)
#define min(a,b) ((a)<(b)?(a):(b))
struct product {
int code,st,num,price;
}arr[MAX],special[MAX];
int dp[MAX],hash[1000],n,m,nn,ans;
int ST[10] = {1,6,36,216,1296,7776,46656,279936};
void Initial() {
ans = nn = 0;
memset(hash,-1,sizeof(hash));
for (int i = 0; i < ST[6]; ++i)
dp[i] = INF;
}
void Solve_DP() {
int i,j,k;
dp[0] = 0;
for (i = 0; i <= nn; ++i)
for (j = 0; j < n + m; ++j)
dp[i+arr[j].st] = min(dp[i+arr[j].st],dp[i]+arr[j].price);
ans = min(ans,dp[nn]);
}
int main()
{
int i,j,k,tp,num,code;
while (scanf("%d",&n) != EOF) {
Initial();
for (i = 0; i < n; ++i) {
scanf("%d%d%d",&arr[i].code,&arr[i].num,&arr[i].price);
arr[i].st = ST[i];
hash[arr[i].code] = i;
nn += arr[i].num * arr[i].st;
ans += arr[i].num * arr[i].price;
}
scanf("%d",&m);
for (i = 0; i < m; ++i) {
scanf("%d",&k);
arr[i+n].st = 0;
for (j = 0; j < k; ++j) {
scanf("%d%d",&code,&num);
if (hash[code] == -1) continue;
arr[i+n].st += arr[hash[code]].st * num;
}
scanf("%d",&arr[i+n].price);
}
Solve_DP();
printf("%d\n",ans);
}
}

95
POJ/1170_JDPlus.cpp Normal file
View File

@ -0,0 +1,95 @@
#include <stdio.h>
#include <string.h>
#include <limits.h>
int code[6]; //商品代码
int num[6]; //商品数量
int price[6]; //商品价格
int special_num[100][6]; //促销项目各个商品数量
int special_cnt[100]; //促销项目的商品数量
int special_price[100]; //促销项目价格
int basket;
int special;
int dp[6][6][6][6][6];
int Decode (int c){
int i;
for (i=1; i<=5; ++i){
if (code[i] == c)
break;
}
return i;
}
void Init(){
int i;
int j;
int c;
int k;
int index;
scanf ("%d", &basket);
for (i=1; i<=basket; ++i){
scanf ("%d%d%d", &code[i], &num[i], &price[i]);
}
scanf ("%d", &special);
for (i=1; i<=special; ++i){
scanf ("%d", &special_cnt[i]);
for (j=1; j<=special_cnt[i]; ++j){
scanf ("%d%d", &c, &k);
index = Decode (c);
special_num[i][index] = k;
}
scanf ("%d", &special_price[i]);
}
}
void Lowest_Price (){
int i1, i2, i3, i4, i5;
int i;
int tmp1, tmp2;
memset (dp, -1, sizeof(dp));
dp[0][0][0][0][0] = 0;
for (i1=0; i1<=num[1]; ++i1){
for (i2=0; i2<=num[2]; ++i2){
for (i3=0; i3<=num[3]; ++i3){
for (i4=0; i4<=num[4]; ++i4){
for (i5=0; i5<=num[5]; ++i5){
tmp1 = INT_MAX;
tmp2 = INT_MAX;
for (i=1; i<=special; ++i){
if (i1 >= special_num[i][1] &&
i2 >= special_num[i][2] &&
i3 >= special_num[i][3] &&
i4 >= special_num[i][4] &&
i5 >= special_num[i][5]){
tmp2 = dp[i1-special_num[i][1]]
[i2-special_num[i][2]]
[i3-special_num[i][3]]
[i4-special_num[i][4]]
[i5-special_num[i][5]] + special_price[i];
if (tmp1 > tmp2)
tmp1 = tmp2;
}
}
if (tmp1 != INT_MAX){
dp[i1][i2][i3][i4][i5] = tmp1;
}
else{
dp[i1][i2][i3][i4][i5] = i1 * price[1] + i2 * price[2]
+ i3 * price[3] + i4 * price[4] + i5 * price[5];
}
}
}
}
}
}
printf ("%d\n", dp[num[1]][num[2]][num[3]][num[4]][num[5]]);
}
int main(void){
Init ();
Lowest_Price ();
return 0;
}

168
POJ/1204.cpp Normal file
View File

@ -0,0 +1,168 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
const int MAXC=1005;
const int MAXL=1005;
const int MAXW=1005;
#define nullptr NULL
//#define ISDEBUG
#ifdef ISDEBUG
#define dprintf(format,args...) printf(format,##args)
#define dsystem(cmd) system(cmd)
#else
#define dprintf(format,args...)
#define dsystem(cmd)
#endif // ISDEBUG
char map[MAXL][MAXC];
char word[MAXW];
int ans[MAXW][3];
int L,C,W;
int direction[8][2];
inline void initdirection()
{
/// ..[..][0] +-Y ..[..][1] +-X
direction[0][0]=-1;
direction[0][1]=0;
direction[1][0]=-1;
direction[1][1]=+1;
direction[2][0]=0;
direction[2][1]=+1;
direction[3][0]=+1;
direction[3][1]=+1;
direction[4][0]=+1;
direction[4][1]=0;
direction[5][0]=+1;
direction[5][1]=-1;
direction[6][0]=0;
direction[6][1]=-1;
direction[7][0]=-1;
direction[7][1]=-1;
}
struct node
{
node* next[26];
int wid;
};
node _root;
node* root=&_root;
void display_tree(int step,node* ptr)
{
for(int i=0;i<26;i++)
{
if(ptr->next[i]!=nullptr)
{
for(int i=0;i<step;i++)
{
printf("\t");
}
printf("%c\n",i+'A');
display_tree(step+1,ptr->next[i]);
}
}
}
void printtree()
{
for(int i=0;i<26;i++)
{
if(root->next[i]!=nullptr)
{
printf("%c\n",i+'A');
display_tree(1,root->next[i]);
}
}
}
void dfs(node* ptr,int starty,int startx,int y,int x,int dir)
{
dprintf("In DFS: %d %d Dir=%d\n",y,x,dir);
if(ptr==nullptr)
{
dprintf("Null Pointer.\n");
return;
}
if(ptr->wid!=0)
{
dprintf("Set ID ptr->wid=%d Set to %d,%d,%d(dir)\n",ptr->wid,startx,starty,dir);
dsystem("PAUSE");
ans[ptr->wid][0]=starty;
ans[ptr->wid][1]=startx;
ans[ptr->wid][2]=dir+'A';
/// Reset word ID to 0.
ptr->wid=0;
}
if(y<0||y>=L||x<0||x>=C) return;
/// Same Direction (dir)
dfs(ptr->next[map[y][x]-'A'],starty,startx,y+direction[dir][0],x+direction[dir][1],dir);
}
void buildtree(char* str,node* curnode,int id)
{
int len=strlen(str);
node* nptr=curnode;
for(int i=0;i<len;i++)
{
int index=str[i]-'A';
if(nptr->next[index]==nullptr)
{
node* newnode=(node*)malloc(sizeof(node));
for(int s=0;s<26;s++) newnode->next[s]=nullptr;
newnode->wid=0;
nptr->next[index]=newnode;
nptr=newnode;
}
else
{
nptr=nptr->next[index];
}
}
nptr->wid=id;
}
int main()
{
initdirection();
scanf("%d %d %d",&L,&C,&W);
for(int i=0;i<L;i++)
{
scanf("%s",&map[i][0]);
}
for(int i=0;i<W;i++)
{
scanf("%s",word);
buildtree(word,root,i+1);
}
/// Main LOOP
for(int i=0;i<L;i++)
{
for(int j=0;j<C;j++)
{
for(int k=0;k<8;k++)
{
node* ptrStart=root;
dfs(ptrStart,i,j,i,j,k);
}
}
}
for(int i=1;i<=W;i++)
{
printf("%d %d %c\n",ans[i][0],ans[i][1],(char)ans[i][2]);
}
return 0;
}

70
POJ/1204_ACM_Ted.cpp Normal file
View File

@ -0,0 +1,70 @@
#include<cstdio>
#include<cstring>
using namespace std;
struct node
{
node *next[26];
int id;
node()
{
memset(next,0,sizeof(next));
id=0;
}
}*head;
int dir[][2]={-1,0,-1,1,0,1,1,1,1,0,1,-1,0,-1,-1,-1};//方向
char mat[1005][1005];//地图
char s[1005];
int out[1005][3];
int cnt,x,y;
int n,m,w;
void build(char *s,node *head,int id)//建树
{
int len=strlen(s),k;
for(int i=0;i<len;++i)
{
k=s[i]-'A';
if(head->next[k]==NULL)
head->next[k]=new node();
head=head->next[k];
}
head->id=id;
}
void dfs(node *p,int a,int b,int c)//深搜
{
if(p==NULL) return;
if(p->id>0)
{
out[p->id][0]=x;out[p->id][1]=y;out[p->id][2]=c;
p->id=0;
//这里不能return
}
if(a<0||n<=a||b<0||m<=b) return;
dfs(p->next[mat[a][b]-'A'],a+dir[c][0],b+dir[c][1],c);
}
int main()
{
head=new node();
node *p;
scanf("%d%d%d",&n,&m,&w);
//输入地图
for(int i=0;i<n;++i)
scanf("%s",mat[i]);
for(int i=1;i<=w;++i)
{
scanf("%s",s);
build(s,head,i);
}
//枚举地图上每个点
for(int i=0;i<n;++i)
for(int j=0;j<m;++j)
for(int k=0;k<8;++k)
{
x=i;y=j;
p=head;
dfs(p,i,j,k);
}
//输出
for(int i=1;i<=w;++i)
printf("%d %d %c\n",out[i][0],out[i][1],out[i][2]+'A');
return 0;
}

106
POJ/1204_kk303.cpp Normal file
View File

@ -0,0 +1,106 @@
#include<iostream>
#include<queue>
#define judge (y<ny && y>=0 && x<nx && x>=0)
using namespace std;
struct node
{
node *s[26],*fail;
char c;
int w;
}*head,*p;
struct pp
{
int y,x,len;
char c;
}outdata[1001];
int ny,nx,n,i,face,len,t[8][2]={{0,-1},{1,-1},{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1}};
char s[1001][1001],str[2001];
node *UpdateFail(node *p,int k)
{
if (p->s[k]!=NULL) return p->s[k];
else
if (p==head) return p;
else
return UpdateFail(p->fail,k);
}
void built_trie(node *h,int k)
{
if (k==len)
{
h->w=i;
return;
}
if (h->s[str[k]-'A']==NULL)
{
p=new node;
p->w=0;
p->c=str[k];
for (int j=0;j<26;j++) p->s[j]=NULL;
p->fail=head;
h->s[str[k]-'A']=p;
}
built_trie(h->s[str[k]-'A'],k+1);
return;
}
queue< node* > myqueue;
void built_AC_automation()
{
int i;
node *h;
while (!myqueue.empty()) myqueue.pop();
myqueue.push(head);
while (!myqueue.empty())
{
h=myqueue.front();
myqueue.pop();
for (i=0;i<26;i++)
if (h->s[i]!=NULL)
{
myqueue.push(h->s[i]);
if (h==head) h->s[i]->fail=head;
else
h->s[i]->fail=UpdateFail(h->fail,i);
}
}
return;
}
void find(int y,int x,node *h)
{
if (h==NULL) h=head;
if (h->w && outdata[h->w].x<0)
{
outdata[h->w].x=x-t[face][0]*(outdata[h->w].len);
outdata[h->w].y=y-t[face][1]*(outdata[h->w].len);
outdata[h->w].c=face+'A';
}
if (!judge) return;
while (h->s[s[y][x]-'A']==NULL && h!=head) h=h->fail;
find(y+t[face][1],x+t[face][0],h->s[s[y][x]-'A']);
return;
}
int main()
{
memset(outdata,-1,sizeof(outdata));
scanf("%d%d%d",&ny,&nx,&n);
getchar();
for (i=0;i<ny;i++) scanf("%s",s[i]);
head=new node;
head->w=0;
head->fail=head;
for (i=0;i<26;i++) head->s[i]=NULL;
for (i=1;i<=n;i++)
{
scanf("%s",str);
len=strlen(str);
outdata[i].len=len;
built_trie(head,0);
}
built_AC_automation();
for (face=0;face<8;face++)
{
for (i=0;i<nx;i++) { find(0,i,head); find(ny-1,i,head); }
for (i=0;i<ny;i++) { find(i,0,head); find(i,nx-1,head); }
}
for (i=1;i<=n;i++) printf("%d %d %c\n",outdata[i].y,outdata[i].x,outdata[i].c);
return 0;
}

78
POJ/1276.cpp Normal file
View File

@ -0,0 +1,78 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
///Internet 背包
namespace PackageNet
{
using namespace std ;
#define MAX 100001
struct NODE
{
int n ;
int d ;
};
int dp[MAX] ;
NODE node[11] ;
int cash, n ;
void ZeroOnePack( int cost )
{
int i ;
for ( i = cash ; i >= cost ; i-- )
{
if ( dp[i] < dp[i-cost]+cost )
dp[i] = dp[i-cost]+cost ;
}
}
void CompletePack( int cost )
{
int i ;
for ( i = cost ; i <= cash ; i++ )
{
if ( dp[i] < dp[i-cost]+cost )
dp[i] = dp[i-cost]+cost ;
}
}
void MultiplePack( int cost, int count )
{
if ( cost*count >= cash )
{
CompletePack( cost ) ;
}
else
{
int k = 1 ;
while ( k < count )
{
ZeroOnePack( cost*k ) ;
count -= k ;
k *= 2 ;
}
ZeroOnePack( cost*count ) ;
}
}
int main()
{
// freopen( "e:\\in.txt" , "r" , stdin ) ;
while ( cin >> cash >> n )
{
int i ;
for ( i = 1 ; i <= n ; i++ )
cin >> node[i].n >> node[i].d ;
memset( dp, 0, sizeof( dp ) ) ;
for ( i = 1 ; i <= n ; i++ )
{
MultiplePack( node[i].d, node[i].n ) ;
}
cout << dp[cash] << endl ;
}
return 0 ;
}
}
int main()
{
return PackageNet::main();
}

58
POJ/1276_funCfans.cpp Normal file
View File

@ -0,0 +1,58 @@
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct q
{
int a,p;
};
int cmp(const void *a,const void *b)
{
return (*(q*)a).p-(*(q*)b).p;
}
int main()
{
q qs[11];
int cash,n,i,j,con[100001];
char dp[100001];
while(scanf("%d%d",&cash,&n)==2)
{
if(!n)
{
putchar('0');putchar('\n');
continue;
}
for(i=0;i<n;i++)
{
scanf("%d%d",&(qs[i].a),&(qs[i].p));//a[i]是钞票数p[i]是钞票面值
}
qs[n].a=0;
if(!cash)
{
putchar('0');putchar('\n');
continue;
}
qsort(qs,n,sizeof(q),cmp);//先规划小面额的dp末尾不会出现错误
memset(dp,0,sizeof(dp));
memset(con,0,sizeof(con));
dp[cash]=1;
con[cash]=qs[0].a;
for(i=0;i<n;i++)
{
for(j=cash;j>=qs[i].p;j--)
{
if(dp[j])
{
if(con[j] && !dp[j-qs[i].p])
{
con[j-qs[i].p]=con[j]-1;
dp[j-qs[i].p]=1;
}
con[j]=qs[i+1].a;
}
}
}
for(i=0;!dp[i];i++);
printf("%d\n",cash-i);
}
return 0;
}

69
POJ/1384.cpp Normal file
View File

@ -0,0 +1,69 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
///完全背包问题
namespace Package2
{
const int INF = 1e9;
const int MAXM=10000+5;
const int MAXN = 500+5;
int ldp[MAXM];
int w[MAXN];
int v[MAXN];
int N,M;
inline void init()
{
for(int i=0;i<MAXM;i++)
{
ldp[i]=INF;
}
ldp[0]=0;
}
void solve()
{
for(int i=1;i<=N;i++)
{
for(int j=1;j<=M;j++)///从1到M循环
{
int a=ldp[j];
int b=INF;
if(j-w[i]>=0) b=ldp[j-w[i]]+v[i];
ldp[j]=min(a,b);
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int E,F;
scanf("%d %d",&E,&F);
scanf("%d",&N);
M=F-E;
for(int i=1;i<=N;i++)
{
scanf("%d %d",&v[i],&w[i]);
}
init();
solve();
if(ldp[M]!=INF)
{
printf("The minimum amount of money in the piggy-bank is %d.\n",ldp[M]);
}
else
{
printf("This is impossible.\n");
}
}
return 0;
}
}
int main()
{
return Package2::main();
}

93
POJ/1511_aszxqw.cpp Normal file
View File

@ -0,0 +1,93 @@
//36412K 1829MS
#include<iostream>
#include<vector>
#include<string>
#include<queue>
#include<algorithm>
#define llong long long
#define Abs(a) ((a)>0?(a):-(a))
#define Mod(a,b) (((a)-1+(b))%(b)+1)
using namespace std;
const int N=1000005;
const int inf=1e10;
int n,m;
int a[N][3];
struct
{
int w,v,next;
}edge[N];
int edgehead[N];
llong d[N];
bool vis[N];
int k;
void addedge(int u,int v,int w)
{
edge[k].v=v;
edge[k].w=w;
edge[k].next=edgehead[u];
edgehead[u]=k++;
}
struct cmp//注意priority_queue的cmp写的和sort里面的cmp的区别前者是struct后者是函数而且比较大小的关系刚好是相反的。
{
bool operator()(const int a,const int b)
{
return d[a]>d[b];
}
};
llong dijstra()
{
memset(vis,0,sizeof(vis));
priority_queue<int,vector<int>,cmp> que;
for(int i=2;i<=n;i++)
d[i]=inf;
d[1]=0;
que.push(1);
while(!que.empty())
{
int u=que.top();
vis[u]=true;
que.pop();
for(int i=edgehead[u];i;i=edge[i].next)//遍历节点u的每条出去的边
{
int v=edge[i].v;
int w=edge[i].w;
if(!vis[v]&&d[v]>d[u]+w)//如果是可松弛点即加入优先队列d值越小越优先。
{
d[v]=d[u]+w;
que.push(v);
}
}
}
llong ans=0;
for(int i=1;i<=n;i++)
ans+=d[i];
return ans;
}
int main()
{
int cases;
scanf("%d",&cases);
while(cases--)
{
k=1;
memset(edgehead,0,sizeof(edgehead));
memset(edge,0,sizeof(edge));
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",a[i],a[i]+1,a[i]+2);
addedge(a[i][0],a[i][1],a[i][2]);
}
llong ans=dijstra();
k=1;//将图的边反向之后再来一遍
memset(edgehead,0,sizeof(edgehead));
memset(edge,0,sizeof(edge));
for(int i=1;i<=m;i++)
{
addedge(a[i][1],a[i][0],a[i][2]);
}
ans+=dijstra();
printf("%lld\n",ans);
}
return 0;
}

View File

@ -0,0 +1,84 @@
#include<iostream>
#include<queue>
using namespace std;
typedef struct fun
{
int a,w,pre;
} e;
fun link[2][1000005];
bool vis[1000005];
int point[2][1000005];
__int64 price[1000005];
int P,Q;
void spfa(int n)
{
int i;
for(i=2; i<=P; i++)
{
vis[i]=false;
price[i]=INT_MAX;
}
vis[1]=true;
price[i]=0;
queue<int>q;
q.push(1);
while(q.empty()==false)
{
int t=q.front();
q.pop();
vis[t]=false;
i=point[n][t];
while(i!=-1)
{
int v=link[n][i].a;
if(price[v]>price[t]+link[n][i].w)
{
price[v]=price[t]+link[n][i].w;
if(vis[v]==false)
{
vis[v]=true;
q.push(v);
}
}
i=link[n][i].pre;
}
}
}
int main()
{
int T,i,a,b,c;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&P,&Q);
for(i=1; i<=P; i++)
point[0][i]=point[1][i]=-1;//这个是用来弄初值的
i=0;
for(i=1; i<=Q; i++)
{
scanf("%d%d%d",&a,&b,&c);
link[0][i].a=a;
link[0][i].w=c;
link[1][i].a=b;
link[1][i].w=c;
link[1][i].pre=point[1][a];//记录的是以a开头的
point[1][a]=i;
link[0][i].pre=point[0][b];
point[0][b]=i;
}
__int64 sum=0;
spfa(1);//是用来出发点不同的啊
for(i=1; i<=P; i++)
{
sum+=price[i];
}
spfa(0);
for(i=1; i<=P; i++)
{
sum+=price[i];
}
printf("%I64d\n",sum);
}
return 0;
}

104
POJ/1988.cpp Normal file
View File

@ -0,0 +1,104 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
#define MAXN 30005
int parent[MAXN];
int parentdis[MAXN];
int child[MAXN];
int childdis[MAXN];
int backfrom;
int father(int pos)
{
if(parent[pos]!=pos)
{
parent[pos]=father(parent[pos]);
parentdis[pos]=parentdis[pos]+parentdis[backfrom];
}
backfrom=pos;
return parent[pos];
}
int son(int pos)
{
if(child[pos]!=pos)
{
child[pos]=son(child[pos]);
childdis[pos]=childdis[pos]+childdis[backfrom];
}
backfrom=pos;
return child[pos];
}
void put_a_on_b(int a,int b)
{
int A1=father(a);
int A2=father(b);
int B1=son(a);
int B2=son(b);
int x1=parentdis[a];
int y1=childdis[a];
int x2=parentdis[b];
int y2=childdis[b];
parent[A2]=A1;
parentdis[A2]=x1+y1+1;
if(A2!=B2)
{
parent[B2]=A1;
parentdis[B2]+=x1+y1+1;
}
child[A1]=B2;
childdis[A1]+=x2+y2+1;
if(A1!=B1)
{
child[B1]=B2;
childdis[B1]=x2+y2+1;
}
}
inline void init()
{
for(int i=0;i<MAXN;i++)
{
parent[i]=child[i]=i;
parentdis[i]=childdis[i]=0;
}
}
char buff[32];
int main()
{
int n;
scanf("%d%*c",&n);
int a,b;
init();
for(int i=0;i<n;i++)
{
char ccc;
scanf("%c %d%*c",&ccc,&a);
switch(ccc)
{
case 'M':
{
scanf("%d%*c",&b);
put_a_on_b(a,b);
}
break;
case 'C':
{
son(a);
father(a);
printf("%d\n",childdis[a]);
}
break;
}
}
return 0;
}

44
POJ/3624.cpp Normal file
View File

@ -0,0 +1,44 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
namespace Package1_SpaceOptimized
{
const int MAXM = 12880 + 5;
const int MAXN = 3402 + 5;
int ldp[MAXM];
int w[MAXN];
int v[MAXN];
int N,M;
void solve()
{
for(int i=1;i<=N;i++)
{
for(int j=M;j>=1;j--)
{
int a=ldp[j];
int b=-1;
if(j-w[i]>=0) b=ldp[j-w[i]]+v[i];
ldp[j]=max(a,b);
}
}
printf("%d\n",ldp[M]);
}
int main()
{
scanf("%d %d",&N,&M);
for(int i=1;i<=N;i++)
{
scanf("%d %d",&w[i],&v[i]);
}
solve();
return 0;
}
}
int main()
{
return Package1_SpaceOptimized::main();
}

55
QUSTOJ/1062_lelouch1.pas Normal file
View File

@ -0,0 +1,55 @@
var
i,j,n,min,max,x,y:longint;
s:Array[1..10000000]of shortint;
function long(x,k:longint):longint;
begin
long:=0;
while x>0 do
begin
inc(long);
x:=x div k;
end;
end;
function power(a,b:longint):longint;
var
t:integer;
begin
power:=1;
for t:=1 to b do
power:=power*a;
end;
function num(a,b,l,k:longint):longint;
var
i:longint;
begin
num:=0;
for i:=1 to l do
if odd(i)
then inc(num, a*trunc(power(k,l-i)) )
else inc(num, b*trunc(power(k,l-i)) );
end;
procedure all(k:longint);
var
i,j,l,l1,l2,m:longint;
begin
l1:=long(min,k);
l2:=long(max,k);
for i:=1 to k-1 do
for j:=0 to k-1 do
if i<>j then
begin
for l:=l1 to l2 do
begin
m:=num(i,j,l,k);
if (m<=max)and(m>=min) then
inc(s[m]);
end;
end;
end;
begin
readln(x,y,min,max,n);
fillchar(s,sizeof(s),0);
for i:=x to y do all(i);
for i:=min to max do
if s[i]=n then writeln(i);
end.

40
QUSTOJ/1062_zdf995.pas Normal file
View File

@ -0,0 +1,40 @@
program num;
var s:array[0..10000000] of integer;
l,a,b,c,d,t,h,w,p:int64;
i,j,m,n:longint;
begin
readln(a,b,c,d,t);
for i:=a to b do
begin
for j:=1 to i-1 do inc(s[j]);
for m:=0 to i-1 do
for n:=0 to i-1 do
if m<>n then
begin
w:=n;
p:=1;
l:=1;
while w<=d do
begin
l:=l+1;p:=p*i;
if l mod 2=0 then
begin
if m<>0 then
begin
w:=w+p*m;
if w<=d then s[w]:=s[w]+1;
end;
end
else
begin
if n<>0 then
begin
w:=w+p*n;
if w<=d then s[w]:=s[w]+1;
end;
end;
end;
end;
end;
for i:=c to d do if s[i]=t then writeln(i);
end.

30
QUSTOJ/1534.cpp Normal file
View File

@ -0,0 +1,30 @@
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int n;
while(scanf("%d",&n)==1)
{
vector<double> vec;
double tmp;
for(int i=0;i<n;i++)
{
scanf("%lf",&tmp);
vec.push_back(tmp);
}
sort(vec.begin(),vec.end());
double sum=0;
for(int i=1;i<n-1;i++)
{
sum+=vec.at(i);
}
sum=sum/(n-2);
printf("%.2f\n",sum);
}
return 0;
}

6
QUSTOJ/1727.c Normal file
View File

@ -0,0 +1,6 @@
#include <stdio.h>
int main()
{
printf("6=1+2+3\n28=1+2+4+7+14\n496=1+2+4+8+16+31+62+124+248\n");
return 0;
}

6
QUSTOJ/1728.c Normal file
View File

@ -0,0 +1,6 @@
#include <stdio.h>
int main()
{
printf("153 370 371 407 \n");
return 0;
}

6
QUSTOJ/1729.c Normal file
View File

@ -0,0 +1,6 @@
#include <stdio.h>
int main()
{
printf("220..284\n1184..1210\n2620..2924\n");
return 0;
}

6
QUSTOJ/1731.c Normal file
View File

@ -0,0 +1,6 @@
#include <stdio.h>
int main()
{
printf("3121\n");
return 0;
}

34
QUSTOJ/1732.c Normal file
View File

@ -0,0 +1,34 @@
#include <stdio.h>
//Written by Kiritow. 求最大公约数
long gcd(long a,long b)
{
if(a==0||b==0)
{
if(a+b==0)
{
return 0;
}
return (a==0)?b:a;
}
while(a!=b)
{
if(a>b)
{
a=a-b;
}
else
{
b=b-a;
}
}
return a;
}
int main()
{
int a,b;
scanf("%d %d",&a,&b);
int ans=gcd(a,b);
printf("最大公约数:%d\n最小公倍数:%d\n",ans,a*b/ans);
return 0;
}

44
QUSTOJ/1733.c Normal file
View File

@ -0,0 +1,44 @@
#include <stdio.h>
//数据范围n<=10^18,m<=1000,时间几十ms
#define __int64 long long
__int64 N,M;
int main()
{
scanf("%lld",&N);
M=3;
{
__int64 f1 = 0;
__int64 f2;
__int64 X;
if (M == 1)
{
printf("%lld\n",N);
}
else
{
for (__int64 i = 2; i <= N; ++ i)
{
if (f1 + M < i)//表示很有可能跳过X个i
{
X = (i - f1) / M;//能跳过多少个
if (i + X < N)//如果没有跳过n就是i<=N
{
i = i + X;//i直接到i+X
f2 = (f1 + X*M);//由于f1+X*M肯定<=i,所以这里不用%i
f1 = f2;
}
else//如果跳过了n,那么就不能直接加X了而是只需要加(N-i)个M即可
{
f2 = f1+(N-i)*M;
f1 = f2;
i = N;
}
}
f2 = (f1 + M) % i;//如果f1+M>=i或者跳过上面的一些i之后还是要继续当前i对于的出列的人
f1 = f2;
}
}
printf("%lld\n",f2+1);
}
return 0;
}

36
QUSTOJ/1734.c Normal file
View File

@ -0,0 +1,36 @@
#include <stdio.h>
//Written by Coffee. 判断素数
int isPrime(int num)
{
if (num == 2 || num == 3)
{
return 1;
}
if (num % 6 != 1 && num % 6 != 5)
{
return 0;
}
for (int i = 5; i*i <= num; i += 6)
{
if (num % i == 0 || num % (i+2) == 0)
{
return 0;
}
}
return 1;
}
int main()
{
int a;
scanf("%d",&a);
for(int i=2;i<=a/2;i++)
{
if(isPrime(i)&&isPrime(a-i))
{
printf("%d=%d+%d\n",a,i,a-i);
}
}
return 0;
}

34
QUSTOJ/1735.c Normal file
View File

@ -0,0 +1,34 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*
int main()
{
int a;
scanf("%d",&a);
a=((a/1000)+5)%10+(((a/100)+5)%10)*100+(((a/10)+5)%10)*10+((a+5)%10)*1000;
printf("%d\n",a);
return 0;
}
//*/
void swap(char* a,char* b)
{
char tmp=*b;
*b=*a;
*a=tmp;
}
char buff[512];
int main()
{
scanf("%s",buff);
for(int i=0;i<4;i++)
{
buff[i]+=5;
if(buff[i]>'9') buff[i]-=10;
}
swap(&buff[0],&buff[3]);
swap(&buff[1],&buff[2]);
printf("%s\n",buff);
return 0;
}

6
QUSTOJ/1736.c Normal file
View File

@ -0,0 +1,6 @@
#include <stdio.h>
int main()
{
printf("For maxinum profit,he should so save his money in a bank:\nmade fixed deposit for 8 year: 0 times\nmade fixed deposit for 5 year: 4 times\nmade fixed deposit for 3 year: 0 times\nmade fixed deposit for 2 year: 0 times\nmade fixed deposit for 1 year: 0 times\nToal:8841.01\n");
return 0;
}

62
QUSTOJ/1737.c Normal file
View File

@ -0,0 +1,62 @@
#include <stdio.h>
//Written by Kiritow. 求最大公约数
long gcd(long a,long b)
{
if(a==0||b==0)
{
if(a+b==0)
{
return 0;
}
return (a==0)?b:a;
}
while(a!=b)
{
if(a>b)
{
a=a-b;
}
else
{
b=b-a;
}
}
return a;
}
int main()
{
int a,b,c,d;
char op;
scanf("%d/%d%c%d/%d",&a,&b,&op,&c,&d);
int x,y;
switch(op)
{
case '+':
x=a*d+b*c;
y=b*d;
break;
case '-':
x=a*d-b*c;
y=b*d;
break;
case '*':
x=a*c;
y=b*d;
break;
case '/':
x=a*d;
y=b*c;
break;
}
int ret;
while((ret=gcd(x,y))!=1)
{
x/=ret;
y/=ret;
}
//*/
int mx=x>y?x:y;
printf("%d/%d%c%d/%d=%d/%d\n",a,b,op,c,d,x,y);
return 0;
}

16
QUSTOJ/1738.c Normal file
View File

@ -0,0 +1,16 @@
#include <stdio.h>
char str[256];
char ans[256];
int main()
{
scanf("%s",str);
int a=0;
int b=0;
while(str[a]!=0)
{
if((str[a]-'0')%2) ans[b++]=str[a];
++a;
}
printf("%s\n",ans);
return 0;
}

33
QUSTOJ/1739.c Normal file
View File

@ -0,0 +1,33 @@
#include <stdio.h>
#define n "\n"
int main()
{
printf(
"123"n
"124"n
"132"n
"134"n
"142"n
"143"n
"213"n
"214"n
"231"n
"234"n
"241"n
"243"n
"312"n
"314"n
"321"n
"324"n
"341"n
"342"n
"412"n
"413"n
"421"n
"423"n
"431"n
"432"n
"这样的数共有24个"n
);
return 0;
}

View File

@ -1,3 +1,8 @@
#青岛科技大学 QUSTOJ
[Goto QUSTOJ Problem Set](http://acm.qust.edu.cn/problemset.php "QUSTOJ Problem Set")
![image](/QUSTOJ/qust.jpg)
半公用账号
账号(已加密): -60 -94 -66 -66 -20 -13 -89 -122 -65 -3 -11 -82 -4 -5
密码(已加密): -5 -89 -93 -68 -4 -2 -84 -90
Key: m435f

View File

@ -1,2 +1,6 @@
#太原成成中学 TYVJ
[Goto Home Page](http://www.tyvj.cn/ "Welcome to TYVJ 4.0")
账号(已加密): -54 -82 -76 -66 -20 -3 -85 -116 -65 -3 -5 -94 -10 -5
密码(已加密): -11 -85 -87 -68 -4 -16 -96 -84
Key: c895f

57
UVA/12100_Richr_Wong.cpp Normal file
View File

@ -0,0 +1,57 @@
#include <iostream>
#include <string>
#include <queue>
using namespace std;
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
int num, index;
queue<int> q;
priority_queue<int> pq;
cin>>num>>index;
for(int j=0;j<num;j++)
{
int rate;
cin>>rate;
pq.push(rate);
q.push(rate);
}
int x = 0;
while(true)
{
if(q.front()==pq.top())
{
if(x==index)
{
cout<<num-q.size()+1<<endl;
break;
}
else
{
q.pop();
pq.pop();
x++;
}
}
else
{
int temp = q.front();
q.pop();
q.push(temp);
if(x==index)
{
x=0;
index=q.size()-1;
}
else
{
x++;
}
}
}
}
return 0;
}

60
UVA/12504_Richr_Wong.cpp Normal file
View File

@ -0,0 +1,60 @@
#include <iostream>
#include <strstream>
#include <sstream>
#include <map>
#include <set>
#include <cstdio>
#include <cstring>
using namespace std;
char bufold[1000],bufnnew[1000];
string key,value;
map<string,string>old,nnew;
map<string,string>::iterator it;
set<string>add,sub,cha;
set<string>::iterator itt;
inline void filter(char *str,int n){
for(int i=0;i<n;i++)if(str[i]=='{'||str[i]==':'||str[i]==','||str[i]=='}')str[i]=' ';
}
int main()
{
int n;
cin>>n;
getchar();
while(n--)
{
gets(bufold);
gets(bufnnew);
filter(bufold,strlen(bufold));
filter(bufnnew,strlen(bufnnew));
istringstream oldin(bufold);
istringstream nnewin(bufnnew);
old.clear();nnew.clear();
while(oldin>>key>>value)old[key]=value;
while(nnewin>>key>>value)nnew[key]=value;
add.clear();sub.clear();cha.clear();
for(it=nnew.begin();it!=nnew.end();it++)
if(old.find(it->first)==old.end())add.insert(it->first);
for(it=old.begin();it!=old.end();it++)
if(nnew.find(it->first)==nnew.end())sub.insert(it->first);
for(it=nnew.begin();it!=nnew.end();it++)
if(old.find(it->first)!=old.end()&&old[it->first]!=it->second)
cha.insert(it->first);
if(add.empty()&&sub.empty()&&cha.empty()){
cout<<"No changes"<<endl;
}
if(!add.empty()){
cout<<"+"<<*(add.begin());
for(itt=add.begin(),itt++;itt!=add.end();itt++)cout<<","<<*itt;cout<<endl;
}
if(!sub.empty()){
cout<<"-"<<*(sub.begin());
for(itt=sub.begin(),itt++;itt!=sub.end();itt++)cout<<","<<*itt;cout<<endl;
}
if(!cha.empty()){
cout<<"*"<<*(cha.begin());
for(itt=cha.begin(),itt++;itt!=cha.end();itt++)cout<<","<<*itt;cout<<endl;
}
cout<<endl;
}
return 0;
}

51
UVA/1595_Richr_Wong.cpp Normal file
View File

@ -0,0 +1,51 @@
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <sstream>
#include <fstream>
using namespace std;
typedef pair<int,int> point;
int main()
{
int T;
cin>>T;
for(int i=0;i<T;i++)
{
int n;
set<point> data;
cin>>n;
int sum = 0;
for(int j=0;j<n;j++)
{
int x,y;
cin>>x>>y;
sum+=x;
data.insert(point(x*n,y));
}
bool flag = true;
for(set<point>::iterator i=data.begin();i!=data.end();i++)
{
point p = *i;
if(data.find(point(2*sum-p.first,p.second))==data.end())
{
flag = false;
break;
}
}
if(flag==true)
{
cout<<"YES"<<endl;
}
else
{
cout<<"NO"<<endl;
}
}
return 0;
}

61
UVA/230_Richr_Wong.cpp Normal file
View File

@ -0,0 +1,61 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<set>
#include<map>
using namespace std;
const int maxx=1010;
typedef struct Node
{
string book,name;
bool operator<(const struct Node &x)const
{
if(name!=x.name) return name<x.name;
return book<x.book;
}
}node;
int main()
{
set<node> st;
map<string,string> mp;
string s;
node tnode;
int p;
while(getline(cin,s))
{
if(s[0]=='E') break;
p=s.find('"',1);
tnode.book=s.substr(0,p+1);
tnode.name=s.substr(p+5);
mp.insert(make_pair(tnode.book,tnode.name));
st.insert(tnode);
}
set<node> st1;
while(getline(cin,s))
{
if(s[0]=='E') break;
if(s[0]=='S')
{
set<node>::iterator it,it1;
for(it=st1.begin();it!=st1.end();it++)
{
cout<<"Put "<<it->book<<" ";
it1=st.lower_bound(*it);
if(st.empty()||it==st.begin()) cout<<"first"<<endl;
else cout<<"after "<<(--it1)->book<<endl;
st.insert(*it);
}
st1.clear();
cout<<"END"<<endl;
}
else
{
p=s.find('"');
string book=s.substr(p);
tnode.book=book;
tnode.name=mp[book];
if(s[0]=='B') st.erase(tnode);
else st1.insert(tnode);
}
}
}

3
UVA/Readme.md Normal file
View File

@ -0,0 +1,3 @@
#UVa Online Judge (UVA)
[Goto UVa Online Judge](https://uva.onlinejudge.org/ "UVa Online Judge")
[View UVa Problems On Virtual Judge](http://acm.hust.edu.cn/vjudge/problem/toListProblem.action#OJId=UVA&probNum=&title=&source= "UVa Problems")

View File

@ -1,2 +1,3 @@
#UVALive
[Goto UVa Online Judge](https://uva.onlinejudge.org/ "UVa Online Judge")
[View UVa Problems On Virtual Judge](http://acm.hust.edu.cn/vjudge/problem/toListProblem.action#OJId=UVA&probNum=&title=&source= "UVa Problems")

55
VIJOS/1162_lelouch1.pas Normal file
View File

@ -0,0 +1,55 @@
var
i,j,n,min,max,x,y:longint;
s:Array[1..10000000]of shortint;
function long(x,k:longint):longint;
begin
long:=0;
while x>0 do
begin
inc(long);
x:=x div k;
end;
end;
function power(a,b:longint):longint;
var
t:integer;
begin
power:=1;
for t:=1 to b do
power:=power*a;
end;
function num(a,b,l,k:longint):longint;
var
i:longint;
begin
num:=0;
for i:=1 to l do
if odd(i)
then inc(num, a*trunc(power(k,l-i)) )
else inc(num, b*trunc(power(k,l-i)) );
end;
procedure all(k:longint);
var
i,j,l,l1,l2,m:longint;
begin
l1:=long(min,k);
l2:=long(max,k);
for i:=1 to k-1 do
for j:=0 to k-1 do
if i<>j then
begin
for l:=l1 to l2 do
begin
m:=num(i,j,l,k);
if (m<=max)and(m>=min) then
inc(s[m]);
end;
end;
end;
begin
readln(x,y,min,max,n);
fillchar(s,sizeof(s),0);
for i:=x to y do all(i);
for i:=min to max do
if s[i]=n then writeln(i);
end.

40
VIJOS/1162_zdf995.pas Normal file
View File

@ -0,0 +1,40 @@
program num;
var s:array[0..10000000] of integer;
l,a,b,c,d,t,h,w,p:int64;
i,j,m,n:longint;
begin
readln(a,b,c,d,t);
for i:=a to b do
begin
for j:=1 to i-1 do inc(s[j]);
for m:=0 to i-1 do
for n:=0 to i-1 do
if m<>n then
begin
w:=n;
p:=1;
l:=1;
while w<=d do
begin
l:=l+1;p:=p*i;
if l mod 2=0 then
begin
if m<>0 then
begin
w:=w+p*m;
if w<=d then s[w]:=s[w]+1;
end;
end
else
begin
if n<>0 then
begin
w:=w+p*n;
if w<=d then s[w]:=s[w]+1;
end;
end;
end;
end;
end;
for i:=c to d do if s[i]=t then writeln(i);
end.

101
VIJOS/1498.cpp Normal file
View File

@ -0,0 +1,101 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int MAXD = 2000+5;
const int MAXM = 1000+5;
const int MAXN = 1000+ 5;
int N,M,L,K,D;
int XLine[MAXN];
int YLine[MAXM];
struct pack
{
int value;
int id;
pack(int _Value,int _id) :
value(_Value),id(_id){}
};
bool cmp1(const pack& a,const pack& b)
{
return a.value>b.value;
}
bool cmp2(const pack& a,const pack& b)
{
return a.id<b.id;
}
int main()
{
scanf("%d %d %d %d %d",&M,&N,&K,&L,&D);
for(int i=0;i<D;i++)
{
int a,b,c,d;
scanf("%d %d %d %d",&a,&b,&c,&d);
if(a==c)
{
YLine[min(b,d)]++;
}
else
{
XLine[min(a,c)]++;
}
}
int cnt=0;
/*
for(int i=0;i<=N;i++)
{
if(XLine[i]>0)
{
++mcnt;
}
}
//*/
vector<pack> vec;
for(int i=0;i<=M;i++)
{
if(XLine[i]!=0)
{
vec.push_back(pack(XLine[i],i));
}
}
sort(vec.begin(),vec.end(),cmp1);
sort(vec.begin(),vec.begin()+K,cmp2);
for(int i=0;i<K-1;i++)
{
printf("%d ",vec.at(i).id);
}
printf("%d\n",vec.at(K-1).id);
vec.clear();
cnt=0;
/*for(int i=0;i<=M;i++)
{
if(YLine[i]>0)
{
++mcnt;
}
}
//*/
for(int i=0;i<=N;i++)
{
if(YLine[i]!=0)
{
vec.push_back(pack(YLine[i],i));
}
}
sort(vec.begin(),vec.end(),cmp1);
sort(vec.begin(),vec.begin()+L,cmp2);
for(int i=0;i<L-1;i++)
{
printf("%d ",vec.at(i).id);
}
printf("%d\n",vec.at(L-1).id);
return 0;
}

59
VIJOS/1498_1085165020.cpp Normal file
View File

@ -0,0 +1,59 @@
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
struct Node
{
int sum,num;
}x[1005],y[1005];
bool rule1(Node a,Node b)
{
return a.sum>b.sum;
}
bool rule2(Node a,Node b)
{
return a.num<b.num;
}
int main()
{
int m,n,k,l,d;
cin >> m >> n >> k >> l >> d;
for(int i=0;i<=m;i++)
{
x[i].num=i;
}
for(int i=0;i<=n;i++)
{
y[i].num=i;
}
for (int i=0,xi,yi,pi,qi;i<d;i++)
{
scanf("%d %d %d %d",&xi,&yi,&pi,&qi);
if(xi==pi)
{
y[yi<qi?yi:qi].sum++;
}
else if(yi==qi)
{
x[xi<pi?xi:pi].sum++;
}
}
sort(x,x+m,rule1);
sort(y,y+n,rule1);
sort(x,x+k,rule2);
sort(y,y+l,rule2);
for(int i=0;i<k;i++)
{
printf("%d ",x[i].num);
}
cout << endl;
for(int i=0;i<l;i++)
{
printf("%d ",y[i].num);
}
return 0;
}

58
VIJOS/1498_2868647513.cpp Normal file
View File

@ -0,0 +1,58 @@
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
struct zong{
int sum,num;
}y[2001];
struct heng{
int sum,num;
}x[2001];
int dog(const zong&a,const zong&b)
{
return a.sum>b.sum;
}
int cat(const heng&a,const heng&b)
{
return a.sum>b.sum;
}
int tiger(const zong&a,const zong&b)
{
return a.num<b.num;
}
int sheep(const heng&a,const heng&b)
{
return a.num<b.num;
}
int main()
{
int m,n,k,l,d,x1,y1,x2,y2,i;
scanf("%d%d%d%d%d",&m,&n,&k,&l,&d);
for(i=1;i<=m;i++){
x[i].num=i;
}
for(i=1;i<=n;i++){
y[i].num=i;
}
for(i=1;i<=d;i++){
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
if(x1==x2){
y[y1<y2?y1:y2].sum++;
}
else{
x[x1<x2?x1:x2].sum++;
}
}
sort(x+1,x+m+1,cat);
sort(y+1,y+n+1,dog);
sort(x+1,x+k+1,sheep);
sort(y+1,y+l+1,tiger);
for(i=1;i<=k;i++){
printf("%d ",x[i].num);
}
printf("\n");
for(i=1;i<=l;i++){
printf("%d ",y[i].num);
}
return 0;
}

65
VIJOS/1498_Max0857.cpp Normal file
View File

@ -0,0 +1,65 @@
#include<iostream>
#include<stdlib.h>
#include<string.h>
using namespace std;
struct node
{
int wei;
int sum;
};
int min(int a,int b)
{
if(a>b) return b;
else return a;
}
int cmp(const void *a,const void *b)
{
node n1=*(node *)a;
node n2=*(node *)b;
if(n2.sum!=n1.sum) return n2.sum-n1.sum;
else
return n2.wei-n1.wei;
}
int cmp1(const void *a,const void *b)
{
return *(int *) a-*(int *) b;
}
int main()
{
node Xxian[1001];
node Yxian[1001];
int pai1[1001],pai2[1001];
memset(Xxian,0,sizeof(Xxian));
memset(Yxian,0,sizeof(Yxian));
int n,m,k,l,d;
int x1,y1,x2,y2;
cin>>n>>m>>k>>l>>d;
for(int i=1;i<=d;i++)
{
cin>>x1>>y1>>x2>>y2;
if(x1==x2)
{
Xxian[min(y1,y2)].sum++;
Xxian[min(y1,y2)].wei=min(y1,y2);
}
else
{
Yxian[min(x1,x2)].sum++;
Yxian[min(x1,x2)].wei=min(x1,x2);
}
}
qsort(Xxian,1000,sizeof(node),cmp);
qsort(Yxian,1000,sizeof(node),cmp);
for(int b=0;b<k;b++)
pai1[b]=Yxian[b].wei;
qsort(pai1,k,sizeof(int),cmp1);
for(int b=0;b<k-1;b++)
cout<<pai1[b]<<' ';
cout<<pai1[k-1]<<'\n';
for(int c=0;c<l;c++)
pai2[c]=Xxian[c].wei;
qsort(pai2,l,sizeof(int),cmp1);
for(int c=0;c<l-1;c++)
cout<<pai2[c]<<' ';
cout<<pai2[l-1]<<'\n';
}

34
VIJOS/1498_wlzxczy.pas Normal file
View File

@ -0,0 +1,34 @@
var
m,n,k,l,d,q,w,e,r,i,j,max,u,f:longint;
a,b,uu:array[1..1000]of longint;
procedure sw(var i,j:longint);
var p:longint;
begin
p:=uu[i];uu[i]:=uu[j];uu[j]:=p;
inc(i);dec(j);
end;
procedure qsort(i,j:longint);
var l,r,mid:longint;
begin
l:=i;r:=j;mid:=uu[(l+r) div 2];
repeat
while uu[i]<mid do inc(i); while uu[j]>mid do dec(j);
if i<=j then sw(i,j);
until i>j;
if i<r then qsort(i,r); if l<j then qsort(l,j); end; begin readln(m,n,k,l,d); for i:=1 to d do begin readln(q,w,e,r); if q=e then if w<r then inc(a[w]) else inc(a[r]); if w=r then if q<e then inc(b[q]) else inc(b[e]); end; for i:=1 to k do begin max:=0; for j:=1 to m-1 do if b[j]>max then begin max:=b[j];f:=j; end;
inc(u);uu[u]:=f;b[f]:=0;
end;
qsort(1,u);
for i:=1 to u-1 do write(uu[i],' ');
writeln(uu[u]);
u:=0;
for i:=1 to l do
begin max:=0;
for j:=1 to n-1 do
if a[j]>max then begin max:=a[j];f:=j; end;
inc(u);uu[u]:=f;a[f]:=0;
end;
qsort(1,u);
for i:=1 to u-1 do write(uu[i],' ');
writeln(uu[u]);
end.

View File

@ -1,3 +1,7 @@
#Velocious Informatics Judge Online System (VIJOS)
**Also named as: 高效信息学在线评测系统**
[Goto Vijos Home Page](https://vijos.org/ "Vijos - Home")
账号(已加密): -30 -45 -33 -59 -49 -46 -2 -41 -28 -7 -30 -63 -44 -39 -127 -117
密码(已加密): -35 -42 -62 -57 -33 -33 -11 -4
Key: KERNELmixer